MyBatis学习总结(四)——MyBatis缓存与代码生成

目录

 一、MyBatis 缓存

缓存可以提高系统性能,可以加快访问速度,减轻服务器压力,带来更好的用户体验。缓存用空间换时间,好的缓存是缓存命中率高的且数据量小的。缓存是一种非常重要的技术。

1.0、再次封装 SqlSessionFactoryUtils

为了配置缓存的学习我们将工具类再次封装。

原 SqlSessionFactoryUtil 工具类如下:

package com.zhangguo.mybatis03.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**

  • MyBatis 会话工具类

  • */
    public class SqlSessionFactoryUtil {

    /**

    • 获得会话工厂

    • */
      public static SqlSessionFactory getFactory(){
      InputStream inputStream
      = null;
      SqlSessionFactory sqlSessionFactory
      =null;
      try{
      //加载 conf.xml 配置文件,转换成输入流
      inputStream = SqlSessionFactoryUtil.class.getClassLoader().getResourceAsStream("mybatisCfg.xml");

       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据配置文件的输入流构造一个SQL会话工厂</span>
       sqlSessionFactory = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SqlSessionFactoryBuilder().build(inputStream);
      

      }
      finally {
      if(inputStream!=null){
      try {
      inputStream.close();
      }
      catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
      return sqlSessionFactory;
      }

    /**

    • 获得 sql 会话,是否自动提交
    • */
      public static SqlSession openSession(boolean isAutoCommit){
      return getFactory().openSession(isAutoCommit);
      }

    /**

    • 关闭会话
    • */
      public static void closeSession(SqlSession session){
      if(session!=null){
      session.close();
      }
      }

}

View Code

上面的代码中当我们每次获取 SQLSession 时都要实例化 sqlSessionFactory,效率不高。可以使用单例改进:

package com.zhangguo.mybatis03.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**

  • MyBatis 会话工具类

  • */
    public class SqlSessionFactoryUtils {

    /**会话工厂*/
    private static SqlSessionFactory factory;

    static {
    try {
    /获得配置文件的文件流/
    InputStream inputStream
    =Resources.getResourceAsStream("mybatisCfg.xml");
    //初始化工厂
    factory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**

    • 获得会话对象
    • 指定是否自动提交
    • */
      public static SqlSession openSqlSession(boolean isAutoCommit){
      return getFactory().openSession(isAutoCommit);
      }

    public static SqlSessionFactory getFactory() {
    return factory;
    }
    public static void setFactory(SqlSessionFactory factory) {
    SqlSessionFactoryUtils.factory
    = factory;
    }

    /**

    • 关闭会话
    • */
      public static void closeSession(SqlSession session){
      if(session!=null){
      session.close();
      }
      }
      }

1.1、MyBatis 缓存概要

在一个系统中查询的频次远远高于增删改,据三方统计不同系统比例在 9:1-7:3 之间。正如大多数持久层框架一样,MyBatis 同样提供了一级缓存二级缓存的支持

(1)、一级缓存基于 PerpetualCache 的 HashMap 本地缓存,其存储作用域为 Session,当 Session flush close 之后,该Session 中的所有 Cache 就将清空

(2)、二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap 存储,不同在于其存储作用域为 Mapper(Namespace)并且可自定义存储源,如 Ehcache。

(3)、对于缓存数据更新机制,当某一个作用域 (一级缓存 Session/ 二级缓存 Namespaces) 的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被 clear。

1.2、默认 MyBatis 的一级缓存是开启的

测试用例:

    /**缓存测试*/
    @Test
    public void cacheTest(){
        //打开一个会话,不自动提交
        SqlSession session1 = SqlSessionFactoryUtils.openSqlSession(false);
        //获得一个映射器
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        //查询单个对象通过编号
        Student student1 = mapper1.selectStudentById(1);
        System.out.println(student1);
    Student student2 </span>= mapper1.selectStudentById(1<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(student2);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭</span>

SqlSessionFactoryUtils.closeSession(session1);
}

结果:

虽然查询了二次,但只向数据库发送了一次 SQL 请求,因为第二次是在缓存中获得的数据。

1.3、一级缓存仅在同一个会话(SQLSession)中有效

测试用例:

    /**缓存测试*/
    @Test
    public void cacheTest(){
        //打开一个会话 1,不自动提交
        SqlSession session1 = SqlSessionFactoryUtils.openSqlSession(false);
        //获得一个映射器
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        //查询单个对象通过编号
        Student student1 = mapper1.selectStudentById(1);
        System.out.println(student1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开一个会话2,不自动提交</span>
    SqlSession session2 = SqlSessionFactoryUtils.openSqlSession(<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得一个映射器</span>
    StudentMapper mapper2 = session2.getMapper(StudentMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    Student student2 </span>= mapper2.selectStudentById(1<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(student2);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭</span>

SqlSessionFactoryUtils.closeSession(session1);
}

结果:

从上图可以看出此时并没有使用缓存,向数据库查询了二次,因为第二次查询使用的是新的会话,而一级缓存必须在同一个会话中。

1.4、清空一级缓存

(1)、当对表执行增删改时缓存将清空

测试用例:

    /**缓存测试*/
    @Test
    public void cacheTest(){
        //打开一个会话 1,不自动提交
        SqlSession session1 = SqlSessionFactoryUtils.openSqlSession(false);
        //获得一个映射器
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        //查询单个对象通过编号
        Student student1 = mapper1.selectStudentById(1);
        System.out.println(student1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行更新</span>
    Student lili=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Student();
    lili.setId(</span>5<span style="color: rgba(0, 0, 0, 1)">);
    lili.setSex(</span>"girl"<span style="color: rgba(0, 0, 0, 1)">);
    mapper1.updateStudent(lili);

    Student student2 </span>= mapper1.selectStudentById(1<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(student2);

    SqlSessionFactoryUtils.closeSession(session1);
}</span></pre>

结果:

从日志中可以看出第二次查询也发送了 sql 到数据库中,并没有使用缓存,是因为执行了更新操作缓存已被清空。

此时数据库中的数据其实并未真的更新,如下所示:

因为没有手动提交,可以设置自动提交

/**缓存测试*/
    @Test
    public void cacheTest(){
        //打开一个会话 1,不自动提交
        SqlSession session1 = SqlSessionFactoryUtils.openSqlSession(false);
        //获得一个映射器
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        //查询单个对象通过编号
        Student student1 = mapper1.selectStudentById(1);
        System.out.println(student1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行更新</span>
    Student lili=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Student();
    lili.setId(</span>5<span style="color: rgba(0, 0, 0, 1)">);
    lili.setSex(</span>"girl"<span style="color: rgba(0, 0, 0, 1)">);
    mapper1.updateStudent(lili);

    Student student2 </span>= mapper1.selectStudentById(1<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(student2);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提交</span>

session1.commit();
SqlSessionFactoryUtils.closeSession(session1);
}

View Code

提交后的结果

(2)、手动清空

测试用例:

   /**缓存测试*/
    @Test
    public void cacheTest(){
        //打开一个会话 1,不自动提交
        SqlSession session1 = SqlSessionFactoryUtils.openSqlSession(false);
        //获得一个映射器
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        //查询单个对象通过编号
        Student student1 = mapper1.selectStudentById(1);
        System.out.println(student1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行手动更新</span>

session1.clearCache();

    Student student2 </span>= mapper1.selectStudentById(1<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(student2);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提交</span>

session1.commit();
SqlSessionFactoryUtils.closeSession(session1);
}

结果:

从日志中可以看到第二次查询并未使用缓存因为执行了手动清空缓存,没有缓存可用则再次查询数据库。

小结:当 Session flush 或 close 之后,该 Session 中的所有 Cache 就将清空;执行 CUD 也将会自动清空;手动清空;

1.5、开启二级缓存

默认二级缓存是不开启的,需要手动进行配置

1.5.1、全局开关

默认是 true,如果它配成 false,其余各个 Mapper XML 文件配成支持 cache 也没用。

    <settings>
        <!--设置是否允许缓存-->
        <setting name="cacheEnabled" value="true"/>
        <!--设置日志输出的目标-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

1.5.2、单个 Mapper XML 映射文件开关

默认 Mapper XML 映射文件是不采用 cache。在配置文件加一行就可以支持 cache:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhangguo.mybatis03.dao.StudentMapper">

<cache/>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">select </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="selectStudentById"</span><span style="color: rgba(255, 0, 0, 1)"> resultType</span><span style="color: rgba(0, 0, 255, 1)">="Student"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    SELECT id,name,sex from student where id=#{id}
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">select</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</mapper>

可以在开启二级缓存时候,手动配置一些属性

<cache eviction="LRU" flushInterval="100000" size="1024" readOnly="true"/>

各个属性意义如下:

  • eviction:缓存回收策略
    - LRU:最少使用原则,移除最长时间不使用的对象
    - FIFO:先进先出原则,按照对象进入缓存顺序进行回收
    - SOFT:软引用,移除基于垃圾回收器状态和软引用规则的对象
    - WEAK:弱引用,更积极的移除移除基于垃圾回收器状态和弱引用规则的对象
  • flushInterval:刷新时间间隔,单位为毫秒,这里配置的 100 毫秒。如果不配置,那么只有在进行数据库修改操作才会被动刷新缓存区
  • size:引用额数目,代表缓存最多可以存储的对象个数
  • readOnly:是否只读,如果为 true,则所有相同的 sql 语句返回的是同一个对象(有助于提高性能,但并发操作同一条数据时,可能不安全),如果设置为 false,则相同的 sql,后面访问的是 cache 的 clone 副本。

1.5.3、Mapper statement 开关

Mapper XML 文件配置支持 cache 后,文件中所有的 Mapper statement 就支持了。此时要个别对待某条,需要:

<select id="selectStudentById" resultType="Student" useCache="false">
    SELECT id,name,sex from student where id=#{id}
</select>

可以在 Mapper 的具体方法下设置对二级缓存的访问意愿:

  • useCache 配置

    ​ 如果一条语句每次都需要最新的数据,就意味着每次都需要从数据库中查询数据,可以把这个属性设置为 false,如:

<select id="selectAll" useCache="false">
  • 刷新缓存(就是清空缓存)

    ​ 二级缓存默认会在 insert、update、delete 操作后刷新缓存,可以手动配置不更新缓存,如下:

<update id="updateById" flushCache="false" />

1.5.4、实现可序列化接口

 如果未实现可序列化接口,会引发异常。

修改 POJO 对象,增加实现可序列化接口:

package com.zhangguo.mybatis03.entities;

import java.io.Serializable;

/**

  • 学生实体
    */
    public class Student implements Serializable {
    private int id;
    private String name;
    private String sex;

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }

    @Override
    public String toString() {
    return "Student{" +
    "id=" + id +
    ", name='"+ name +''' +
    ", sex='"+ sex +''' +
    '}';
    }
    }

View Code

1.5.5、注意事项

1、如果 readOnly 为 false,此时要结果集对象是可序列化的。
<cache readOnly="false"/>
2、在 SqlSession 未关闭之前,如果对于同样条件进行重复查询,此时采用的是 local session cache,而不是上面说的这些 cache。
3、MyBatis 缓存查询到的结果集对象,而非结果集数据,是将映射的 POJO 对象集合缓存起来。
4、面对一定规模的数据量,内置的 cache 方式就派不上用场了;
5、对查询结果集做缓存并不是 MyBatis 框架擅长的,它专心做的应该是 sql mapper。采用此框架的 Application 去构建缓存更合理,比如采用 Redis、Ehcache、OSCache、Memcached 等

当我们的配置文件配置了 cacheEnabled=true 时,就会开启二级缓存,二级缓存是 mapper 级别的,也就说不同的 sqlsession 使用同一个 mapper 查询是,查询到的数据可能是另一个 sqlsession 做相同操作留下的缓存。

查询数据的顺序为:二级缓存 -> 一级缓存 -> 数据库

1.6、二级缓存测试

默认情况下一级缓存只在同一个会话中有效:

用例:

    /**缓存测试*/
    @Test
    public void cacheTest(){
        //打开一个会话 1,不自动提交
        SqlSession session1 = SqlSessionFactoryUtils.openSqlSession(false);
        //获得一个映射器 1
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        //查询单个对象通过编号
        Student student1 = mapper1.selectStudentById(1);
        System.out.println(student1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开一个会话2,不自动提交</span>
    SqlSession session2 = SqlSessionFactoryUtils.openSqlSession(<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得一个映射器2</span>
    StudentMapper mapper2 = session2.getMapper(StudentMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询单个对象通过编号</span>
    Student student2 = mapper2.selectStudentById(1<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(student2);

    SqlSessionFactoryUtils.closeSession(session1);
    SqlSessionFactoryUtils.closeSession(session2);
}</span></pre>

结果:

如果需要将范围扩大到同一个 namespace 中有效可以使用二级缓存:

用例:

    /**缓存测试*/
    @Test
    public void cacheTest(){
        //打开一个会话 1,不自动提交
        SqlSession session1 = SqlSessionFactoryUtils.openSqlSession(false);
        //获得一个映射器 1
        StudentMapper mapper1 = session1.getMapper(StudentMapper.class);
        //查询单个对象通过编号
        Student student1 = mapper1.selectStudentById(1);
        System.out.println(student1);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">必须手动提交,否则无效</span>

session1.commit();

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开一个会话2,不自动提交</span>
    SqlSession session2 = SqlSessionFactoryUtils.openSqlSession(<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得一个映射器2</span>
    StudentMapper mapper2 = session2.getMapper(StudentMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询单个对象通过编号</span>
    Student student2 = mapper2.selectStudentById(1<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(student2);

    SqlSessionFactoryUtils.closeSession(session1);
    SqlSessionFactoryUtils.closeSession(session2);
}</span></pre>

结果:

 

如果不手动提交查询结果也不会缓存成功。

使用两个不同的 SqlSession 对象去执行相同查询条件的查询,第二次查询时不会再发送 SQL 语句,而是直接从缓存中取出数据

1.7、二级缓存小结

  1. 映射语句文件中的所有 select 语句将会被缓存。

  2. 映射语句文件中的所有 insert,update 和 delete 语句会刷新缓存。

  3. 缓存会使用 Least Recently Used(LRU,最近最少使用的)算法来收回。

  4. 缓存会根据指定的时间间隔来刷新。

  5. 缓存会存储 1024 个对象

cache 标签常用属性:

<cache 
eviction="FIFO"  <!-- 回收策略为先进先出 -->
flushInterval="60000" <!--自动刷新时间 60s-->
size="512" <!--最多缓存 512 个引用对象-->
readOnly="true"/> <!--只读-->

二、MyBatis-Generator 代码生成

2.1、在 Intellij IDEA 创建 maven 项目

这里创建了一个 Maven 项目,未使用骨架。

2.2、添加依赖

在 maven 项目的 pom.xml 添加 mybatis-generator-maven-plugin 插件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.zhangguo.mybatis06<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>MyBatis06<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.0-SNAPSHOT<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">MyBatis </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.mybatis<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mybatis<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>3.4.6<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">MySql数据库驱动 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql-connector-java<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>5.1.38<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> JUnit单元测试工具 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>junit<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>junit<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>4.11<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> mybatis-generator-core 反向生成java代码</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>

</dependencies>

<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">mybatis 代码生成插件</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>

<build>
<finalName>MyBatis06</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>

</project>

2.3、配置生成参数

在 maven 项目下的 src/main/resources 目录下建立名为 generatorConfig.xml 的配置文件,作为 mybatis-generator-maven-plugin 插件的执行目标:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">导入属性配置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">properties </span><span style="color: rgba(255, 0, 0, 1)">resource</span><span style="color: rgba(0, 0, 255, 1)">="db.properties"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">properties</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">指定特定数据库的jdbc驱动jar包的位置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">classPathEntry </span><span style="color: rgba(255, 0, 0, 1)">location</span><span style="color: rgba(0, 0, 255, 1)">="${mysql.driverLocation}"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="default"</span><span style="color: rgba(255, 0, 0, 1)"> targetRuntime</span><span style="color: rgba(0, 0, 255, 1)">="MyBatis3"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> optional,旨在创建class时,对注释进行控制 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">commentGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="suppressDate"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">commentGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">jdbc的数据库连接 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">jdbcConnection </span><span style="color: rgba(255, 0, 0, 1)">driverClass</span><span style="color: rgba(0, 0, 255, 1)">="${mysql.driver}"</span><span style="color: rgba(255, 0, 0, 1)"> connectionURL</span><span style="color: rgba(0, 0, 255, 1)">="${mysql.url}"</span><span style="color: rgba(255, 0, 0, 1)"> userId</span><span style="color: rgba(0, 0, 255, 1)">="${mysql.username}"</span><span style="color: rgba(255, 0, 0, 1)"> password</span><span style="color: rgba(0, 0, 255, 1)">="${mysql.password}"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">jdbcConnection</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 非必需,类型处理器,在数据库类型和java类型之间的转换控制</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaTypeResolver </span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="forceBigDecimals"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaTypeResolver</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
        targetPackage     指定生成的model生成所在的包名
        targetProject     指定在该项目下所在的路径
    </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaModelGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="com.zhangguo.mybatis06.entities"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="src/main/java"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 是否对model添加 构造函数 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="constructorBased"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 是否允许子包,即targetPackage.schemaName.tableName </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="immutable"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 给Model添加一个父类 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="rootClass"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="com.zhangguo.mybatis06.entities.BaseEntity"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 是否对类CHAR类型的列的数据进行trim操作 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="trimStrings"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaModelGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">sqlMapGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="com.zhangguo.mybatis06.mapper"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="src/main/resources"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">sqlMapGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>


    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
            type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
            type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
            type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
    </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaClientGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="com.zhangguo.mybatis06.dao"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="src/main/java"</span><span style="color: rgba(255, 0, 0, 1)"> type</span><span style="color: rgba(0, 0, 255, 1)">="MIXEDMAPPER"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">
                定义Maper.java 源代码中的ByExample() 方法的可视性,可选的值有:
                public;
                private;
                protected;
                default
                注意:如果 targetRuntime="MyBatis3",此参数被忽略
         </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="exampleMethodVisibility"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">
           方法名计数器
          Important note: this property is ignored if the target runtime is MyBatis3.
         </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="methodNameCalculator"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">
          为生成的接口添加父接口
         </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="rootInterface"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaClientGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>



    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">table </span><span style="color: rgba(255, 0, 0, 1)">tableName</span><span style="color: rgba(0, 0, 255, 1)">="student"</span><span style="color: rgba(255, 0, 0, 1)"> schema</span><span style="color: rgba(0, 0, 255, 1)">="nfmall"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">table</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">table </span><span style="color: rgba(255, 0, 0, 1)">tableName</span><span style="color: rgba(0, 0, 255, 1)">="category"</span><span style="color: rgba(255, 0, 0, 1)"> schema</span><span style="color: rgba(0, 0, 255, 1)">="nfmall"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">table</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">table </span><span style="color: rgba(255, 0, 0, 1)">tableName</span><span style="color: rgba(0, 0, 255, 1)">="goods"</span><span style="color: rgba(255, 0, 0, 1)"> schema</span><span style="color: rgba(0, 0, 255, 1)">="nfmall"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">table</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">context</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</generatorConfiguration>

这里的属性资源文件与 mybatis 共用 db.propities 文件

##MySQL 连接字符串
#驱动
mysql.driver=com.mysql.jdbc.Driver
#地址
mysql.url=jdbc:mysql://127.0.0.1:3306/nfmall?useUnicode=true&characterEncoding=UTF-8
#用户名
mysql.username=root
#密码
mysql.password=uchr@123
#驱动位置
mysql.driverLocation=E:\\NF\\Java\\JDBC\\mysql-connector-java-5.1.47\\mysql-connector-java-5.1.47.jar

参数配置文件一:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 本地数据库驱动程序jar包的全路径 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">classPathEntry </span><span style="color: rgba(255, 0, 0, 1)">location</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="context"</span><span style="color: rgba(255, 0, 0, 1)"> targetRuntime</span><span style="color: rgba(0, 0, 255, 1)">="MyBatis3"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">commentGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="suppressAllComments"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="suppressDate"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">commentGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 数据库的相关配置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">jdbcConnection </span><span style="color: rgba(255, 0, 0, 1)">driverClass</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> connectionURL</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> userId</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> password</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>

    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaTypeResolver</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="forceBigDecimals"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaTypeResolver</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 实体类生成的位置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaModelGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="目标包"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="目标项目classpath"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="trimStrings"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaModelGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> *Mapper.xml 文件的位置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">sqlMapGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="目标包"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="目标项目classpath"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">sqlMapGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> Mapper 接口文件的位置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaClientGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="目标包"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="目标项目classpath"</span><span style="color: rgba(255, 0, 0, 1)"> type</span><span style="color: rgba(0, 0, 255, 1)">="XMLMAPPER"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaClientGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 相关表的配置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">table </span><span style="color: rgba(255, 0, 0, 1)">tableName</span><span style="color: rgba(0, 0, 255, 1)">="表名"</span><span style="color: rgba(255, 0, 0, 1)"> enableCountByExample</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)"> enableDeleteByExample</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)"> enableSelectByExample</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(255, 0, 0, 1)">
           enableUpdateByExample</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">context</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</generatorConfiguration>

View Code

参考配置文件二:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE generatorConfiguration  
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
<generatorConfiguration>  
    <!--导入属性配置 -->  
    <properties resource="generator.properties"></properties>  
<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">指定特定数据库的jdbc驱动jar包的位置 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">classPathEntry </span><span style="color: rgba(255, 0, 0, 1)">location</span><span style="color: rgba(0, 0, 255, 1)">="${jdbc.driverLocation}"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="default"</span><span style="color: rgba(255, 0, 0, 1)"> targetRuntime</span><span style="color: rgba(0, 0, 255, 1)">="MyBatis3"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  


    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> optional,旨在创建class时,对注释进行控制 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">commentGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="suppressDate"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">commentGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  


    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">jdbc的数据库连接 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">jdbcConnection </span><span style="color: rgba(255, 0, 0, 1)">driverClass</span><span style="color: rgba(0, 0, 255, 1)">="${jdbc.driverClass}"</span><span style="color: rgba(255, 0, 0, 1)"> connectionURL</span><span style="color: rgba(0, 0, 255, 1)">="${jdbc.connectionURL}"</span><span style="color: rgba(255, 0, 0, 1)"> userId</span><span style="color: rgba(0, 0, 255, 1)">="${jdbc.userId}"</span><span style="color: rgba(255, 0, 0, 1)"> password</span><span style="color: rgba(0, 0, 255, 1)">="${jdbc.password}"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">jdbcConnection</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  



    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 非必需,类型处理器,在数据库类型和java类型之间的转换控制</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaTypeResolver </span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="forceBigDecimals"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaTypeResolver</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类  
        targetPackage     指定生成的model生成所在的包名  
        targetProject     指定在该项目下所在的路径  
    </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaModelGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="org.louis.hometutor.po"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="src/main/java"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 是否对model添加 构造函数 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="constructorBased"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 是否允许子包,即targetPackage.schemaName.tableName </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="immutable"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 给Model添加一个父类 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="rootClass"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="com.foo.louis.Hello"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 是否对类CHAR类型的列的数据进行trim操作 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="trimStrings"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="true"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaModelGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  

    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">sqlMapGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="org.louis.hometutor.domain"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="src/main/java"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">="false"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">sqlMapGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  


    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码  
            type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象  
            type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象  
            type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口  
    </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">javaClientGenerator </span><span style="color: rgba(255, 0, 0, 1)">targetPackage</span><span style="color: rgba(0, 0, 255, 1)">="com.foo.tourist.dao"</span><span style="color: rgba(255, 0, 0, 1)"> targetProject</span><span style="color: rgba(0, 0, 255, 1)">="src/main/java"</span><span style="color: rgba(255, 0, 0, 1)"> type</span><span style="color: rgba(0, 0, 255, 1)">="MIXEDMAPPER"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="enableSubPackages"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">  
                定义Maper.java 源代码中的ByExample() 方法的可视性,可选的值有:  
                public;  
                private;  
                protected;  
                default  
                注意:如果 targetRuntime="MyBatis3",此参数被忽略  
         </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="exampleMethodVisibility"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  
        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">  
                                       方法名计数器  
          Important note: this property is ignored if the target runtime is MyBatis3.  
         </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="methodNameCalculator"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> 
                                            为生成的接口添加父接口 
         </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">property </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="rootInterface"</span><span style="color: rgba(255, 0, 0, 1)"> value</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">javaClientGenerator</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  



    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">table </span><span style="color: rgba(255, 0, 0, 1)">tableName</span><span style="color: rgba(0, 0, 255, 1)">="lession"</span><span style="color: rgba(255, 0, 0, 1)"> schema</span><span style="color: rgba(0, 0, 255, 1)">="louis"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  

        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> optional   , only for mybatis3 runtime  
             自动生成的键值(identity,或者序列值)  
           如果指定此元素,MBG将会生成&lt;selectKey&gt;元素,然后将此元素插入到SQL Map的&lt;insert&gt; 元素之中  
           sqlStatement 的语句将会返回新的值  
           如果是一个自增主键的话,你可以使用预定义的语句,或者添加自定义的SQL语句. 预定义的值如下:  
              Cloudscape    This will translate to: VALUES IDENTITY_VAL_LOCAL()  
              DB2:      VALUES IDENTITY_VAL_LOCAL()  
              DB2_MF:       SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1  
              Derby:        VALUES IDENTITY_VAL_LOCAL()  
              HSQLDB:   CALL IDENTITY()  
              Informix:     select dbinfo('sqlca.sqlerrd1') from systables where tabid=1  
              MySql:        SELECT LAST_INSERT_ID()  
              SqlServer:    SELECT SCOPE_IDENTITY()  
              SYBASE:   SELECT @@IDENTITY  
              JDBC:     This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. 

This is a database independent method of obtaining the value from identity columns.
identity: 自增主键 If true, then the column is flagged as an identity column and the generated <selectKey>
element will be placed after the insert (for an identity column). If false, then the generated <selectKey> will be placed before
the insert (typically for a sequence).

        </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">generatedKey </span><span style="color: rgba(255, 0, 0, 1)">column</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> sqlStatement</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> identity</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> type</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  


        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> optional.  
                列的命名规则:  
                MBG使用 &lt;columnRenamingRule&gt; 元素在计算列名的对应 名称之前,先对列名进行重命名,  
                作用:一般需要对BUSI_CLIENT_NO 前的BUSI_进行过滤  
                支持正在表达式  
                 searchString 表示要被换掉的字符串  
                 replaceString 则是要换成的字符串,默认情况下为空字符串,可选  
        </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">columnRenamingRule </span><span style="color: rgba(255, 0, 0, 1)">searchString</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> replaceString</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  



        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> optional.告诉 MBG 忽略某一列  
                column,需要忽略的列  
                delimitedColumnName:true ,匹配column的值和数据库列的名称 大小写完全匹配,false 忽略大小写匹配  
                是否限定表的列名,即固定表列在Model中的名称  
        </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">ignoreColumn </span><span style="color: rgba(255, 0, 0, 1)">column</span><span style="color: rgba(0, 0, 255, 1)">="PLAN_ID"</span><span style="color: rgba(255, 0, 0, 1)">  delimitedColumnName</span><span style="color: rgba(0, 0, 255, 1)">="true"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  


        <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">optional.覆盖MBG对Model 的生成规则  
             column: 数据库的列名  
             javaType: 对应的Java数据类型的完全限定名  
             在必要的时候可以覆盖由JavaTypeResolver计算得到的java数据类型. For some databases, this is necessary to handle "odd" 

database types (e.g. MySql's unsigned bigint type should be mapped to java.lang.Object).
jdbcType: 该列的 JDBC 数据类型 (INTEGER, DECIMAL, NUMERIC, VARCHAR, etc.),该列可以覆盖由 JavaTypeResolver 计算得到的 Jdbc 类型,
对某些数据库而言,对于处理特定的 JDBC 驱动癖好 很有必要 (e.g. DB2's LONGVARCHAR type should be mapped to VARCHAR for iBATIS).
typeHandler:

        </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>  
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">columnOverride </span><span style="color: rgba(255, 0, 0, 1)">column</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> javaType</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)">    jdbcType</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)"> typeHandler</span><span style="color: rgba(0, 0, 255, 1)">=""</span><span style="color: rgba(255, 0, 0, 1)">  delimitedColumnName</span><span style="color: rgba(0, 0, 255, 1)">=""</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>  

    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">table</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">context</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>  

</generatorConfiguration>

View Code

属性资源文件:

jdbc.driverLocation=E:\\NF\\Java\\JDBC\\mysql-connector-java-5.1.47\\mysql-connector-java-5.1.47.jar
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/nfmall?useUnicode=true&characterEncoding=UTF-8
jdbc.userId=root
jdbc.password=uchr@123
View Code

这里使用了外置的配置文件 generator.properties,可以将一下属性配置到 properties 文件之中,增加配置的灵活性:

项目目录如下:

2.4、执行生成

在 Maven Projects 中找到 Plugins->mybatis-generator->mybatis-generator:generate

点击运行,然后不出意外的话,会在控制台输出:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MyBatis06 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ MyBatis06 ---
[INFO] Connecting to the Database
[INFO] Introspecting table nfmall.student
log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Introspecting table nfmall.category
[INFO] Introspecting table nfmall.goods
[INFO] Generating Example class for table student
[INFO] Generating Record class for table student
[INFO] Generating Mapper Interface for table student
[INFO] Generating SQL Map for table student
[INFO] Generating Example class for table category
[INFO] Generating Record class for table category
[INFO] Generating Mapper Interface for table category
[INFO] Generating SQL Map for table category
[INFO] Generating Example class for table goods
[INFO] Generating Record class for table goods
[INFO] Generating Mapper Interface for table goods
[INFO] Generating SQL Map for table goods
[INFO] Saving file StudentMapper.xml
[INFO] Saving file CategoryMapper.xml
[INFO] Saving file GoodsMapper.xml
[INFO] Saving file StudentExample.java
[INFO] Saving file Student.java
[INFO] Saving file StudentMapper.java
[INFO] Saving file CategoryExample.java
[INFO] Saving file Category.java
[INFO] Saving file CategoryMapper.java
[INFO] Saving file GoodsExample.java
[INFO] Saving file Goods.java
[INFO] Saving file GoodsMapper.java
[WARNING] Root class com.zhangguo.mybatis06.entities.BaseEntity cannot be loaded, checking for member overrides is disabled for this class 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.845 s
[INFO] Finished at: 2018-10-10T09:51:45+08:00
[INFO] Final Memory: 11M/162M
[INFO] ------------------------------------------------------------------------
View Code

看到 BUILD SUCCESS,则大功告成,如果有错误的话,由于添加了 -e 选项,会把具体的详细错误信息打印出来的,根据错误信息修改即可。

生成结果:

2.5、使用生成的代码

1、MyBatis 会话工具类

package com.zhangguo.mybatis06.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**

  • MyBatis 会话工具类

  • */
    public class SqlSessionFactoryUtils {

    /**会话工厂*/
    private static SqlSessionFactory factory;

    static {
    try {
    /获得配置文件的文件流/
    InputStream inputStream
    =Resources.getResourceAsStream("mybatisCfg.xml");
    //初始化工厂
    factory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**

    • 获得会话对象
    • 指定是否自动提交
    • */
      public static SqlSession openSqlSession(boolean isAutoCommit){
      return getFactory().openSession(isAutoCommit);
      }

    public static SqlSessionFactory getFactory() {
    return factory;
    }
    public static void setFactory(SqlSessionFactory factory) {
    SqlSessionFactoryUtils.factory
    = factory;
    }

    /**

    • 关闭会话
    • */
      public static void closeSession(SqlSession session){
      if(session!=null){
      session.close();
      }
      }
      }
View Code

2、MyBatis 核心配置文件

<?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>
    <!-- 导入 db.properties 文件中的所有 key-value 数据 -->
    <!-- 外部引入的内容将覆盖内部定义的 -->
    <properties resource="db.properties">
        <!-- 定义一个名称为 driver,值为 com.mysql.jdbc.Driver 的属性 -->
        <property name="mysql.driver" value="com.mysql.jdbc.Driver"></property>
    </properties>
&lt;settings&gt;
    &lt;!--设置是否允许缓存--&gt;
    &lt;setting name="cacheEnabled" value="true"/&gt;
    &lt;!--设置日志输出的目标--&gt;
    &lt;setting name="logImpl" value="STDOUT_LOGGING"/&gt;
&lt;/settings&gt;

&lt;!--别名--&gt;
&lt;typeAliases&gt;
    &lt;!--定义单个别名,指定名称为student,对应的类型为com.zhangguo.mybatis02.entities.Student--&gt;
    &lt;!--&lt;typeAlias type="com.zhangguo.mybatis02.entities.Student" alias="student"&gt;&lt;/typeAlias&gt;--&gt;
    &lt;!--<span style="color: rgba(0, 0, 0, 1)">指定包名下所有的类被自动扫描并定义默认别名,
    mybatis会自动扫描包中的pojo类,自动定义别名,别名就是类名(首字母大写或小写都可以)</span>--&gt;
    &lt;<span style="color: rgba(0, 0, 255, 1)">package</span> name="com.zhangguo.mybatis03.entities"&gt;&lt;/<span style="color: rgba(0, 0, 255, 1)">package</span>&gt;
&lt;/typeAliases&gt;

&lt;!--注册自定义的类型处理器--&gt;
&lt;typeHandlers&gt;
    &lt;!--&lt;typeHandler handler="" javaType="" jdbcType=""&gt;&lt;/typeHandler&gt;--&gt;
&lt;/typeHandlers&gt;

&lt;!--环境配置,default为默认选择的环境--&gt;
&lt;environments <span style="color: rgba(0, 0, 255, 1)">default</span>="development"&gt;
    &lt;!--开发--&gt;
    &lt;environment id="development"&gt;
        &lt;!--事务管理--&gt;
        &lt;transactionManager type="JDBC"/&gt;
        &lt;!--连接池--&gt;
        &lt;dataSource type="POOLED"&gt;
            &lt;!--引用属性${mysql.driver}--&gt;
            &lt;property name="driver" value="${mysql.driver}"/&gt;
            &lt;property name="url" value="${mysql.url}"/&gt;
            &lt;property name="username" value="${mysql.username}"/&gt;
            &lt;property name="password" value="${mysql.password}"/&gt;
        &lt;/dataSource&gt;
    &lt;/environment&gt;
    &lt;!--运行--&gt;
    &lt;environment id="work"&gt;
        &lt;transactionManager type="JDBC"/&gt;
        &lt;dataSource type="POOLED"&gt;
            &lt;property name="driver" value="${driver}"/&gt;
            &lt;property name="url" value="jdbc:mysql://127.0.0.1:3306/nfmall?useUnicode=true&amp;amp;characterEncoding=UTF-8"/&gt;
            &lt;property name="username" value="root"/&gt;
            &lt;property name="password" value="uchr@123"/&gt;
        &lt;/dataSource&gt;
    &lt;/environment&gt;
&lt;/environments&gt;

&lt;mappers&gt;
    &lt;!--根据路径注册一个基于XML的映射器--&gt;
    &lt;mapper resource="com/zhangguo/mybatis06/mapper/studentMapper.xml"/&gt;

&lt;/mappers&gt;

</configuration>

View Code

3、测试用例

package test;

import com.zhangguo.mybatis06.dao.StudentMapper;
import com.zhangguo.mybatis06.entities.Student;
import com.zhangguo.mybatis06.entities.StudentExample;
import com.zhangguo.mybatis06.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class StudentTest {
/***
* List<Student> selectByExample(StudentExample example);
*/
@Test
public void testSelectByExample(){

    List</span>&lt;Student&gt; entities = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开一个会话</span>
    SqlSession session = SqlSessionFactoryUtils.openSqlSession(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得一个映射器</span>
    StudentMapper mapper = session.getMapper(StudentMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);

    StudentExample studentExample</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StudentExample();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询名字中含a</span>
    studentExample.createCriteria().andNameLike("%a%"<span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询多个对象,指定参数</span>
    entities =<span style="color: rgba(0, 0, 0, 1)"> mapper.selectByExample(studentExample);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭</span>

SqlSessionFactoryUtils.closeSession(session);

}

}

测试结果:

三、MyBatis-GUI 代码生成器 mybatis-generator-gui

3.1、概要

源码地址:https://github.com/zouzg/mybatis-generator-gui

mybatis-generator-gui 是基于mybatis generator开发一款界面工具, 本工具可以使你非常容易及快速生成 Mybatis 的 Java POJO 文件及数据库 Mapping 文件。

3.2、核心特性

  • 按照界面步骤轻松生成代码,省去 XML 繁琐的学习与配置过程
  • 保存数据库连接与 Generator 配置,每次代码生成轻松搞定
  • 内置常用插件,比如分页插件
  • 把数据库中表列的注释生成为 Java 实体的注释,生成的实体清晰明了
  • 可选的去除掉对版本管理不友好的注释,这样新增或删除字段重新生成的文件比较过来清楚
  • 目前已经支持 Mysql、Mysql8、Oracle、PostgreSQL 与 SQL Server,暂不对其他非主流数据库提供支持。

3.3、要求

本工具由于使用了 Java 8 的众多特性,所以要求 JDK 1.8.0.60 以上版本,另外 JDK 1.9 暂时还不支持。

3.4、下载

你可以从本链接下载本工具: https://github.com/astarring/mybatis-generator-gui/releases

3.5、启动本软件

  • 方法一: 自助构建(注意项目名称需要根据实例情况修改
    git clone https://github.com/astarring/mybatis-generator-gui
    cd mybatis-generator-gui
    mvn jfx:jar
    cd target/jfx/app/
    java -jar mybatis-generator-gui.jar

 

  • 方法二: IDE 中运行

Eclipse or IntelliJ IDEA 中启动, 找到com.zzg.mybatis.generator.MainUI类并运行就可以了

  • 方法三:打包为本地原生应用,双击快捷方式即可启动,方便快捷

    如果不想打包后的安装包 logo 为 Java 的灰色的茶杯,需要在 pom 文件里将对应操作系统平台的图标注释放开

    #<icon>${project.basedir}/package/windows/mybatis-generator-gui.ico</icon>为 windows
    #<icon>${project.basedir}/package/macosx/mybatis-generator-gui.icns</icon>为 mac
    mvn jfx:native

 

​ 另外需要注意,windows 系统打包成 exe 的话需要安装 WiXToolset3+ 的环境;由于打包后会把 jre 打入安装包,两个平台均 100M 左右,体积较大请自行打包;打包后的安装包在 target/jfx/native 目录下

3.6、注意事项

  • 本自动生成代码工具只适合生成单表的增删改查,对于需要做数据库联合查询的,请自行写新的 XML 与 Mapper;
  • 部分系统在中文输入方法时输入框中无法输入文字,请切换成英文输入法;
  • 如果不明白对应字段或选项是什么意思的时候,把光标放在对应字段或 Label 上停留一会然后如果有解释会出现解释;

3.7、文档

更多详细文档请参考本库的 Wiki

3.8、代码生成示例

3.8.1、创建一个 Maven 项目

3.8.2、下载源代码并使用 IDEA 打开

下载地址:https://github.com/zouzg/mybatis-generator-gui/releases

解压后引入到 IDEA 中

找到 MainUI 类

3.8.3、运行程序

3.8.4、连接到数据

 

3.8.5、生成代码

3.8.6、使用生成的代码

POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>com.zhangguo.mybatis05<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>MyBatis05<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1.0-SNAPSHOT<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">MyBatis </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>org.mybatis<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mybatis<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>3.4.6<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">MySql数据库驱动 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>mysql-connector-java<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>5.1.38<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> JUnit单元测试工具 </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>junit<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">groupId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>junit<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">artifactId</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>4.11<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">version</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
        <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">scope</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependency</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">dependencies</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</project>

工具类:

package com.zhangguo.mybatis05.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**

  • MyBatis 会话工具类

  • */
    public class SqlSessionFactoryUtils {

    /**会话工厂*/
    private static SqlSessionFactory factory;

    static {
    try {
    /获得配置文件的文件流/
    InputStream inputStream
    =Resources.getResourceAsStream("mybatisCfg.xml");
    //初始化工厂
    factory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**

    • 获得会话对象
    • 指定是否自动提交
    • */
      public static SqlSession openSqlSession(boolean isAutoCommit){
      return getFactory().openSession(isAutoCommit);
      }

    public static SqlSessionFactory getFactory() {
    return factory;
    }
    public static void setFactory(SqlSessionFactory factory) {
    SqlSessionFactoryUtils.factory
    = factory;
    }

    /**

    • 关闭会话
    • */
      public static void closeSession(SqlSession session){
      if(session!=null){
      session.close();
      }
      }
      }

核心配置文件:

<?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>
    <!-- 导入 db.properties 文件中的所有 key-value 数据 -->
    <!-- 外部引入的内容将覆盖内部定义的 -->
    <properties resource="db.properties">
        <!-- 定义一个名称为 driver,值为 com.mysql.jdbc.Driver 的属性 -->
        <property name="mysql.driver" value="com.mysql.jdbc.Driver"></property>
    </properties>
&lt;settings&gt;
    &lt;!--设置是否允许缓存--&gt;
    &lt;setting name="cacheEnabled" value="true"/&gt;
    &lt;!--设置日志输出的目标--&gt;
    &lt;setting name="logImpl" value="STDOUT_LOGGING"/&gt;
&lt;/settings&gt;

&lt;!--别名--&gt;
&lt;typeAliases&gt;
    &lt;!--定义单个别名,指定名称为student,对应的类型为com.zhangguo.mybatis02.entities.Student--&gt;
    &lt;!--&lt;typeAlias type="com.zhangguo.mybatis02.entities.Student" alias="student"&gt;&lt;/typeAlias&gt;--&gt;
    &lt;!--<span style="color: rgba(0, 0, 0, 1)">指定包名下所有的类被自动扫描并定义默认别名,
    mybatis会自动扫描包中的pojo类,自动定义别名,别名就是类名(首字母大写或小写都可以)</span>--&gt;
    &lt;<span style="color: rgba(0, 0, 255, 1)">package</span> name="com.zhangguo.mybatis03.entities"&gt;&lt;/<span style="color: rgba(0, 0, 255, 1)">package</span>&gt;
&lt;/typeAliases&gt;

&lt;!--注册自定义的类型处理器--&gt;
&lt;typeHandlers&gt;
    &lt;!--&lt;typeHandler handler="" javaType="" jdbcType=""&gt;&lt;/typeHandler&gt;--&gt;
&lt;/typeHandlers&gt;

&lt;!--环境配置,default为默认选择的环境--&gt;
&lt;environments <span style="color: rgba(0, 0, 255, 1)">default</span>="development"&gt;
    &lt;!--开发--&gt;
    &lt;environment id="development"&gt;
        &lt;!--事务管理--&gt;
        &lt;transactionManager type="JDBC"/&gt;
        &lt;!--连接池--&gt;
        &lt;dataSource type="POOLED"&gt;
            &lt;!--引用属性${mysql.driver}--&gt;
            &lt;property name="driver" value="${mysql.driver}"/&gt;
            &lt;property name="url" value="${mysql.url}"/&gt;
            &lt;property name="username" value="${mysql.username}"/&gt;
            &lt;property name="password" value="${mysql.password}"/&gt;
        &lt;/dataSource&gt;
    &lt;/environment&gt;
    &lt;!--运行--&gt;
    &lt;environment id="work"&gt;
        &lt;transactionManager type="JDBC"/&gt;
        &lt;dataSource type="POOLED"&gt;
            &lt;property name="driver" value="${driver}"/&gt;
            &lt;property name="url" value="jdbc:mysql://127.0.0.1:3306/nfmall?useUnicode=true&amp;amp;characterEncoding=UTF-8"/&gt;
            &lt;property name="username" value="root"/&gt;
            &lt;property name="password" value="uchr@123"/&gt;
        &lt;/dataSource&gt;
    &lt;/environment&gt;
&lt;/environments&gt;

&lt;mappers&gt;
    &lt;!--根据路径注册一个基于XML的映射器--&gt;
    &lt;mapper resource="com/zhangguo/mybatis05/StudentMapper.xml"/&gt;
&lt;/mappers&gt;

</configuration>

View Code

POJO:

package com.zhangguo.mybatis05.entities;

import java.io.Serializable;

/**

  • @author
    */
    public class Student implements Serializable {
    private Integer id;

    private String name;

    private String sex;

    private static final long serialVersionUID = 1L;

    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }

    @Override
    public boolean equals(Object that) {
    if (this == that) {
    return true;
    }
    if (that == null) {
    return false;
    }
    if (getClass() != that.getClass()) {
    return false;
    }
    Student other
    = (Student) that;
    return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
    && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
    && (this.getSex() == null ? other.getSex() == null : this.getSex().equals(other.getSex()));
    }

    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result
    = prime * result + ((getId() == null) ? 0 : getId().hashCode());
    result
    = prime * result + ((getName() == null) ? 0 : getName().hashCode());
    result
    = prime * result + ((getSex() == null) ? 0 : getSex().hashCode());
    return result;
    }

    @Override
    public String toString() {
    StringBuilder sb
    = new StringBuilder();
    sb.append(getClass().getSimpleName());
    sb.append(
    "[");
    sb.append(
    "Hash =").append(hashCode());
    sb.append(
    ", id=").append(id);
    sb.append(
    ", name=").append(name);
    sb.append(
    ", sex=").append(sex);
    sb.append(
    ", serialVersionUID=").append(serialVersionUID);
    sb.append(
    "]");
    return sb.toString();
    }
    }

View Code

参数:

package com.zhangguo.mybatis05.entities;

import java.util.ArrayList;
import java.util.List;

public class StudentExample {
protected String orderByClause;

</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> distinct;

</span><span style="color: rgba(0, 0, 255, 1)">protected</span> List&lt;Criteria&gt;<span style="color: rgba(0, 0, 0, 1)"> oredCriteria;

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer limit;

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer offset;

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> StudentExample() {
    oredCriteria </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Criteria&gt;<span style="color: rgba(0, 0, 0, 1)">();
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setOrderByClause(String orderByClause) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.orderByClause =<span style="color: rgba(0, 0, 0, 1)"> orderByClause;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getOrderByClause() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> orderByClause;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setDistinct(<span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> distinct) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.distinct =<span style="color: rgba(0, 0, 0, 1)"> distinct;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isDistinct() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> distinct;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Criteria&gt;<span style="color: rgba(0, 0, 0, 1)"> getOredCriteria() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> oredCriteria;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> or(Criteria criteria) {
    oredCriteria.add(criteria);
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria or() {
    Criteria criteria </span>=<span style="color: rgba(0, 0, 0, 1)"> createCriteriaInternal();
    oredCriteria.add(criteria);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> criteria;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria createCriteria() {
    Criteria criteria </span>=<span style="color: rgba(0, 0, 0, 1)"> createCriteriaInternal();
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (oredCriteria.size() == 0<span style="color: rgba(0, 0, 0, 1)">) {
        oredCriteria.add(criteria);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> criteria;
}

</span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Criteria createCriteriaInternal() {
    Criteria criteria </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Criteria();
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> criteria;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> clear() {
    oredCriteria.clear();
    orderByClause </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    distinct </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setLimit(Integer limit) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.limit =<span style="color: rgba(0, 0, 0, 1)"> limit;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getLimit() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> limit;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setOffset(Integer offset) {
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.offset =<span style="color: rgba(0, 0, 0, 1)"> offset;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getOffset() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> offset;
}

</span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">abstract</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> GeneratedCriteria {
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> List&lt;Criterion&gt;<span style="color: rgba(0, 0, 0, 1)"> criteria;

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> GeneratedCriteria() {
        </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        criteria </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;Criterion&gt;<span style="color: rgba(0, 0, 0, 1)">();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isValid() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> criteria.size() &gt; 0<span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Criterion&gt;<span style="color: rgba(0, 0, 0, 1)"> getAllCriteria() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> criteria;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Criterion&gt;<span style="color: rgba(0, 0, 0, 1)"> getCriteria() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> criteria;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addCriterion(String condition) {
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (condition == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> RuntimeException("Value for condition cannot be null"<span style="color: rgba(0, 0, 0, 1)">);
        }
        criteria.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(condition));
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addCriterion(String condition, Object value, String property) {
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (value == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> RuntimeException("Value for " + property + " cannot be null"<span style="color: rgba(0, 0, 0, 1)">);
        }
        criteria.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(condition, value));
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> addCriterion(String condition, Object value1, Object value2, String property) {
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (value1 == <span style="color: rgba(0, 0, 255, 1)">null</span> || value2 == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> RuntimeException("Between values for " + property + " cannot be null"<span style="color: rgba(0, 0, 0, 1)">);
        }
        criteria.add(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(condition, value1, value2));
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdIsNull() {
        addCriterion(</span>"id is null"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdIsNotNull() {
        addCriterion(</span>"id is not null"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdEqualTo(Integer value) {
        addCriterion(</span>"id =", value, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdNotEqualTo(Integer value) {
        addCriterion(</span>"id &lt;&gt;", value, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdGreaterThan(Integer value) {
        addCriterion(</span>"id &gt;", value, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdGreaterThanOrEqualTo(Integer value) {
        addCriterion(</span>"id &gt;=", value, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdLessThan(Integer value) {
        addCriterion(</span>"id &lt;", value, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdLessThanOrEqualTo(Integer value) {
        addCriterion(</span>"id &lt;=", value, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Criteria andIdIn(List&lt;Integer&gt;<span style="color: rgba(0, 0, 0, 1)"> values) {
        addCriterion(</span>"id in", values, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Criteria andIdNotIn(List&lt;Integer&gt;<span style="color: rgba(0, 0, 0, 1)"> values) {
        addCriterion(</span>"id not in", values, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdBetween(Integer value1, Integer value2) {
        addCriterion(</span>"id between", value1, value2, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andIdNotBetween(Integer value1, Integer value2) {
        addCriterion(</span>"id not between", value1, value2, "id"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameIsNull() {
        addCriterion(</span>"name is null"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameIsNotNull() {
        addCriterion(</span>"name is not null"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameEqualTo(String value) {
        addCriterion(</span>"name =", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameNotEqualTo(String value) {
        addCriterion(</span>"name &lt;&gt;", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameGreaterThan(String value) {
        addCriterion(</span>"name &gt;", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameGreaterThanOrEqualTo(String value) {
        addCriterion(</span>"name &gt;=", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameLessThan(String value) {
        addCriterion(</span>"name &lt;", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameLessThanOrEqualTo(String value) {
        addCriterion(</span>"name &lt;=", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameLike(String value) {
        addCriterion(</span>"name like", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameNotLike(String value) {
        addCriterion(</span>"name not like", value, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Criteria andNameIn(List&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)"> values) {
        addCriterion(</span>"name in", values, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Criteria andNameNotIn(List&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)"> values) {
        addCriterion(</span>"name not in", values, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameBetween(String value1, String value2) {
        addCriterion(</span>"name between", value1, value2, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andNameNotBetween(String value1, String value2) {
        addCriterion(</span>"name not between", value1, value2, "name"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexIsNull() {
        addCriterion(</span>"sex is null"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexIsNotNull() {
        addCriterion(</span>"sex is not null"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexEqualTo(String value) {
        addCriterion(</span>"sex =", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexNotEqualTo(String value) {
        addCriterion(</span>"sex &lt;&gt;", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexGreaterThan(String value) {
        addCriterion(</span>"sex &gt;", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexGreaterThanOrEqualTo(String value) {
        addCriterion(</span>"sex &gt;=", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexLessThan(String value) {
        addCriterion(</span>"sex &lt;", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexLessThanOrEqualTo(String value) {
        addCriterion(</span>"sex &lt;=", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexLike(String value) {
        addCriterion(</span>"sex like", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexNotLike(String value) {
        addCriterion(</span>"sex not like", value, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Criteria andSexIn(List&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)"> values) {
        addCriterion(</span>"sex in", values, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Criteria andSexNotIn(List&lt;String&gt;<span style="color: rgba(0, 0, 0, 1)"> values) {
        addCriterion(</span>"sex not in", values, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexBetween(String value1, String value2) {
        addCriterion(</span>"sex between", value1, value2, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Criteria andSexNotBetween(String value1, String value2) {
        addCriterion(</span>"sex not between", value1, value2, "sex"<span style="color: rgba(0, 0, 0, 1)">);
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> (Criteria) <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span>
 <span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span> Criteria <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> GeneratedCriteria {

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Criteria() {
        </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
    }
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Criterion {
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String condition;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Object value;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Object secondValue;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> noValue;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> singleValue;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> betweenValue;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> listValue;

    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String typeHandler;

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getCondition() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> condition;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object getValue() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> value;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object getSecondValue() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> secondValue;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isNoValue() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> noValue;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isSingleValue() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> singleValue;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isBetweenValue() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> betweenValue;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">boolean</span><span style="color: rgba(0, 0, 0, 1)"> isListValue() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> listValue;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getTypeHandler() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> typeHandler;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(String condition) {
        </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.condition =<span style="color: rgba(0, 0, 0, 1)"> condition;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.typeHandler = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.noValue = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(String condition, Object value, String typeHandler) {
        </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.condition =<span style="color: rgba(0, 0, 0, 1)"> condition;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.value =<span style="color: rgba(0, 0, 0, 1)"> value;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.typeHandler =<span style="color: rgba(0, 0, 0, 1)"> typeHandler;
        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (value <span style="color: rgba(0, 0, 255, 1)">instanceof</span> List&lt;?&gt;<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.listValue = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
        } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.singleValue = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
        }
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(String condition, Object value) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>(condition, value, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(String condition, Object value, Object secondValue, String typeHandler) {
        </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.condition =<span style="color: rgba(0, 0, 0, 1)"> condition;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.value =<span style="color: rgba(0, 0, 0, 1)"> value;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.secondValue =<span style="color: rgba(0, 0, 0, 1)"> secondValue;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.typeHandler =<span style="color: rgba(0, 0, 0, 1)"> typeHandler;
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.betweenValue = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    }

    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"> Criterion(String condition, Object value, Object secondValue) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>(condition, value, secondValue, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
    }
}

}

View Code

映射器:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhangguo.mybatis05.mapper.StudentMapper">
  <resultMap id="BaseResultMap" type="com.zhangguo.mybatis05.entities.Student">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="sex" jdbcType="CHAR" property="sex" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, name, sex
  </sql>
  <select id="selectByExample" parameterType="com.zhangguo.mybatis05.entities.StudentExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from student
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="limit != null">
      <if test="offset != null">
        limit ${offset}, ${limit}
      </if>
      <if test="offset == null">
        limit ${limit}
      </if>
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from student
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from student
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.zhangguo.mybatis05.entities.StudentExample">
    delete from student
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.zhangguo.mybatis05.entities.Student">
    insert into student (id, name, sex)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=CHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zhangguo.mybatis05.entities.Student">
    insert into student
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="sex != null">
        sex,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        #{sex,jdbcType=CHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.zhangguo.mybatis05.entities.StudentExample" resultType="java.lang.Long">
    select count(*) from student
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update student
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.name != null">
        name = #{record.name,jdbcType=VARCHAR},
      </if>
      <if test="record.sex != null">
        sex = #{record.sex,jdbcType=CHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update student
    set id = #{record.id,jdbcType=INTEGER},
      name = #{record.name,jdbcType=VARCHAR},
      sex = #{record.sex,jdbcType=CHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.zhangguo.mybatis05.entities.Student">
    update student
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        sex = #{sex,jdbcType=CHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zhangguo.mybatis05.entities.Student">
    update student
    set name = #{name,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=CHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>
View Code

接口:

package com.zhangguo.mybatis05.mapper;

import com.zhangguo.mybatis05.entities.Student;
import com.zhangguo.mybatis05.entities.StudentExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface StudentMapper {
long countByExample(StudentExample example);

</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> deleteByExample(StudentExample example);

</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> deleteByPrimaryKey(Integer id);

</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> insert(Student record);

</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> insertSelective(Student record);

List</span>&lt;Student&gt;<span style="color: rgba(0, 0, 0, 1)"> selectByExample(StudentExample example);

Student selectByPrimaryKey(Integer id);

</span><span style="color: rgba(0, 0, 255, 1)">int</span> updateByExampleSelective(@Param("record") Student record, @Param("example"<span style="color: rgba(0, 0, 0, 1)">) StudentExample example);

</span><span style="color: rgba(0, 0, 255, 1)">int</span> updateByExample(@Param("record") Student record, @Param("example"<span style="color: rgba(0, 0, 0, 1)">) StudentExample example);

</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> updateByPrimaryKeySelective(Student record);

</span><span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> updateByPrimaryKey(Student record);

}

View Code

数据访问类:

package com.zhangguo.mybatis05.dao;

import com.zhangguo.mybatis05.entities.Student;
import com.zhangguo.mybatis05.entities.StudentExample;
import com.zhangguo.mybatis05.utils.SqlSessionFactoryUtils;
import com.zhangguo.mybatis05.mapper.StudentMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class StudentDao implements StudentMapper {

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> countByExample(StudentExample example) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> deleteByExample(StudentExample example) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> deleteByPrimaryKey(Integer id) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> insert(Student record) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> insertSelective(Student record) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Student&gt;<span style="color: rgba(0, 0, 0, 1)"> selectByExample(StudentExample example) {
    List</span>&lt;Student&gt;  entities = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打开一个会话</span>
    SqlSession session = SqlSessionFactoryUtils.openSqlSession(<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得一个映射器</span>
    StudentMapper mapper = session.getMapper(StudentMapper.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询多个对象</span>
    entities =<span style="color: rgba(0, 0, 0, 1)"> mapper.selectByExample(example);

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关闭</span>

SqlSessionFactoryUtils.closeSession(session);

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> entities;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Student selectByPrimaryKey(Integer id) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> updateByExampleSelective(Student record, StudentExample example) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> updateByExample(Student record, StudentExample example) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> updateByPrimaryKeySelective(Student record) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> updateByPrimaryKey(Student record) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> 0<span style="color: rgba(0, 0, 0, 1)">;
}

}

View Code

单元测试:

package com.zhangguo.mybatis05.dao;

import com.zhangguo.mybatis05.entities.Student;
import com.zhangguo.mybatis05.entities.StudentExample;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import java.util.List;

/**

  • StudentDao Tester.

  • @author <Authors name>

  • @version 1.0

  • @since <pre>10/09/2018</pre>
    */
    public class StudentDaoTest {

    StudentDao dao;

    @Before
    public void before() throws Exception {
    dao
    = new StudentDao();
    }

    @After
    public void after() throws Exception {
    }

    /**

    • Method: countByExample(StudentExample example)
      */
      @Test
      public void testCountByExample() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: deleteByExample(StudentExample example)
      */
      @Test
      public void testDeleteByExample() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: deleteByPrimaryKey(Integer id)
      */
      @Test
      public void testDeleteByPrimaryKey() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: insert(Student record)
      */
      @Test
      public void testInsert() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: insertSelective(Student record)
      */
      @Test
      public void testInsertSelective() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: selectByExample(StudentExample example)
      */
      @Test
      public void testSelectByExample() throws Exception {
      StudentExample se
      = new StudentExample();

      se.createCriteria().andIdBetween(3, 5);

      List<Student> students = dao.selectByExample(se);
      System.out.println(students);
      }

    /**

    • Method: selectByPrimaryKey(Integer id)
      */
      @Test
      public void testSelectByPrimaryKey() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: updateByExampleSelective(Student record, StudentExample example)
      */
      @Test
      public void testUpdateByExampleSelective() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: updateByExample(Student record, StudentExample example)
      */
      @Test
      public void testUpdateByExample() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: updateByPrimaryKeySelective(Student record)
      */
      @Test
      public void testUpdateByPrimaryKeySelective() throws Exception {
      //TODO: Test goes here...
      }

    /**

    • Method: updateByPrimaryKey(Student record)
      */
      @Test
      public void testUpdateByPrimaryKey() throws Exception {
      //TODO: Test goes here...
      }

}

View Code

运行结果:

其它 GUI 工具:

https://github.com/spawpaw/mybatis-generator-gui-extension

mybatis-generator-gui-extension 是一个为 MybatisGenerator 编写的图形化界面,为实体 /Example/Mapper 提供了丰富的扩展。

https://github.com/xialeistudio/mybatis-generator-gui
可视化 mybatis 生成工具

[https://www.oschina.net/p/mybatis-generator-gui]

四、示例源代码

https://git.dev.tencent.com/zhangguo5/MyBatis06.git

https://git.coding.net/zhangguo5/MyBatis03.git

https://git.coding.net/zhangguo5/MyBatis02.git

五、视频

https://www.bilibili.com/video/av32447485/

六、大作业 

概述:

在中国云南有一个美丽的地方叫瑞丽,那里盛产玉饰,为了发展网络经济打开销路,有一家叫瑞丽玉源的公司搭建了一个电子商务网站,要求网站中可以按多种条件对商品进行搜索,可以同时按照多个条件进行过滤,包括:品牌、价格、颜色、水种、镶嵌、寓意和挂件类型。

功能需求:

1、要展示的数据已给出,在素材与数据 \Data 文件夹下有 MSSQL Server 数据库 SouthMall、SQL 脚本与 Excel 格式的数据, 使用其中任意方式可以获得需要的数据。

2、数据库中有一个名为 Products 的商品表,表结构如表 3-1 所示:

序号

列名

说明

数据类型

长度

小数位

标识

主键

允许空

1

Id

编号

int

4

0

 

2

Name

商品名称

nvarchar

255

0

 

 

3

Color

颜色

nvarchar

500

0

 

 

4

BrandId

品牌

int

4

0

 

 

5

InLayId

镶嵌

Int

4

0

 

 

6

MoralId

寓意

Int

4

0

 

 

7

IceTypeId

种地

Int

4

0

 

 

8

HangTypeId

挂件类型

int

4

0

 

 

9

MarketPrice

市场价格

float

8

0

 

 

10

MyPrice

商城价格

float

8

0

 

 

11

Picture

图片

nvarchar

255

0

 

 

表 3-1

请将 Color、Brand、InLay、Moral、IceType、HangType 作成外键, 拆分成 7 张表

3、默认状态为所有条件都选择全部,如图 3-1 所示,即展示所有商品;

图 3-1

4、当用户点击不同的条件时可以进行多种组合过滤出相应结果,被选择条件项应该高亮显示,而这时不应该勾选全部(这里使用了两张图片,一张是选择全部 checked.jpg,一张是未选择全部 unchecked.jpg,存放在素材与数据 /UI 图片目录中)如图 3-2 所示;

 

图 3-2 

技术实现:

1、为了考虑用户体验变换条件时不能刷新整页,可考虑使用 AJAX。jQuery 库文件已存放在素材与数据 /Scripts 目录下,请不要使用 ASP.NET AJAX 技术 (微软封装的服务器 AJAX 技术)。

2、搜索条件应该根据数据库动态生成,价格除外。

3、如果使用 ASP.NET WebForms 技术实现,页面中不能出现 ViewState(页面状态),请禁用控件与页面的状态(EnableViewState="false")。

4、可以使用任意 B/S 技术,如.NET,Java 或 PHP 平台下的 Web 技术。

5、除指定要求外,不对 UI 作特殊要求,如色彩、布局与字体等,基本规整就可以。UI 用到的所有素材已保存在素材与数据文件夹下。

6、数据访问方式不作限制,可以使用任意 ORM 框架或数据库访问组件,如 JDB、MyBatis、Hibernate、LINQ to SQL/Entity、Entity Framework、ADO.NET 等。

普通:

分页

后台添加

模块块、Maven、Git

高级:

在普通的基础上增加多字段排序(可以选择排序类型与升降序)

CRUD

 素材下载:https://git.dev.tencent.com/zhangguo5/MyBatis06.git