git源码:https://github.com/singgel/Spring-SkillTree/tree/master/spring-mybatis
Spring Boot 集成MyBatis和Pagehelper分页插件
mybatis-spring-boot-starter依赖树如下:
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>
<groupId>com.hks</groupId>
<artifactId>spring-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-mybatis</name>
<description>spring-mybatis project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
<!-- Mybatis generator. -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<!-- 配置这个依赖主要是为了等下在配置MG的时候可以不用配置classPathEntry这样的一个属性 -->
<!-- 避免代码的耦合度太高 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
application.properties配置
server.port=8080
# 数据源配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/relation_schema?useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#连接池配置
#spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
#mybatis
#entity扫描的包名
mybatis.type-aliases-package=com.hks.springmybatis.model
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath:mybatis/mapper/*Mapper.xml
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
#日志配置
logging.level.com.xiaolyuh=debug
logging.level.org.springframework.web=debug
logging.level.org.springframework.transaction=debug
logging.level.org.mybatis=debug
debug=false
除了上面常见的两项配置,还有:
mybatis.config-location=mybatis-config.xml配置文件的路径
mybatis.type-handlers-package=扫描typeHandlers的包
mybatis.check-config-location=检查配置文件是否存在
mybatis.executor-type=设置执行模式(SIMPLE, REUSE, BATCH),默认为SIMPLE
Mapping XML文件
在resources文件夹下创建generatorConfig.properties文件
# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/relation_schema?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
# 包路径配置
model.package=com.hks.springmybatis.model
mapper.package=com.hks.springmybatis.mapper
xml.package=mybatis/mapper
target.project=src/main/java
xml.target.project=src/main/resources
在resources文件夹下创建generatorConfig.xml文件
<?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="generatorConfig.properties"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="false"/>
<property name="javaFileEncoding" value="UTF-8"/>
</commentGenerator>
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
<property name="javaExampleTargetPackage" value="${model.package}"/>
<property name="javaExampleTargetProject" value="${target.project}"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="${xml.package}" targetProject="${xml.target.project}">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="${mapper.package}"
targetProject="${target.project}">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="department" >
<!--<generatedKey column="id" sqlStatement="Mysql" type="post" identity="true"/>-->
</table>
</context>
</generatorConfiguration>
DAO层Mapper类
在Mapper接口上需要加上@Mapper注解,@Mapper注解声明成mybatis Dao层的Bean。也可以在配置类上使用@MapperScan("com.**.mapper")注解声明。(自动生成)
package com.hks.springmybatis.mapper;
import com.hks.springmybatis.model.Department;
import com.hks.springmybatis.model.DepartmentExample;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface DepartmentMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
long countByExample(DepartmentExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int deleteByExample(DepartmentExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int deleteByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int insert(Department record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int insertSelective(Department record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
List<Department> selectByExample(DepartmentExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
Department selectByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int updateByExampleSelective(@Param("record") Department record, @Param("example") DepartmentExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int updateByExample(@Param("record") Department record, @Param("example") DepartmentExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int updateByPrimaryKeySelective(Department record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
int updateByPrimaryKey(Department record);
}
实体类(自动生成)
package com.hks.springmybatis.model;
import java.util.Date;
public class Department {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column department.id
*
* @mbg.generated
*/
private Long id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column department.name
*
* @mbg.generated
*/
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column department.user_create
*
* @mbg.generated
*/
private String userCreate;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column department.gmt_create
*
* @mbg.generated
*/
private Date gmtCreate;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column department.user_modified
*
* @mbg.generated
*/
private String userModified;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column department.gmt_modified
*
* @mbg.generated
*/
private Date gmtModified;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column department.id
*
* @return the value of department.id
*
* @mbg.generated
*/
public Long getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column department.id
*
* @param id the value for department.id
*
* @mbg.generated
*/
public void setId(Long id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column department.name
*
* @return the value of department.name
*
* @mbg.generated
*/
public String getName() {
return name;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column department.name
*
* @param name the value for department.name
*
* @mbg.generated
*/
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column department.user_create
*
* @return the value of department.user_create
*
* @mbg.generated
*/
public String getUserCreate() {
return userCreate;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column department.user_create
*
* @param userCreate the value for department.user_create
*
* @mbg.generated
*/
public void setUserCreate(String userCreate) {
this.userCreate = userCreate == null ? null : userCreate.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column department.gmt_create
*
* @return the value of department.gmt_create
*
* @mbg.generated
*/
public Date getGmtCreate() {
return gmtCreate;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column department.gmt_create
*
* @param gmtCreate the value for department.gmt_create
*
* @mbg.generated
*/
public void setGmtCreate(Date gmtCreate) {
this.gmtCreate = gmtCreate;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column department.user_modified
*
* @return the value of department.user_modified
*
* @mbg.generated
*/
public String getUserModified() {
return userModified;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column department.user_modified
*
* @param userModified the value for department.user_modified
*
* @mbg.generated
*/
public void setUserModified(String userModified) {
this.userModified = userModified == null ? null : userModified.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column department.gmt_modified
*
* @return the value of department.gmt_modified
*
* @mbg.generated
*/
public Date getGmtModified() {
return gmtModified;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column department.gmt_modified
*
* @param gmtModified the value for department.gmt_modified
*
* @mbg.generated
*/
public void setGmtModified(Date gmtModified) {
this.gmtModified = gmtModified;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
Department other = (Department) 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.getUserCreate() == null ? other.getUserCreate() == null : this.getUserCreate().equals(other.getUserCreate()))
&& (this.getGmtCreate() == null ? other.getGmtCreate() == null : this.getGmtCreate().equals(other.getGmtCreate()))
&& (this.getUserModified() == null ? other.getUserModified() == null : this.getUserModified().equals(other.getUserModified()))
&& (this.getGmtModified() == null ? other.getGmtModified() == null : this.getGmtModified().equals(other.getGmtModified()));
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
@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 + ((getUserCreate() == null) ? 0 : getUserCreate().hashCode());
result = prime * result + ((getGmtCreate() == null) ? 0 : getGmtCreate().hashCode());
result = prime * result + ((getUserModified() == null) ? 0 : getUserModified().hashCode());
result = prime * result + ((getGmtModified() == null) ? 0 : getGmtModified().hashCode());
return result;
}
}
package com.hks.springmybatis.model;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DepartmentExample {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table department
*
* @mbg.generated
*/
protected String orderByClause;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table department
*
* @mbg.generated
*/
protected boolean distinct;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table department
*
* @mbg.generated
*/
protected List<Criteria> oredCriteria;
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public DepartmentExample() {
oredCriteria = new ArrayList<Criteria>();
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public String getOrderByClause() {
return orderByClause;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public boolean isDistinct() {
return distinct;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table department
*
* @mbg.generated
*/
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table department
*
* @mbg.generated
*/
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(Long value) {
addCriterion("id =", value, "id");
return (Criteria) this;
}
public Criteria andIdNotEqualTo(Long value) {
addCriterion("id <>", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThan(Long value) {
addCriterion("id >", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThanOrEqualTo(Long value) {
addCriterion("id >=", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThan(Long value) {
addCriterion("id <", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThanOrEqualTo(Long value) {
addCriterion("id <=", value, "id");
return (Criteria) this;
}
public Criteria andIdIn(List<Long> values) {
addCriterion("id in", values, "id");
return (Criteria) this;
}
public Criteria andIdNotIn(List<Long> values) {
addCriterion("id not in", values, "id");
return (Criteria) this;
}
public Criteria andIdBetween(Long value1, Long value2) {
addCriterion("id between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andIdNotBetween(Long value1, Long value2) {
addCriterion("id not between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andNameIsNull() {
addCriterion("name is null");
return (Criteria) this;
}
public Criteria andNameIsNotNull() {
addCriterion("name is not null");
return (Criteria) this;
}
public Criteria andNameEqualTo(String value) {
addCriterion("name =", value, "name");
return (Criteria) this;
}
public Criteria andNameNotEqualTo(String value) {
addCriterion("name <>", value, "name");
return (Criteria) this;
}
public Criteria andNameGreaterThan(String value) {
addCriterion("name >", value, "name");
return (Criteria) this;
}
public Criteria andNameGreaterThanOrEqualTo(String value) {
addCriterion("name >=", value, "name");
return (Criteria) this;
}
public Criteria andNameLessThan(String value) {
addCriterion("name <", value, "name");
return (Criteria) this;
}
public Criteria andNameLessThanOrEqualTo(String value) {
addCriterion("name <=", value, "name");
return (Criteria) this;
}
public Criteria andNameLike(String value) {
addCriterion("name like", value, "name");
return (Criteria) this;
}
public Criteria andNameNotLike(String value) {
addCriterion("name not like", value, "name");
return (Criteria) this;
}
public Criteria andNameIn(List<String> values) {
addCriterion("name in", values, "name");
return (Criteria) this;
}
public Criteria andNameNotIn(List<String> values) {
addCriterion("name not in", values, "name");
return (Criteria) this;
}
public Criteria andNameBetween(String value1, String value2) {
addCriterion("name between", value1, value2, "name");
return (Criteria) this;
}
public Criteria andNameNotBetween(String value1, String value2) {
addCriterion("name not between", value1, value2, "name");
return (Criteria) this;
}
public Criteria andUserCreateIsNull() {
addCriterion("user_create is null");
return (Criteria) this;
}
public Criteria andUserCreateIsNotNull() {
addCriterion("user_create is not null");
return (Criteria) this;
}
public Criteria andUserCreateEqualTo(String value) {
addCriterion("user_create =", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateNotEqualTo(String value) {
addCriterion("user_create <>", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateGreaterThan(String value) {
addCriterion("user_create >", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateGreaterThanOrEqualTo(String value) {
addCriterion("user_create >=", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateLessThan(String value) {
addCriterion("user_create <", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateLessThanOrEqualTo(String value) {
addCriterion("user_create <=", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateLike(String value) {
addCriterion("user_create like", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateNotLike(String value) {
addCriterion("user_create not like", value, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateIn(List<String> values) {
addCriterion("user_create in", values, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateNotIn(List<String> values) {
addCriterion("user_create not in", values, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateBetween(String value1, String value2) {
addCriterion("user_create between", value1, value2, "userCreate");
return (Criteria) this;
}
public Criteria andUserCreateNotBetween(String value1, String value2) {
addCriterion("user_create not between", value1, value2, "userCreate");
return (Criteria) this;
}
public Criteria andGmtCreateIsNull() {
addCriterion("gmt_create is null");
return (Criteria) this;
}
public Criteria andGmtCreateIsNotNull() {
addCriterion("gmt_create is not null");
return (Criteria) this;
}
public Criteria andGmtCreateEqualTo(Date value) {
addCriterion("gmt_create =", value, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateNotEqualTo(Date value) {
addCriterion("gmt_create <>", value, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateGreaterThan(Date value) {
addCriterion("gmt_create >", value, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateGreaterThanOrEqualTo(Date value) {
addCriterion("gmt_create >=", value, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateLessThan(Date value) {
addCriterion("gmt_create <", value, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateLessThanOrEqualTo(Date value) {
addCriterion("gmt_create <=", value, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateIn(List<Date> values) {
addCriterion("gmt_create in", values, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateNotIn(List<Date> values) {
addCriterion("gmt_create not in", values, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateBetween(Date value1, Date value2) {
addCriterion("gmt_create between", value1, value2, "gmtCreate");
return (Criteria) this;
}
public Criteria andGmtCreateNotBetween(Date value1, Date value2) {
addCriterion("gmt_create not between", value1, value2, "gmtCreate");
return (Criteria) this;
}
public Criteria andUserModifiedIsNull() {
addCriterion("user_modified is null");
return (Criteria) this;
}
public Criteria andUserModifiedIsNotNull() {
addCriterion("user_modified is not null");
return (Criteria) this;
}
public Criteria andUserModifiedEqualTo(String value) {
addCriterion("user_modified =", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedNotEqualTo(String value) {
addCriterion("user_modified <>", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedGreaterThan(String value) {
addCriterion("user_modified >", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedGreaterThanOrEqualTo(String value) {
addCriterion("user_modified >=", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedLessThan(String value) {
addCriterion("user_modified <", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedLessThanOrEqualTo(String value) {
addCriterion("user_modified <=", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedLike(String value) {
addCriterion("user_modified like", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedNotLike(String value) {
addCriterion("user_modified not like", value, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedIn(List<String> values) {
addCriterion("user_modified in", values, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedNotIn(List<String> values) {
addCriterion("user_modified not in", values, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedBetween(String value1, String value2) {
addCriterion("user_modified between", value1, value2, "userModified");
return (Criteria) this;
}
public Criteria andUserModifiedNotBetween(String value1, String value2) {
addCriterion("user_modified not between", value1, value2, "userModified");
return (Criteria) this;
}
public Criteria andGmtModifiedIsNull() {
addCriterion("gmt_modified is null");
return (Criteria) this;
}
public Criteria andGmtModifiedIsNotNull() {
addCriterion("gmt_modified is not null");
return (Criteria) this;
}
public Criteria andGmtModifiedEqualTo(Date value) {
addCriterion("gmt_modified =", value, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedNotEqualTo(Date value) {
addCriterion("gmt_modified <>", value, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedGreaterThan(Date value) {
addCriterion("gmt_modified >", value, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedGreaterThanOrEqualTo(Date value) {
addCriterion("gmt_modified >=", value, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedLessThan(Date value) {
addCriterion("gmt_modified <", value, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedLessThanOrEqualTo(Date value) {
addCriterion("gmt_modified <=", value, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedIn(List<Date> values) {
addCriterion("gmt_modified in", values, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedNotIn(List<Date> values) {
addCriterion("gmt_modified not in", values, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedBetween(Date value1, Date value2) {
addCriterion("gmt_modified between", value1, value2, "gmtModified");
return (Criteria) this;
}
public Criteria andGmtModifiedNotBetween(Date value1, Date value2) {
addCriterion("gmt_modified not between", value1, value2, "gmtModified");
return (Criteria) this;
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table department
*
* @mbg.generated do_not_delete_during_merge
*/
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table department
*
* @mbg.generated
*/
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);
}
}
}
分页的包装类PageInfo
需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
package com.hks.springmybatis.mybatis;
import com.github.pagehelper.Page;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
/**
* 对Page<E>结果进行包装
* <p/>
* 新增分页的多项属性,主要参考:http://bbs.csdn.net/topics/360010907
*
* @author liuzh/abel533/isea533
* @version 3.3.0
* @since 3.2.2
* 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
public PageInfo() {
}
/**
* 包装Page对象
*
* @param list
*/
public PageInfo(List<T> list) {
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.list = page;
this.total = page.getTotal();
} else if (list instanceof Collection) {
this.pageNum = 1;
this.pageSize = list.size();
this.pages = 1;
this.list = list;
this.total = list.size();
}
if (list instanceof Collection) {
//判断页面边界
judgePageBoudary();
}
}
/**
* 判定页面边界
*/
private void judgePageBoudary() {
isFirstPage = pageNum == 1;
isLastPage = pageNum == pages;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public boolean isIsFirstPage() {
return isFirstPage;
}
public void setIsFirstPage(boolean isFirstPage) {
this.isFirstPage = isFirstPage;
}
public boolean isIsLastPage() {
return isLastPage;
}
public void setIsLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("PageInfo{");
sb.append("pageNum=").append(pageNum);
sb.append(", pageSize=").append(pageSize);
sb.append(", total=").append(total);
sb.append(", pages=").append(pages);
sb.append(", list=").append(list);
sb.append(", isFirstPage=").append(isFirstPage);
sb.append(", isLastPage=").append(isLastPage);
sb.append(", navigatepageNums=");
sb.append('}');
return sb.toString();
}
}
测试类
package com.hks.springmybatis.controller;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hks.springmybatis.mapper.DepartmentMapper;
import com.hks.springmybatis.model.Department;
import com.hks.springmybatis.model.DepartmentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: hekuangsheng
* @Date: 2018/10/16
*/
@Controller
public class DepartmentController {
@Autowired
private DepartmentMapper departmentMapper;
@RequestMapping(value = "/find/mybatis/page",method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> findUserPageFromMybatis(Integer pageNum, Integer pageSize) {
pageNum = pageNum == null ? 1 : pageNum;
pageSize = pageSize == null ? 10 : pageSize;
PageHelper.startPage(pageNum, pageSize);
List<Department> list = departmentMapper.selectByExample(new DepartmentExample());
PageInfo pageInfo = new PageInfo(list);
Page page = (Page) list;
Map<String, Object> map = new HashMap<>(2);
map.put("pageinfo",pageInfo);
map.put("page",page);
return map;
}
}