实例:
mybatis_config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <environments default="development">
  7. <environment id="development">
  8. <transactionManager type="JDBC"/>
  9. <dataSource type="POOLED">
  10. <property name="driver" value="com.mysql.jdbc.Driver"/>
  11. <property name="url" value="jdbc:mysql://localhost:3306/mybatis_achang"/>
  12. <property name="username" value="root"/>
  13. <property name="password" value="00000"/>
  14. </dataSource>
  15. </environment>
  16. </environments>
  17. <mappers>
  18. <mapper resource="EmployeeDao.xml"/>
  19. </mappers>
  20. </configuration>

image.png

2.1 properties(属性)

作用:通过properties标签引入外部内容

properties标签:和Spring的context:property-placeholder;引用外部配置文件
resource属性:从类路径下引入
url属性:引用磁盘路径或网络路径

dbconfig.properties:

username=root
password=00000
url=jdbc:mysql://localhost:3306/mybatis_achang
driver=com.mysql.jdbc.Driver

mybatis_config.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>
    <properties resource="dbconfig.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--${}取出配置文件中的值-->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="EmployeeDao.xml"/>
    </mappers>

</configuration>

2.2 settings(设置)

settings是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。

说明文档:https://mybatis.org/mybatis-3/zh/configuration.html#settings

举例:mapUnderscoreToCamelCase

数据库字段名与bean对象属性对应驼峰原则
数据库:login_account
javabean:loginAccount
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

image.pngimage.png

xml配置:

<settings>
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

2.3 typeAliases(类型别名)

推荐还是使用全类名!!!

类型别名:为常用的类型起别名
        typeAlias属性:就是为一个javaBean起别名;别名默认就是类名(不区分大小写),配置文件中就可以用别名了
                alias属性:指定一个别名
        package属性:批量起别名,配合注解使用
                name属性:指定一个包名,默认别名就是类名
                @alias()注解:起别名
<typeAliases>
    <typeAlias type="com.achang.bean.Employee" alias="emp"/>//起别名
    <package name="com.achang.bean"/>//批量起别名
</typeAliases>

下面是一些为常见的 Java 类型内建的类型别名。它们都是不区分大小写的,注意,为了应对原始类型的命名重复,采取了特殊的命名风格。
image.png

2.4 typeHandlers(类型处理器)

  • 无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时, 都会用类型处理器将获取的值以合适的方式转换成 Java 类型。 ```xml
![image.png](https://cdn.nlark.com/yuque/0/2021/png/2963105/1630832052276-f5037fe8-f6c0-46a4-ae16-d9e9c586cef1.png#clientId=udd8d712d-d57e-4&from=paste&height=421&id=u7d497476&margin=%5Bobject%20Object%5D&name=image.png&originHeight=421&originWidth=941&originalType=binary&ratio=1&size=63281&status=done&style=none&taskId=ucf5367ed-df01-4e19-b0ab-0f75a429572&width=941)

<a name="cgvQm"></a>
## 2.5 plugins(插件)

- 插件是MyBatis提供的一个非常强大的机制,我们可以通过插件来修改MyBatis的一些核心行为。插件通过动态代理机制,可以介入四大对象的任何一个方法的执行。后面会有专门的章节我们来介绍mybatis运行原理以及插件
```java
•Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)

•ParameterHandler (getParameterObject, setParameters)

•ResultSetHandler (handleResultSets, handleOutputParameters)

StatementHandler (prepare, parameterize, batch, update, query)

2.6 environments(环境)

default属性:默认使用哪个环境;填写某个environment标签的id
environment标签:配置一个具体的环境;每一个环境都需要一个事务管理器和数据源
    id属性:当前环境的唯一标识
    transactionManager标签:事务管理器

后来数据源、事务控制管理都Spring来做;
<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <!--${}取出配置文件中的值-->
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </dataSource>
    </environment>
</environments>

2.7 databaseIdProvider(数据库厂商标识)

作用:
mybatis用来考虑数据库移植性的-

name属性:数据库厂商标识  
value属性:给数据库厂商标识起别名
    MYSQL、Oracle、SQL Server;

mybatis_config.xml

type是固定写死的:

<databaseIdProvider type="DB_VENDOR">
    <property name="MYSQL" value="mysql"/>
    <property name="SQL Server" value="sqlserver"/>
    <property name="Oracle" value="oracle"/>
</databaseIdProvider>

EmployeeDao.xml

databaseId属性:选择数据库厂商别名
<!--能精确匹配就精确匹配,不能就模糊匹配-->
<select id="getEmpById" resultType="com.achang.bean.Employee">
    select * from t_employee where id = #{id}
</select>
<select id="getEmpById" resultType="com.achang.bean.Employee" databaseId="mysql">
    select * from t_employee where id = #{id}
</select>
<select id="getEmpById" resultType="com.achang.bean.Employee" databaseId="oracle">
    select * from t_employee where id = #{id}
</select>

2.8 mappers(映射器)

 <!-- class属性:引用接口全类名
                可以将xml和dao接口放在同一个文件目录下,并文件名和接口名相同
             resource属性:在类路径下找sql映射文件
             url属性:从磁盘和网络路径引用sql映射文件
                        配合使用:重要的dao写配置;简单的用头注解搞定
    package标签:批量注册;要求xml和dao类接口在同一个文件夹下且名字相同(可以使用设置资源文件)
       name属性:dao所在的包名
-->

EmployeeAnnotationDao:在dao接口头注解上写对应的sql语句

public interface EmployeeAnnotationDao {

    @Select("select * from t_employee where id = #{id}")
    public Employee getEmpById(Integer id);

    @Update("update t_employee set empname = #{empName},gender = #{gender},email = #{email} where id = #{id}\n")
    public int updateEmp(Employee employee);

    @Delete("delete from t_employee where id = #{id}")
    public boolean deleteEmpById(Integer id);

    @Insert("insert into t_employee(empname,gender,email)" +
            "values(#{empName},#{gender},#{email}))")
    public int insertEmp(Employee employee);

}

使用class导入接口注解头sql语句类

    <!--写好的sql映射文件,需要使用mappers注册-->
    <mappers>
        <mapper class="com.achang.dao.EmployeeAnnotationDao"/>
        <package name=""/>
    </mappers>
//通过注解导入sql语句;查询
@Test
public void test5() throws IOException {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    EmployeeAnnotationDao mapper = sqlSession.getMapper(EmployeeAnnotationDao.class);
    System.out.println(mapper.getEmpById(3));

}