Mybatis - CONFIGURATION XMLIn the previous chapter, we have seen how to install MyBatis. This chapter discusses how to configure MyBatis using XML file. Since we are communicating with the database, we have to configure the details of the database. Configuration XML is the file used for the XML-based configuration. By using this file, you can configure various elements. The following programing is a typical structure of MyBatis configuration file.
<configuration>
<typeAliases>
<typeAlias alias="class_alias_Name" type="absolute_clas_Name"/>
</typeAliases>
<environments default="default_environment _name">
<environment id="environment_id">
<transactionManager type="JDBC/MANAGED"/>
<dataSource type="UNPOOLED/POOLED/JNDI">
<property name="driver" value="database_driver_class_name"/>
<property name="url" value="database_url"/>
<property name="username" value="database_user_name"/>
<property name="password" value="database_password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="path of the configuration XML file"/>
</mappers>
</configuration>
Let us discuss the important elements (tags) of the configuration XML file one by one. environments tag Within the environments element, we configure the environment of the database that we use in our application. In MyBatis, you can connect to multiple databases by configuring multiple environment elements. To configure the environment, we are provided with two sub tags namely transactionManager and dataSource. transactionManager tag MyBatis supports two transaction managers namely JDBC and MANAGED
It is used to configure the connection properties of the database, such as driver-name, url, user-name, and is of three types namely:
<environments default="development">
<environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/details"/> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> typeAliases tag Instead of specifying the absolute class name everywhere, we can use typeAliases, a shorter name for a Java type. Suppose, we have a class Student in Student.java file within the package named tutorials_point.com.mybatis_examples, then the absolute class name will be tutorials_point.com.mybatis_examples.Student. Instead of using this name to address the class every time, you can declare an alias to that class as shown below:
<typeAliases>
<typeAlias alias="Student" type="mybatis.Student"/> </typeAliases> mappers tag Mapper XML file is the important file, which contains the mapped SQL statements. Mapper’s element is used to configure the location of these mappers xml files in the configuration file of MyBatis (this element contains four attributes namely resources, url, class, and name). For example, the name of the mapper xml file is Student.xml and it resides in the package named as mybatis, then you can configure the mapper tag as shown below.
<mappers>
<mapper resource="mybatis/Student.xml"/> </mappers>
MyBatis with MySQL database MySQL is one of the most popular open-source database systems available today. Let us create a SqlMapConfig.xml configuration file to connect to mysql database. The example given below are the dataSource properties (driver-name, url, user-name, and password) for MySQL database:
We use the dataSource of type UNPOOLED, which means new connection is created for each database operation. Therefore, it is recommended to close the connection manually after the completion of database operations. SqlMapConfig.xml Below given is the XML configuration for the examples used in this tutorial. Copy the content given below in a text file and save it as SqlMapConfig.xml. We are going to use this file in all the examples given in this tutorial.
<configuration>
<environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/details"/> <property name="username" value="root"/> <property name="password" value="password"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis/Student.xml"/> </mappers> </configuration> |