SpringBoot系列——MyBatis整合

  前言

  MyBatis官網:http://www.mybatis.org/mybatis-3/zh/index.htmlhtml

  本文記錄springboot與mybatis的整合實例;一、以註解方式;二、手寫XML配置、逆向工程生成XML配置java

 

  maven依賴mysql

        <!-- springboot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- MVC -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--Thymeleaf模板依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--熱部署工具dev-tools-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--添加MyBatis依賴 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--添加MySQL驅動依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

  application.ymlgit

#注意:在yml文件中添加value值時,value前面須要加一個空格
#2.0.0的配置切換爲servlet.path而不是"-"
server:
  port: 10086 #端口號
  servlet:
    context-path: /springboot

spring:
    thymeleaf:
      cache: false  #關閉頁面緩存
      prefix: classpath:/view/  #thymeleaf訪問根路徑
      mode: LEGACYHTML5

    datasource: #數據庫相關
      url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
    mvc:
      date-format: yyyy-MM-dd HH:mm:ss #mvc接收參數時對日期進行格式化

    jackson:
      date-format: yyyy-MM-dd HH:mm:ss #jackson對響應回去的日期參數進行格式化
      time-zone: GMT+8

mybatis:
  configuration:
   map-underscore-to-camel-case: true #開啓駝峯映射

   result,通用響應數據格式github

  注:不想本身寫set、get方法的用lombok插件web

public class Result {

    private String message;

    private Integer status;

    private Object data;

    public Result() {
    }

    public Result(String message, Integer status, Object data) {
        this.message = message;
        this.status = status;
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "message='" + message + '\'' +
                ", status=" + status +
                ", data=" + data +
                '}';
    }

    public static Result build(Integer status,String message,Object data){
            return new Result(message,status,data);
    }
}

   user,實體類spring

  注:不想本身寫set、get方法的用lombok插件sql

public class User {

    private Integer id;

    private String username;

    private String password;

    private Date created;

    public User(){}

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", created=" + created +
                '}';
    }
}

  表SQL數據庫

DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表id',
  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用戶名',
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密碼',
  `created` datetime NULL DEFAULT NULL COMMENT '建立時間',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 45 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用戶信息表' ROW_FORMAT = Compact;

 

  controller緩存

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/insert")
    public Result insert(User user) {
        return  userService.insert(user);
    }

    @RequestMapping("/delete")
    public Result delete(User user) {
        return  userService.delete(user);
    }

    @RequestMapping("/update")
    public Result update(User user) {
        return  userService.update(user);
    }

    @RequestMapping("/select")
    public Result select(User user) {
        return  userService.select(user);
    }
}

  service

public interface UserService {

    /**
     * 增
     */
    Result insert(User user);

    /**
     * 刪
     */
    Result delete(User user);

    /**
     * 改
     */
    Result update(User user);

    /**
     * 查
     */
    Result select(User user);
}
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result insert(User user) {
        int i = userMapper.insert(user);
        if (i > 0) {
            return Result.build(200, "操做成功!", user);
        } else {
            return Result.build(400, "操做失敗!", null);
        }
    }

    @Override
    public Result delete(User user) {
        int i = userMapper.delete(user);
        if (i > 0) {
            return Result.build(200, "操做成功!", user);
        } else {
            return Result.build(400, "操做失敗!", null);
        }
    }

    @Override
    public Result update(User user) {
        int i = userMapper.update(user);
        if (i > 0) {
            return select(user);
        } else {
            return Result.build(400, "操做失敗!", null);
        }
    }

    @Override
    public Result select(User user) {
        User user1 = userMapper.select(user);
        if (user1 != null) {
            return Result.build(200, "操做成功!", user1);
        } else {
            return Result.build(400, "操做失敗!", user1);
        }
    }
}

  

  接下來不一樣就是mapper映射了

 

  註解方式

  工程結構

  mapper.java

  直接使用註解,@Insert、@Delete、@Update、@Select,前三個事務自動提交,不用咱們管理;增、刪、改返回的是影響的行數,當設置了主鍵自動遞增,須要返回自增主鍵時添加@Options(useGeneratedKeys = true),設置是否獲取主鍵並設置到模型實體屬性中;注意:insert接口返回的依舊是受影響的行數,但自增主鍵已經設置到了模型實體屬性中。

  這裏介紹如下佔位符跟拼接符的區別:

  佔位符 #{param}
  佔位符在拼接sql的過程當中,會給指定的佔位符,加上引號
  簡單類型傳參:
  一、會忽略佔位符的名字
  二、會忽略佔位符的數據類型
  三、會忽略佔位符的順序,將傳進去的參加,依次賦給全部的佔位符
  適用場景:
  單一佔位符傳參查詢

 

  拼接符 ${param}
  拼接符至關於鏈接符合,把左右兩邊、本身拼接在一塊兒
  一、不接受簡單類型傳參,只接收對象類型或者Map集合類型傳參
  二、動態指定表名,列名,排序的方式(升序or降序)

@Mapper
@Component(value = "UserMapper")
public interface UserMapper {

    /**
     * 增
     */
    @Insert(value = "insert into tb_user(username,password,created) value(#{username},#{password},#{created})")
    @Options(useGeneratedKeys = true)
    int insert(User user);

    /**
     * 刪
     */
    @Delete("delete from tb_user where id = #{id}")
    int delete(User user);

