mybatis-plus是mybatis的增强版,其中对于简单的CRUD操作及分页查询有极其简便的配置方式,大大增加了开发效率,并且不影响之前mybatis的使用习惯。
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>3.2.0version>
dependency>
<dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.2version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
虽然mybatis-plus 有原生的分页插件类可供使用,但是也只是供
BaseMapper,如果是自己写在mapper.xml文件中,则其分页插件就不起作用,就要使用第三方分页插件PageHelper。
加入log4j可以查看每次执行的sql语句,要配置log4j.properties文件
log4j.rootLogger=INFO,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern= %d{yy-MM-dd HH:mm:ss:SSSS} %p %C{1}.%M(%L) | %m%n
log4j.appender.stdout.layout.ConversionPattern= %d %-5p %c.%M:%L - %m%n
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
<configuration>
<settings>
<setting name="useGeneratedKeys" value="true" />
<setting name="useColumnLabel" value="true" />
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="logImpl" value="STDOUT_LOGGING" />
settings>
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<property name="dialecType" value="mysql" />
plugin>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
plugin>
plugins>
configuration>
此处pageHelper仅仅是注册一下类就可以了,无需写方言,自动匹配,而mybatis-plus的原生分页要初始化一下方言类型。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xr.dao.ContractStampUpdateMapper">
</mapper>
public interface ContractStampUpdateMapper extends BaseMapper<ContractStampUpdate> {
}
由于Mybatis-plus有BaseMapper类进行基本的CRUD所以可以不用Mapper文件内容可以不用写,同样的Mapper接口类也不用写实际内容,但是必须要有而且接口必须继承BaseMapper接口。
package com.xr.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.Date;
@TableName("contract_stamp_update")
public class ContractStampUpdate {
@TableId(value = "ID",type = IdType.AUTO)
private Long id;
@TableField("CONTRACT_ID")
private String contractId;
@TableField("CONTRACT_NO")
private String contractNo;
@TableField("PRINT_TYPE")
private String printType;
@TableField("PRINT_URL")
private String printUrl;
@TableField("VIEW_URL")
private String viewUrl;
@TableField("PRINT_CODE")
private String printCode;
@TableField("PRINT_NAME")
private String printName;
@TableField("PRINT_DATE")
private Date printDate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContractId() {
return contractId;
}
public void setContractId(String contractId) {
this.contractId = contractId;
}
public String getContractNo() {
return contractNo;
}
public void setContractNo(String contractNo) {
this.contractNo = contractNo;
}
public String getPrintType() {
return printType;
}
public void setPrintType(String printType) {
this.printType = printType;
}
public String getPrintUrl() {
return printUrl;
}
public void setPrintUrl(String printUrl) {
this.printUrl = printUrl;
}
public String getViewUrl() {
return viewUrl;
}
public void setViewUrl(String viewUrl) {
this.viewUrl = viewUrl;
}
public String getPrintCode() {
return printCode;
}
public void setPrintCode(String printCode) {
this.printCode = printCode;
}
public String getPrintName() {
return printName;
}
public void setPrintName(String printName) {
this.printName = printName;
}
public Date getPrintDate() {
return printDate;
}
public void setPrintDate(Date printDate) {
this.printDate = printDate;
}
}
注解写好自动与数据库进行匹配,这就是接口继承BaseMapper之后无需编写mapper.xml的奥妙。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.propertiesvalue>
list>
property>
bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.connection.driverclassname}" />
<property name="url" value="${jdbc.connection.url}"/>
<property name="username" value="${jdbc.connection.name}"/>
<property name="password" value="${jdbc.connection.password}"/>
bean>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="com.xr.dao" />
bean>
<context:component-scan base-package="com.xr" />
beans>
由此可以看到mybaits与mybatis-plus 的spring配置时的区别,主要在sqlSessionFactory上,bean使用的class不同。
@Autowired
public class Controller1 {
ContractStampUpdateMapper mapper;
@RequestMapping(value = "/test" ,produces = "application/json;charset=utf-8",method = RequestMethod.POST)
@ResponseBody
public String one() throws JsonProcessingException {
QueryWrapper<ContractStampUpdate> wrapper = new QueryWrapper<>();
Page page = new Page(1,2);
IPage iPage = mapper.selectPage(page, wrapper);
ObjectMapper omapper = new ObjectMapper();
String s = omapper.writeValueAsString(iPage);
System.out.println(s);
return s;
}
}
{
"records": [
{
"id": 294,
"contractId": "20191107150003",
"contractNo": "573455149800",
"printType": "2",
"printUrl": null,
"viewUrl": null,
"printCode": null,
"printName": null,
"printDate": 1573427454000
},
{
"id": 295,
"contractId": "20191107150003",
"contractNo": "573455149800",
"printType": "3",
"printUrl": "",
"viewUrl": null,
"printCode": null,
"printName": null,
"printDate": 1573427702000
}
],
"total": 7,
"size": 2,
"current": 1,
"orders": [],
"searchCount": true,
"pages": 4
}