Mybatis Framework Learning (2) mybatis.cfg.xml Configuration Notes

Keywords: xml JDBC Mybatis MySQL

Click Open Link


I used Idea's IDE environment. To create xml more easily and quickly using mybatis framework, I created templates in my IDE environment.

Specific template creation can be viewed by clicking on the following link: ----> to be added later

Under the resource folder, create the configuration xml file - > mybatis. cfg. xml


Configuration: This is the root node of the file, under which is the global configuration of the mybatis framework

Properties:

The resource attribute in the 1/Properties tag can introduce an external property file, resource= "property file path"

<properties resource="user/database.properties">  </properties>

2/Subtags can also be configured under the label of Properties, as follows:

<properties >
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms"/>
        <property name="user" value="root"/>
        <property name="password" value="mysql"/>-->
    </properties>
It is worth noting that after adopting the first one, the second one will be covered by the first one regardless of right or wrong.

Settings

This tag is used to modify the behavior of the Mybatis runtime: For more information, you can refer to this link: Click Open Link

   <settings>  
        <setting name="cacheEnabled" value="true" />     //Whether the cache is started or not
        <setting name="lazyLoadingEnabled" value="true" />   //Whether to start lazy loading
        <setting name="multipleResultSetsEnabled" value="true" /> 
    </settings>
typeAliases

This tag i can be used to alias java classes for easy use in mapper's xml files

You can name a class or package directly. It's worth noting that frameworks ignore case in mapper's xml return type

 <typeAliases>
        <typeAlias type="cn.smbms.pojo.User" alias="suser"></typeAlias>
        <package name="cn.smbms.pojo"></package>
 </typeAliases>
....

Environment configuration, environment can configure more than one set, but must default to one set, default value is the id of the environment

<environments default="one">
    <environment id="one">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"></property>
             <property name="url" value="${url}"></property>
            <!--No duplication?????-->
            <property name="username" value="${user}"></property>
            <property name="password" value="${password}"></property>
        </dataSource>
    </environment>
</environments>

 Among them: <transactionManager type="JDBC"></transactionManager> transaction manager has two values: JDBC and managed.
                   Data Source Data Source   
                        There are three type s [UNPOOLED|POOLED|JNDI]
<dataSource type="POOLED">Data source, with three values: UNPOOLED | POOLED | JNDI
Mappers
This tag manages mapper's xml file. If you want the sql statement in mapper to work, you must introduce it here.
  <mappers>
        <mapper resource="user/usermapper.xml"></mapper>
        <mapper resource="cn/smbms/dao/user/UserMapper.xml"></mapper>
    </mappers>



In the mapper.xml file

The problem of configuring paths:

In Ide environment, when building a file package, use /, not..





Posted by serg91 on Sun, 19 May 2019 20:46:15 -0700