    /**
     * 改
     */
    @Update(value = "update tb_user set username = #{username} where id = #{id}")
    int update(User user);

    /**
     * 查
     */
    @Select(value = "select * from tb_user where id = #{id}")
    User select(User user);

}

 

  配置XML

  工程目錄

  加多了mybatis相關的XML配置文件

 

  其餘部分都沒有變,變的只有如下部分:

  application.yml的mybatis部分改爲,

mybatis:

  # 配置mapper的掃描,找到全部的mapper.xml映射文件
  mapperLocations: classpath:conf/mybatis/mapper/*.xml

  # 加載全局的配置文件
  configLocation: classpath:conf/mybatis/mybatis.cfg.xml

  mapper.java

  再也不使用註解,將SQL寫到XML裏面

@Mapper
@Component(value = "UserMapper")
public interface UserMapper {

    /**
     * 增
     */
    int insert(User user);

    /**
     * 刪
     */
    int delete(User user);

    /**
     * 改
     */
    int update(User user);

    /**
     * 查
     */
    User select(User user);

}

   mybatis.cgf.xml

  這裏配置全局設置,但我這裏沒什麼要配的

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

</configuration>

  UserMapper.xml

  由於咱們的表主鍵是自動遞增,insert標籤使用useGeneratedKeys="true"  keyProperty="id",模型實體屬性就會設置遞增後的值

<?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="cn.mapper.UserMapper">

    <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.pojo.User">
        insert into tb_user(username,password,created) value(#{username},#{password},#{created})
    </insert>

    <delete id="delete" parameterType="cn.pojo.User">
        delete from tb_user where id = #{id}
    </delete>

    <update id="update" parameterType="cn.pojo.User">
        update tb_user set username = #{username} where id = #{id}
    </update>

    <select id="select" parameterType="cn.pojo.User" resultType="cn.pojo.User">
        select * from tb_user where id = #{id}
    </select>

</mapper>

 

  逆向工程

  注意:本逆向工程例子是我以前畢業設計的表爲操做對象,爲了方便介紹,我直接用以前的項目作介紹

  數據庫單表逆向生成單表對應的pojo實體、mapper.java、mapper.xml,無需手寫這幾個文件,生成的接口方法已經足夠知足大部分業務需求,實現快速開發,關於逆向工程的具體配置,請戳官網介紹:http://www.javashuo.com/article/p-ucavgype-cw.html 或這篇博客:Mybatis(七) mybatis的逆向工程的配置詳解

  逆向工程項目結構

  逆向工程配置文件genreatorConfig.xml,名字無所謂,只要在java程序中做爲file傳入就好  (存放生成文件的對應的包要提早建好,不然會找不到)

<?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>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--數據庫鏈接的信息:驅動類、鏈接地址、用戶名、密碼 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/cpsh" userId="root"
            password="123456">
        </jdbcConnection>
        <!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析爲 Integer,爲 true時把JDBC DECIMAL 和 
            NUMERIC 類型解析爲java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO類的位置 -->
        <javaModelGenerator targetPackage="cn.gx.hzu.cpsh.common.pojo"
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema做爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
            <!-- 從數據庫返回的值被清理先後的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件.xml生成的位置 -->
        <sqlMapGenerator targetPackage="cn.gx.hzu.cpsh.common.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema做爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口.java生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.gx.hzu.cpsh.common.mapper" 
            targetProject=".\src">
            <!-- enableSubPackages:是否讓schema做爲包的後綴 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>
        <!-- 指定數據庫表 -->
        <table schema="" tableName="t_ad"></table>
        <table schema="" tableName="t_admin"></table>
        <table schema="" tableName="t_ad_position"></table>
        <table schema="" tableName="t_chat"></table>
        <table schema="" tableName="t_purchase"></table>
        <table schema="" tableName="t_recommend"></table>
        <table schema="" tableName="t_sell"></table>
        <table schema="" tableName="t_type"></table>
        <table schema="" tableName="t_user"></table>
        <table schema="" tableName="t_user_datum"></table>
        <table schema="" tableName="t_notice"></table>
        
    </context>
</generatorConfiguration>

  GeneratorSqlmap.java 文件,配置好XML後,直接運行main函數就行

public class GeneratorSqlmap {

    public void generator() throws Exception{

        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("generatorConfig.xml"); 
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);

    } 
    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

}

  生成後整體的效果

  

  pojo.java

  數據庫字段若是是有大寫字母,逆向工程生成的實體類中的屬性全都是小寫

  數據庫字段若是有下劃線,逆向工程生成的實體類中下劃線後面的字母會以大寫開頭

public class TUser {
    private Integer id;

    private String username;

    private String password;

    private String phone;

    private String eamil;

    private Integer credit;

    private String registerTime;

    private String loginTime;

    private String loginCity;

    private String logoutTime;

    private String chatId;

    private Integer isAuthentication;

    private Integer status;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public String getEamil() {
        return eamil;
    }

    public void setEamil(String eamil) {
        this.eamil = eamil == null ? null : eamil.trim();
    }

    public Integer getCredit() {
        return credit;
    }

    public void setCredit(Integer credit) {
        this.credit = credit;
    }

    public String getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(String registerTime) {
        this.registerTime = registerTime == null ? null : registerTime.trim();
    }

    public String getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(String loginTime) {
        this.loginTime = loginTime == null ? null : loginTime.trim();
    }

