Configuration parsing

Configuration parsing

1. The core profile

  • mybatis-config.xml
  • MyBatis configuration file contains settings and properties that can deeply affect MyBatis behavior. Top-level configuration document is structured as follows:
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)

2. Environment Configuration (environments)

MyBatis can be configured to accommodate a variety of environments,

But remember: Although you can configure multiple environments, but can choose only one instance of each SqlSessionFactory environment.

mybatis default transaction manager is jdbc, connection pool is pooled

3. Properties (Properties)

We can attribute properties to achieve the reference profile

These properties are externally configured and dynamically replaceable, may be arranged in a typical java file attributes, sub-elements can also be transmitted by the properties element ---------------- --- db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=ture&characterEncoding=UTF-8
username=root
password=123456

Introduced in the core configuration file

<properties resource="db.properties"/>
<dataSource type="POOLED">
    <property name="driver" value="${driver}"/>
    <property name="url" value="${url}"/>
    <property name="username" value="${username}"/>
    <property name="password" value="${password}"/>
</dataSource>

4. Alias ​​type (typeAliases)

  • Type alias is set a short name for a Java type
  • Significance only in the presence of fully qualified name is used to reduce redundant
<typeAliases>
    <typeAlias type="cn.pinked.pojo.User" alias="User"/>
</typeAliases>
<select id="getUserList" resultType="User">
    select * from mybatis.user
</select>

You can also specify a package name, MyBatis will need to search for the package name following Java Bean, scanning entity class package, its default alias for the class name of the class, the first letter lowercase

<typeAliases>
    <package name="cn.pinked.pojo"/>
</typeAliases>

Guess you like

Origin www.cnblogs.com/pinked/p/12168220.html