1. The configuration for connecting to the database is placed in a properties file separately
Previously, we directly wrote the connection configuration information of the database in the conf.xml file of MyBatis, as follows:
<?xml version="." encoding="UTF-"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config .//EN" "http://mybatis.org/dtd/mybatis--config.dtd"><configuration><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- Configure database connection information--><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:/mybatis" /><property name="username" value="root" /><property name="password" value="XDP" /></dataSource></environment></environments></configuration>
In fact, we can write the connection configuration information of the database in a properties file, and then reference the properties file in the conf.xml file. The specific method is as follows:
1. Create a new db.properties file in the src directory, as shown in the figure below:
In the db.properties file, the database driver you need to use to connect to the database, the connection URL address, user name, and password are as follows:
driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:/mybatisname=rootpassword=XDP
2. Reference the db.properties file in the conf.xml file of MyBatis, as follows:
<?xml version="." encoding="UTF-"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config .//EN" "http://mybatis.org/dtd/mybatis--config.dtd"><configuration><!-- Quoting db.properties configuration file--><properties resource="db.properties"/><!-- development : Development mode work: Work mode--><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- Configure database connection information--><dataSource type="POOLED"><!-- value attribute value refers to the value configured in the db.properties configuration file--><property name="driver" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${name}" /><property name="password" value="${password}" /></dataSource></environment></environments></configuration> 2. Define alias for entity classes to simplify references in SQL mapping XML files
Previously, when we referenced entity classes in SQL-mapping XML files, we needed to write the full class name of the entity class (package name + class name), as follows:
<!-- Create user (Create) --><insert id="addUser" parameterType="me.gacl.domain.User">insert into users(name,age) values(#{name},#{age})</insert> parameterType="me.gacl.domain.User" The full class name of the entity class User written here is me.gacl.domain.User. It is very troublesome to write such a long list of contents every time, and we hope it can be abbreviated into the following form.
<insert id="addUser2" parameterType="_User">insert into users(name,age) values(#{name},#{age})</insert> This way of writing parameterType="_User" is much simpler. In order to achieve this effect, we need to define an alias "_User" for the entity class="me.gacl.domain.User" in the conf.xml file. The specific method is as follows:
Add the following configuration to the <configuration></configuration> tag in the conf.xml file:
<typeAliases><typeAlias type="me.gacl.domain.User" alias="_User"/></typeAliases>
In this way, an alias is defined for the me.gacl.domain.User class, and in the future, _User represents the me.gacl.domain.User class. In this way, wherever the me.gacl.domain.User class is needed to refer to the me.gacl.domain.User class in the sql mapped XML file, you can use _User instead, which achieves the purpose of simplifying entity class references.
In addition to using <typeAlias type="me.gacl.domain.User" alias="_User"/> to set alias for a certain entity class separately, we can also set alias for all entity classes under a certain package in batches using the following method, as follows:
<!-- Configure the alias of the entity class. The purpose of configuring the entity class alias is to use the alias of the entity class instead of the entity class when referring to the entity class to achieve the purpose of abbreviation--><typeAliases><!-- Configure an alias for the entity class me.gacl.domain.User_User --><!-- <typeAlias type="me.gacl.domain.User" alias="_User"/> --><!-- Configure alias for all entity classes under the me.gacl.domain package. The default way to set the alias of MyBatis is to remove the simple class name after the package where the class is located, such as me.gacl.domain.User, the alias of the entity class will be set to User--><package name="me.gacl.domain"/></typeAliases>
<package name="me.gacl.domain"/> means setting aliases for all entity classes below this package. The default way to set aliases by MyBatis is to remove the simple class name after the package where the class is located. For example, the alias of the entity class me.gacl.domain.User will be set to User.
The above is the MyBatis learning tutorial (III) introduced to you by the editor - MyBatis configuration optimization. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!