    public String getLoginCity() {
        return loginCity;
    }

    public void setLoginCity(String loginCity) {
        this.loginCity = loginCity == null ? null : loginCity.trim();
    }

    public String getLogoutTime() {
        return logoutTime;
    }

    public void setLogoutTime(String logoutTime) {
        this.logoutTime = logoutTime == null ? null : logoutTime.trim();
    }

    public String getChatId() {
        return chatId;
    }

    public void setChatId(String chatId) {
        this.chatId = chatId == null ? null : chatId.trim();
    }

    public Integer getIsAuthentication() {
        return isAuthentication;
    }

    public void setIsAuthentication(Integer isAuthentication) {
        this.isAuthentication = isAuthentication;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}

   *Example.java

  example類的使用方法請戳:http://www.cnblogs.com/liuzunli/articles/9257949.html

public class TUserExample {
    protected String orderByClause;

    protected boolean distinct;

    protected List<Criteria> oredCriteria;

    public TUserExample() {
        oredCriteria = new ArrayList<Criteria>();
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public String getOrderByClause() {
        return orderByClause;
    }

    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public boolean isDistinct() {
        return distinct;
    }

    public List<Criteria> getOredCriteria() {
        return oredCriteria;
    }

    public void or(Criteria criteria) {
        oredCriteria.add(criteria);
    }

    public Criteria or() {
        Criteria criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }

    public Criteria createCriteria() {
        Criteria criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }

    protected Criteria createCriteriaInternal() {
        Criteria criteria = new Criteria();
        return criteria;
    }

    public void clear() {
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
    }

    protected abstract static class GeneratedCriteria {
        protected List<Criterion> criteria;

        protected GeneratedCriteria() {
            super();
            criteria = new ArrayList<Criterion>();
        }

        public boolean isValid() {
            return criteria.size() > 0;
        }

        public List<Criterion> getAllCriteria() {
            return criteria;
        }

        public List<Criterion> getCriteria() {
            return criteria;
        }

        protected void addCriterion(String condition) {
            if (condition == null) {
                throw new RuntimeException("Value for condition cannot be null");
            }
            criteria.add(new Criterion(condition));
        }

        protected void addCriterion(String condition, Object value, String property) {
            if (value == null) {
                throw new RuntimeException("Value for " + property + " cannot be null");
            }
            criteria.add(new Criterion(condition, value));
        }

        protected void addCriterion(String condition, Object value1, Object value2, String property) {
            if (value1 == null || value2 == null) {
                throw new RuntimeException("Between values for " + property + " cannot be null");
            }
            criteria.add(new Criterion(condition, value1, value2));
        }

        public Criteria andIdIsNull() {
            addCriterion("id is null");
            return (Criteria) this;
        }

        public Criteria andIdIsNotNull() {
            addCriterion("id is not null");
            return (Criteria) this;
        }

        public Criteria andIdEqualTo(Integer value) {
            addCriterion("id =", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotEqualTo(Integer value) {
            addCriterion("id <>", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdGreaterThan(Integer value) {
            addCriterion("id >", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdGreaterThanOrEqualTo(Integer value) {
            addCriterion("id >=", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdLessThan(Integer value) {
            addCriterion("id <", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdLessThanOrEqualTo(Integer value) {
            addCriterion("id <=", value, "id");
            return (Criteria) this;
        }

        public Criteria andIdIn(List<Integer> values) {
            addCriterion("id in", values, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotIn(List<Integer> values) {
            addCriterion("id not in", values, "id");
            return (Criteria) this;
        }

        public Criteria andIdBetween(Integer value1, Integer value2) {
            addCriterion("id between", value1, value2, "id");
            return (Criteria) this;
        }

        public Criteria andIdNotBetween(Integer value1, Integer value2) {
            addCriterion("id not between", value1, value2, "id");
            return (Criteria) this;
        }

        public Criteria andUsernameIsNull() {
            addCriterion("username is null");
            return (Criteria) this;
        }

        public Criteria andUsernameIsNotNull() {
            addCriterion("username is not null");
            return (Criteria) this;
        }

        public Criteria andUsernameEqualTo(String value) {
            addCriterion("username =", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameNotEqualTo(String value) {
            addCriterion("username <>", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameGreaterThan(String value) {
            addCriterion("username >", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameGreaterThanOrEqualTo(String value) {
            addCriterion("username >=", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameLessThan(String value) {
            addCriterion("username <", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameLessThanOrEqualTo(String value) {
            addCriterion("username <=", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameLike(String value) {
            addCriterion("username like", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameNotLike(String value) {
            addCriterion("username not like", value, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameIn(List<String> values) {
            addCriterion("username in", values, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameNotIn(List<String> values) {
            addCriterion("username not in", values, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameBetween(String value1, String value2) {
            addCriterion("username between", value1, value2, "username");
            return (Criteria) this;
        }

        public Criteria andUsernameNotBetween(String value1, String value2) {
            addCriterion("username not between", value1, value2, "username");
            return (Criteria) this;
        }

        public Criteria andPasswordIsNull() {
            addCriterion("password is null");
            return (Criteria) this;
        }

        public Criteria andPasswordIsNotNull() {
            addCriterion("password is not null");
            return (Criteria) this;
        }

        public Criteria andPasswordEqualTo(String value) {
            addCriterion("password =", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordNotEqualTo(String value) {
            addCriterion("password <>", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordGreaterThan(String value) {
            addCriterion("password >", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordGreaterThanOrEqualTo(String value) {
            addCriterion("password >=", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordLessThan(String value) {
            addCriterion("password <", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordLessThanOrEqualTo(String value) {
            addCriterion("password <=", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordLike(String value) {
            addCriterion("password like", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordNotLike(String value) {
            addCriterion("password not like", value, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordIn(List<String> values) {
            addCriterion("password in", values, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordNotIn(List<String> values) {
            addCriterion("password not in", values, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordBetween(String value1, String value2) {
            addCriterion("password between", value1, value2, "password");
            return (Criteria) this;
        }

        public Criteria andPasswordNotBetween(String value1, String value2) {
            addCriterion("password not between", value1, value2, "password");
            return (Criteria) this;
        }

        public Criteria andPhoneIsNull() {
            addCriterion("phone is null");
            return (Criteria) this;
        }

        public Criteria andPhoneIsNotNull() {
            addCriterion("phone is not null");
            return (Criteria) this;
        }

        public Criteria andPhoneEqualTo(String value) {
            addCriterion("phone =", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneNotEqualTo(String value) {
            addCriterion("phone <>", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneGreaterThan(String value) {
            addCriterion("phone >", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneGreaterThanOrEqualTo(String value) {
            addCriterion("phone >=", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneLessThan(String value) {
            addCriterion("phone <", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneLessThanOrEqualTo(String value) {
            addCriterion("phone <=", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneLike(String value) {
            addCriterion("phone like", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneNotLike(String value) {
            addCriterion("phone not like", value, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneIn(List<String> values) {
            addCriterion("phone in", values, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneNotIn(List<String> values) {
            addCriterion("phone not in", values, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneBetween(String value1, String value2) {
            addCriterion("phone between", value1, value2, "phone");
            return (Criteria) this;
        }

        public Criteria andPhoneNotBetween(String value1, String value2) {
            addCriterion("phone not between", value1, value2, "phone");
            return (Criteria) this;
        }

        public Criteria andEamilIsNull() {
            addCriterion("eamil is null");
            return (Criteria) this;
        }

        public Criteria andEamilIsNotNull() {
            addCriterion("eamil is not null");
            return (Criteria) this;
        }

        public Criteria andEamilEqualTo(String value) {
            addCriterion("eamil =", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilNotEqualTo(String value) {
            addCriterion("eamil <>", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilGreaterThan(String value) {
            addCriterion("eamil >", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilGreaterThanOrEqualTo(String value) {
            addCriterion("eamil >=", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilLessThan(String value) {
            addCriterion("eamil <", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilLessThanOrEqualTo(String value) {
            addCriterion("eamil <=", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilLike(String value) {
            addCriterion("eamil like", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilNotLike(String value) {
            addCriterion("eamil not like", value, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilIn(List<String> values) {
            addCriterion("eamil in", values, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilNotIn(List<String> values) {
            addCriterion("eamil not in", values, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilBetween(String value1, String value2) {
            addCriterion("eamil between", value1, value2, "eamil");
            return (Criteria) this;
        }

        public Criteria andEamilNotBetween(String value1, String value2) {
            addCriterion("eamil not between", value1, value2, "eamil");
            return (Criteria) this;
        }

        public Criteria andCreditIsNull() {
            addCriterion("credit is null");
            return (Criteria) this;
        }

        public Criteria andCreditIsNotNull() {
            addCriterion("credit is not null");
            return (Criteria) this;
        }

        public Criteria andCreditEqualTo(Integer value) {
            addCriterion("credit =", value, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditNotEqualTo(Integer value) {
            addCriterion("credit <>", value, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditGreaterThan(Integer value) {
            addCriterion("credit >", value, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditGreaterThanOrEqualTo(Integer value) {
            addCriterion("credit >=", value, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditLessThan(Integer value) {
            addCriterion("credit <", value, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditLessThanOrEqualTo(Integer value) {
            addCriterion("credit <=", value, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditIn(List<Integer> values) {
            addCriterion("credit in", values, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditNotIn(List<Integer> values) {
            addCriterion("credit not in", values, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditBetween(Integer value1, Integer value2) {
            addCriterion("credit between", value1, value2, "credit");
            return (Criteria) this;
        }

        public Criteria andCreditNotBetween(Integer value1, Integer value2) {
            addCriterion("credit not between", value1, value2, "credit");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeIsNull() {
            addCriterion("register_time is null");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeIsNotNull() {
            addCriterion("register_time is not null");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeEqualTo(String value) {
            addCriterion("register_time =", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeNotEqualTo(String value) {
            addCriterion("register_time <>", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeGreaterThan(String value) {
            addCriterion("register_time >", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeGreaterThanOrEqualTo(String value) {
            addCriterion("register_time >=", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeLessThan(String value) {
            addCriterion("register_time <", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeLessThanOrEqualTo(String value) {
            addCriterion("register_time <=", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeLike(String value) {
            addCriterion("register_time like", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeNotLike(String value) {
            addCriterion("register_time not like", value, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeIn(List<String> values) {
            addCriterion("register_time in", values, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeNotIn(List<String> values) {
            addCriterion("register_time not in", values, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeBetween(String value1, String value2) {
            addCriterion("register_time between", value1, value2, "registerTime");
            return (Criteria) this;
        }

        public Criteria andRegisterTimeNotBetween(String value1, String value2) {
            addCriterion("register_time not between", value1, value2, "registerTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeIsNull() {
            addCriterion("login_time is null");
            return (Criteria) this;
        }

        public Criteria andLoginTimeIsNotNull() {
            addCriterion("login_time is not null");
            return (Criteria) this;
        }

        public Criteria andLoginTimeEqualTo(String value) {
            addCriterion("login_time =", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeNotEqualTo(String value) {
            addCriterion("login_time <>", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeGreaterThan(String value) {
            addCriterion("login_time >", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeGreaterThanOrEqualTo(String value) {
            addCriterion("login_time >=", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeLessThan(String value) {
            addCriterion("login_time <", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeLessThanOrEqualTo(String value) {
            addCriterion("login_time <=", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeLike(String value) {
            addCriterion("login_time like", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeNotLike(String value) {
            addCriterion("login_time not like", value, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeIn(List<String> values) {
            addCriterion("login_time in", values, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeNotIn(List<String> values) {
            addCriterion("login_time not in", values, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeBetween(String value1, String value2) {
            addCriterion("login_time between", value1, value2, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginTimeNotBetween(String value1, String value2) {
            addCriterion("login_time not between", value1, value2, "loginTime");
            return (Criteria) this;
        }

        public Criteria andLoginCityIsNull() {
            addCriterion("login_city is null");
            return (Criteria) this;
        }

        public Criteria andLoginCityIsNotNull() {
            addCriterion("login_city is not null");
            return (Criteria) this;
        }

        public Criteria andLoginCityEqualTo(String value) {
            addCriterion("login_city =", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityNotEqualTo(String value) {
            addCriterion("login_city <>", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityGreaterThan(String value) {
            addCriterion("login_city >", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityGreaterThanOrEqualTo(String value) {
            addCriterion("login_city >=", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityLessThan(String value) {
            addCriterion("login_city <", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityLessThanOrEqualTo(String value) {
            addCriterion("login_city <=", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityLike(String value) {
            addCriterion("login_city like", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityNotLike(String value) {
            addCriterion("login_city not like", value, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityIn(List<String> values) {
            addCriterion("login_city in", values, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityNotIn(List<String> values) {
            addCriterion("login_city not in", values, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityBetween(String value1, String value2) {
            addCriterion("login_city between", value1, value2, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLoginCityNotBetween(String value1, String value2) {
            addCriterion("login_city not between", value1, value2, "loginCity");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeIsNull() {
            addCriterion("logout_time is null");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeIsNotNull() {
            addCriterion("logout_time is not null");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeEqualTo(String value) {
            addCriterion("logout_time =", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeNotEqualTo(String value) {
            addCriterion("logout_time <>", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeGreaterThan(String value) {
            addCriterion("logout_time >", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeGreaterThanOrEqualTo(String value) {
            addCriterion("logout_time >=", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeLessThan(String value) {
            addCriterion("logout_time <", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeLessThanOrEqualTo(String value) {
            addCriterion("logout_time <=", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeLike(String value) {
            addCriterion("logout_time like", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeNotLike(String value) {
            addCriterion("logout_time not like", value, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeIn(List<String> values) {
            addCriterion("logout_time in", values, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeNotIn(List<String> values) {
            addCriterion("logout_time not in", values, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeBetween(String value1, String value2) {
            addCriterion("logout_time between", value1, value2, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andLogoutTimeNotBetween(String value1, String value2) {
            addCriterion("logout_time not between", value1, value2, "logoutTime");
            return (Criteria) this;
        }

        public Criteria andChatIdIsNull() {
            addCriterion("chat_id is null");
            return (Criteria) this;
        }

        public Criteria andChatIdIsNotNull() {
            addCriterion("chat_id is not null");
            return (Criteria) this;
        }

        public Criteria andChatIdEqualTo(String value) {
            addCriterion("chat_id =", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdNotEqualTo(String value) {
            addCriterion("chat_id <>", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdGreaterThan(String value) {
            addCriterion("chat_id >", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdGreaterThanOrEqualTo(String value) {
            addCriterion("chat_id >=", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdLessThan(String value) {
            addCriterion("chat_id <", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdLessThanOrEqualTo(String value) {
            addCriterion("chat_id <=", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdLike(String value) {
            addCriterion("chat_id like", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdNotLike(String value) {
            addCriterion("chat_id not like", value, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdIn(List<String> values) {
            addCriterion("chat_id in", values, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdNotIn(List<String> values) {
            addCriterion("chat_id not in", values, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdBetween(String value1, String value2) {
            addCriterion("chat_id between", value1, value2, "chatId");
            return (Criteria) this;
        }

        public Criteria andChatIdNotBetween(String value1, String value2) {
            addCriterion("chat_id not between", value1, value2, "chatId");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationIsNull() {
            addCriterion("is_authentication is null");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationIsNotNull() {
            addCriterion("is_authentication is not null");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationEqualTo(Integer value) {
            addCriterion("is_authentication =", value, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationNotEqualTo(Integer value) {
            addCriterion("is_authentication <>", value, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationGreaterThan(Integer value) {
            addCriterion("is_authentication >", value, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationGreaterThanOrEqualTo(Integer value) {
            addCriterion("is_authentication >=", value, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationLessThan(Integer value) {
            addCriterion("is_authentication <", value, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationLessThanOrEqualTo(Integer value) {
            addCriterion("is_authentication <=", value, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationIn(List<Integer> values) {
            addCriterion("is_authentication in", values, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationNotIn(List<Integer> values) {
            addCriterion("is_authentication not in", values, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationBetween(Integer value1, Integer value2) {
            addCriterion("is_authentication between", value1, value2, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andIsAuthenticationNotBetween(Integer value1, Integer value2) {
            addCriterion("is_authentication not between", value1, value2, "isAuthentication");
            return (Criteria) this;
        }

        public Criteria andStatusIsNull() {
            addCriterion("status is null");
            return (Criteria) this;
        }

        public Criteria andStatusIsNotNull() {
            addCriterion("status is not null");
            return (Criteria) this;
        }

        public Criteria andStatusEqualTo(Integer value) {
            addCriterion("status =", value, "status");
            return (Criteria) this;
        }

        public Criteria andStatusNotEqualTo(Integer value) {
            addCriterion("status <>", value, "status");
            return (Criteria) this;
        }

        public Criteria andStatusGreaterThan(Integer value) {
            addCriterion("status >", value, "status");
            return (Criteria) this;
        }

        public Criteria andStatusGreaterThanOrEqualTo(Integer value) {
            addCriterion("status >=", value, "status");
            return (Criteria) this;
        }

        public Criteria andStatusLessThan(Integer value) {
            addCriterion("status <", value, "status");
            return (Criteria) this;
        }

        public Criteria andStatusLessThanOrEqualTo(Integer value) {
            addCriterion("status <=", value, "status");
            return (Criteria) this;
        }

        public Criteria andStatusIn(List<Integer> values) {
            addCriterion("status in", values, "status");
            return (Criteria) this;
        }

        public Criteria andStatusNotIn(List<Integer> values) {
            addCriterion("status not in", values, "status");
            return (Criteria) this;
        }

        public Criteria andStatusBetween(Integer value1, Integer value2) {
            addCriterion("status between", value1, value2, "status");
            return (Criteria) this;
        }

        public Criteria andStatusNotBetween(Integer value1, Integer value2) {
            addCriterion("status not between", value1, value2, "status");
            return (Criteria) this;
        }
    }

    public static class Criteria extends GeneratedCriteria {

        protected Criteria() {
            super();
        }
    }

    public static class Criterion {
        private String condition;

        private Object value;

        private Object secondValue;

        private boolean noValue;

        private boolean singleValue;

        private boolean betweenValue;

        private boolean listValue;

        private String typeHandler;

        public String getCondition() {
            return condition;
        }

        public Object getValue() {
            return value;
        }

        public Object getSecondValue() {
            return secondValue;
        }

        public boolean isNoValue() {
            return noValue;
        }

        public boolean isSingleValue() {
            return singleValue;
        }

        public boolean isBetweenValue() {
            return betweenValue;
        }

        public boolean isListValue() {
            return listValue;
        }

        public String getTypeHandler() {
            return typeHandler;
        }

        protected Criterion(String condition) {
            super();
            this.condition = condition;
            this.typeHandler = null;
            this.noValue = true;
        }

        protected Criterion(String condition, Object value, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.typeHandler = typeHandler;
            if (value instanceof List<?>) {
                this.listValue = true;
            } else {
                this.singleValue = true;
            }
        }

        protected Criterion(String condition, Object value) {
            this(condition, value, null);
        }

        protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
            super();
            this.condition = condition;
            this.value = value;
            this.secondValue = secondValue;
            this.typeHandler = typeHandler;
            this.betweenValue = true;
        }

        protected Criterion(String condition, Object value, Object secondValue) {
            this(condition, value, secondValue, null);
        }
    }
}

 

  *mapper.java

public interface TUserMapper {
    int countByExample(TUserExample example);

    int deleteByExample(TUserExample example);

    int deleteByPrimaryKey(Integer id);

    int insert(TUser record);

    int insertSelective(TUser record);

    List<TUser> selectByExample(TUserExample example);

    TUser selectByPrimaryKey(Integer id);

    int updateByExampleSelective(@Param("record") TUser record, @Param("example") TUserExample example);

    int updateByExample(@Param("record") TUser record, @Param("example") TUserExample example);

    int updateByPrimaryKeySelective(TUser record);

    int updateByPrimaryKey(TUser record);
}

   *Mapper.xml

<?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="cn.gx.hzu.cpsh.common.mapper.TUserMapper" >
  <resultMap id="BaseResultMap" type="cn.gx.hzu.cpsh.common.pojo.TUser" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="eamil" property="eamil" jdbcType="VARCHAR" />
    <result column="credit" property="credit" jdbcType="INTEGER" />
    <result column="register_time" property="registerTime" jdbcType="VARCHAR" />
    <result column="login_time" property="loginTime" jdbcType="VARCHAR" />
    <result column="login_city" property="loginCity" jdbcType="VARCHAR" />
    <result column="logout_time" property="logoutTime" jdbcType="VARCHAR" />
    <result column="chat_id" property="chatId" jdbcType="VARCHAR" />
    <result column="is_authentication" property="isAuthentication" jdbcType="INTEGER" />
    <result column="status" property="status" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <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 collection="criterion.value" item="listItem" open="(" close=")" 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="(" suffix=")" prefixOverrides="and" >
            <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 collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    id, username, password, phone, eamil, credit, register_time, login_time, login_city, 
    logout_time, chat_id, is_authentication, status
  </sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="cn.gx.hzu.cpsh.common.pojo.TUserExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_user
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="cn.gx.hzu.cpsh.common.pojo.TUserExample" >
    delete from t_user
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="cn.gx.hzu.cpsh.common.pojo.TUser" >
    insert into t_user (id, username, password, 
      phone, eamil, credit, 
      register_time, login_time, login_city, 
      logout_time, chat_id, is_authentication, 
      status)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{phone,jdbcType=VARCHAR}, #{eamil,jdbcType=VARCHAR}, #{credit,jdbcType=INTEGER}, 
      #{registerTime,jdbcType=VARCHAR}, #{loginTime,jdbcType=VARCHAR}, #{loginCity,jdbcType=VARCHAR}, 
      #{logoutTime,jdbcType=VARCHAR}, #{chatId,jdbcType=VARCHAR}, #{isAuthentication,jdbcType=INTEGER}, 
      #{status,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="cn.gx.hzu.cpsh.common.pojo.TUser" >
    insert into t_user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="username != null" >
        username,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="phone != null" >
        phone,
      </if>
      <if test="eamil != null" >
        eamil,
      </if>
      <if test="credit != null" >
        credit,
      </if>
      <if test="registerTime != null" >
        register_time,
      </if>
      <if test="loginTime != null" >
        login_time,
      </if>
      <if test="loginCity != null" >
        login_city,
      </if>
      <if test="logoutTime != null" >
        logout_time,
      </if>
      <if test="chatId != null" >
        chat_id,
      </if>
      <if test="isAuthentication != null" >
        is_authentication,
      </if>
      <if test="status != null" >
        status,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="eamil != null" >
        #{eamil,jdbcType=VARCHAR},
      </if>
      <if test="credit != null" >
        #{credit,jdbcType=INTEGER},
      </if>
      <if test="registerTime != null" >
        #{registerTime,jdbcType=VARCHAR},
      </if>
      <if test="loginTime != null" >
        #{loginTime,jdbcType=VARCHAR},
      </if>
      <if test="loginCity != null" >
        #{loginCity,jdbcType=VARCHAR},
      </if>
      <if test="logoutTime != null" >
        #{logoutTime,jdbcType=VARCHAR},
      </if>
      <if test="chatId != null" >
        #{chatId,jdbcType=VARCHAR},
      </if>
      <if test="isAuthentication != null" >
        #{isAuthentication,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
        #{status,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="cn.gx.hzu.cpsh.common.pojo.TUserExample" resultType="java.lang.Integer" >
    select count(*) from t_user
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    update t_user
    <set >
      <if test="record.id != null" >
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.username != null" >
        username = #{record.username,jdbcType=VARCHAR},
      </if>
      <if test="record.password != null" >
        password = #{record.password,jdbcType=VARCHAR},
      </if>
      <if test="record.phone != null" >
        phone = #{record.phone,jdbcType=VARCHAR},
      </if>
      <if test="record.eamil != null" >
        eamil = #{record.eamil,jdbcType=VARCHAR},
      </if>
      <if test="record.credit != null" >
        credit = #{record.credit,jdbcType=INTEGER},
      </if>
      <if test="record.registerTime != null" >
        register_time = #{record.registerTime,jdbcType=VARCHAR},
      </if>
      <if test="record.loginTime != null" >
        login_time = #{record.loginTime,jdbcType=VARCHAR},
      </if>
      <if test="record.loginCity != null" >
        login_city = #{record.loginCity,jdbcType=VARCHAR},
      </if>
      <if test="record.logoutTime != null" >
        logout_time = #{record.logoutTime,jdbcType=VARCHAR},
      </if>
      <if test="record.chatId != null" >
        chat_id = #{record.chatId,jdbcType=VARCHAR},
      </if>
      <if test="record.isAuthentication != null" >
        is_authentication = #{record.isAuthentication,jdbcType=INTEGER},
      </if>
      <if test="record.status != null" >
        status = #{record.status,jdbcType=INTEGER},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    update t_user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      password = #{record.password,jdbcType=VARCHAR},
      phone = #{record.phone,jdbcType=VARCHAR},
      eamil = #{record.eamil,jdbcType=VARCHAR},
      credit = #{record.credit,jdbcType=INTEGER},
      register_time = #{record.registerTime,jdbcType=VARCHAR},
      login_time = #{record.loginTime,jdbcType=VARCHAR},
      login_city = #{record.loginCity,jdbcType=VARCHAR},
      logout_time = #{record.logoutTime,jdbcType=VARCHAR},
      chat_id = #{record.chatId,jdbcType=VARCHAR},
      is_authentication = #{record.isAuthentication,jdbcType=INTEGER},
      status = #{record.status,jdbcType=INTEGER}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="cn.gx.hzu.cpsh.common.pojo.TUser" >
    update t_user
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="phone != null" >
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="eamil != null" >
        eamil = #{eamil,jdbcType=VARCHAR},
      </if>
      <if test="credit != null" >
        credit = #{credit,jdbcType=INTEGER},
      </if>
      <if test="registerTime != null" >
        register_time = #{registerTime,jdbcType=VARCHAR},
      </if>
      <if test="loginTime != null" >
        login_time = #{loginTime,jdbcType=VARCHAR},
      </if>
      <if test="loginCity != null" >
        login_city = #{loginCity,jdbcType=VARCHAR},
      </if>
      <if test="logoutTime != null" >
        logout_time = #{logoutTime,jdbcType=VARCHAR},
      </if>
      <if test="chatId != null" >
        chat_id = #{chatId,jdbcType=VARCHAR},
      </if>
      <if test="isAuthentication != null" >
        is_authentication = #{isAuthentication,jdbcType=INTEGER},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.gx.hzu.cpsh.common.pojo.TUser" >
    update t_user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      eamil = #{eamil,jdbcType=VARCHAR},
      credit = #{credit,jdbcType=INTEGER},
      register_time = #{registerTime,jdbcType=VARCHAR},
      login_time = #{loginTime,jdbcType=VARCHAR},
      login_city = #{loginCity,jdbcType=VARCHAR},
      logout_time = #{logoutTime,jdbcType=VARCHAR},
      chat_id = #{chatId,jdbcType=VARCHAR},
      is_authentication = #{isAuthentication,jdbcType=INTEGER},
      status = #{status,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

 

  生成好代碼以後咱們就能夠掉mapper接口的方法愉快的進行開發了,配合上pagehelpr插件,輕鬆實現分頁、排序

  若是你也在用Mybatis,建議嘗試該分頁插件,這個必定是最方便使用的分頁插件。該插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫分頁。

  maven

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>3.4.2-fix</version>
    </dependency>

  第一步:在Mybatis配置xml中配置攔截器插件:

<!-- 配置分頁插件 -->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫-->        
        <property name="dialect" value="mysql"/>
    </plugin>
</plugins>

  第二步:在代碼中使用

//分頁處理  page當前頁   rows每頁多少條記錄
PageHelper.startPage(page, rows);
//緊跟着的第一個select就會被分頁 查詢回來的list大小爲rows
List<TbItem> list = itemMapper.selectByExample(example);
//使用PageInfo對結果集進行包裝 經過包裝後的pageinfo對象取得相關數據
PageInfo<TbItem> pageInfo = new PageInfo<>(list);

  PageInfo類  定義的屬性:

    //當前頁
    private int pageNum;
    //每頁的數量
    private int pageSize;
    //當前頁的數量
    private int size;
    //排序
    private String orderBy;
    //因爲startRow和endRow不經常使用,這裏說個具體的用法
    //能夠在頁面中"顯示startRow到endRow 共size條數據"
    private int startRow;
    private int endRow;
    //當前頁面第一個元素在數據庫中的行號
    private int startRow;
    //當前頁面最後一個元素在數據庫中的行號
    private int endRow;
    //總記錄數
    private long total;
    //總頁數
    private int pages;
    //結果集
    private List<T> list;
    //第一頁
    private int firstPage;
    //前一頁
    private int prePage;
    //下一頁
    private int nextPage;
    //最後一頁
    private int lastPage;
    //是否爲第一頁
    private boolean isFirstPage = false;
    //是否爲最後一頁
    private boolean isLastPage = false;
    //是否有前一頁
    private boolean hasPreviousPage = false;
    //是否有下一頁
    private boolean hasNextPage = false;
    //導航頁碼數
    private int navigatePages;
    //全部導航頁號
    private int[] navigatepageNums;

  

  原理:分頁插件會自動在你的查詢語句後面添加 order by addtime DESC limit ?,?


  使用原理:
  pageHelper會使用ThreadLocal獲取到同一線程中的變量信息,各個線程之間的Threadlocal不會相互干擾,也就是Thread1中的ThreadLocal1以後獲取到Tread1中的變量的信息,不會獲取到Thread2中的信息
  因此在多線程環境下,各個Threadlocal之間相互隔離,能夠實現,不一樣thread使用不一樣的數據源或不一樣的Thread中執行不一樣的SQL語句
  因此,PageHelper利用這一點經過攔截器獲取到同一線程中的預編譯好的SQL語句以後將SQL語句包裝成具備分頁功能的SQL語句,並將其再次賦值給下一步操做,因此實際執行的SQL語句就是有了分頁功能的SQL語句
  

  注意:
  一、只有緊跟在PageHelper.startPage方法後的第一個Mybatis的查詢(Select方法)方法會被分頁。
  二、分頁插件不支持帶有for update語句的分頁
  三、分頁插件不支持關聯結果查詢

 

 

  效果展現

  經測試,註解方式跟配置XML方式效果一致,傳參、調用方式、返回結果都一致,接口均能正常調用。

  insert接口

  update接口

  select接口

  delete接口

  後記

  MyBatis 是一款優秀的持久層框架,它支持定製化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎全部的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可使用簡單的 XML 或註解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。

  當咱們一提及mybatis就會默默滴跟hibernate進行對比:

  Hibernate DAO層開發比較簡單,sql語句都封裝好了,mybatis須要維護sql,手動寫
  Hibernate可移植性高,更改數據庫方言便可,mybatis可移植性差,不一樣的數據庫sql不一樣
  Mybatis能夠進行更細緻的sql優化,減小不必的查詢字段,而hibernate封裝太過完全

 

  代碼開源

  代碼已經開源、託管到個人GitHub、碼雲:

  GitHub:https://github.com/huanzi-qch/springBoot

  碼雲:https://gitee.com/huanzi-qch/springBoot

相關文章
相關標籤/搜索