MyBatis learning summary (3) - optimization of MyBatis configuration file configuration

 

1. The configuration of connecting to the database is placed in a properties file alone

  The configuration of connecting to the database above is written in mybatisConf.xml, this article is directly placed in db.properties and referenced in mybatisConf.xml

mybatisConf.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

< configuration > 
    <!-- Refer to the db.properties configuration file --> 
    < properties resource ="db.properties"  />

    < environments default = "development" > 
        < environment id = "development" > 
            < transactionManager type = "JDBC"  /> 
            <!-- Configuration database connection information --> 
            < dataSource type = "POOLED" > 
                <!-- value attribute The 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="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
      <!-- 
        Register the userMapper.xml file,
         userMapper.xml is located under the package me.gacl.mapping, so the resource is written as me/gacl/mapping/userMapper.xml
        -->
        <mapper resource="com/myl/mapping/userMapper.xml"/>
        
        <!-- Register UserMapper mapping interface --> 
        < mapper class ="com.myl.mapping.UserMapperInter"  /> 
    </ mappers >
    
</configuration>

 

 In the db.properties file, write the database driver that needs to be used to connect to the database, the connection URL address, user name, and password

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis
username=root
password=

 

2. Define aliases for entity classes and simplify references in SQL mapping xml files

Before, when we mapped the reference entity class in the xml file in sql, we needed to write the full class name of the entity class (package name + class name)

userMapper.xml

<!-- 修改数据 -->
    <update id="updateUser" parameterType="com.myl.entity.User">
        update user set name=#{name},age=#{age},job=#{job},deptno=#{deptno},hdate=#{hdate} where id=#{id}
    </update>

parameterType=" me.myl.entity.User "The full class name of the entity class User written here is me.myl.entity.User. It is very troublesome to write such a long list of content every time, and we hope to be abbreviated as the following form

<!-- 修改数据 -->
    <update id="updateUser" parameterType="_User">
        update user set name=#{name},age=#{age},job=#{job},deptno=#{deptno},hdate=#{hdate} where id=#{id}
    </update>

  parameterType=" _ User " is much simpler to write. In order to achieve this effect, we need to define an alias as "_User" for entity class="me.myl.entity.User" in the conf.xml file . The method is as follows:

Add the following configuration to the <configuration></configuration> tag in the mybatisConf.xml file:

<!-- Define an alias for entity class="me.myl.entity.User" as "_User" --> 
    < typeAliases > 
        < typeAlias ​​type ="com.myl.entity.User" alias ="_User" /> 
    </ typeAliases >

 

In addition to using <typeAlias ​​type="me.myl.entity.User" alias="_User"/> to set an alias for an entity class separately, we can also use the following method to batch a certain package All entity classes set aliases as follows:

mybatis added and modified as

< typeAliases > 
        <!-- define an alias for entity class="me.myl.entity.User" as "_User" --> 
        <!-- <typeAlias ​​type="com.myl.entity.User" alias=" _User"/> -->
        
        <!-- Configure aliases for all entity classes under the me.gacl.domain package. MyBatis's default way of setting aliases 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
         -->
      <package name="com.myl.entity" />
</typeAliases>

  <package name="me.myl.entity"/> means setting aliases for all entity classes under this package. The default way of setting aliases in 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.myl.entity.User will be set to User.

userMapper.xml can be modified to:

<!-- 修改数据 -->
    <update id="updateUser" parameterType="User">
        update user set name=#{name},age=#{age},job=#{job},deptno=#{deptno},hdate=#{hdate} where id=#{id}
    </update>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326782616&siteId=291194637