通用 Mapper4 是一個能夠實現任意 MyBatis 通用方法的框架,項目提供了常規的增刪改查操做以及Example 相關的單表操做。通用 Mapper 是爲了解決 MyBatis 使用中 90% 的基本操做,使用它能夠很方便的進行開發,能夠節省開發人員大量的時間.文檔java
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.virgo</groupId>
<artifactId>mapper4-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mapper4-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.6</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</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.6</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
複製代碼
<!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="db.properties"/>-->
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
<property name="lombok" value="Getter,Setter,ToString"/>
</plugin>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="XXXX?nullCatalogMeansCurrent=true"
userId=""
password="">
</jdbcConnection>
<!--實體-->
<javaModelGenerator targetPackage="com.virgo.mapper4demo.entity"
targetProject="src/main/java"/>
<!--mapper.xml-->
<sqlMapGenerator targetPackage="com.virgo.mapper4demo.mapper"
targetProject="src/main/resources"/>
<!--mapper接口-->
<javaClientGenerator targetPackage="com.virgo.mapper4demo.dao"
targetProject="src/main/java"
type="XMLMAPPER"/>
<!--爲哪張表生成代碼-->
<table tableName="tbl_user">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
複製代碼
雖然框架自己提供的方法基本已經知足咱們的需求,可是有的時候咱們仍是但願自定義sql語句,經過註解的方式咱們能夠很方便的進行自定義sql。mysql
package com.virgo.mapper4demo.dao;
import com.virgo.mapper4demo.entity.TblUserTest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.BaseMapper;
import java.util.List;
/**
* @author zhaozha
* @date 2019/10/15 上午11:13
*/
@Mapper
@Component
public interface TblUserTestCustomizedMapper extends BaseMapper<TblUserTest> {
@Select("select * from tbl_user_test")
List<TblUserTest> testAnnotation();
}
複製代碼
批量操做我在自帶的方法中找到了一個,mapper:tk.mybatis.mapper.common.special.InsertListMapper,對應的provider:tk.mybatis.mapper.provider.SpecialProvider,按照官方拓展方法的意思,咱們能夠尋找一個方法相近的作自定義sql.git
package com.virgo.dao.mapper;
import com.virgo.dao.provider.InsertListProvider;
import com.virgo.dao.provider.UpdateListProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import tk.mybatis.mapper.annotation.RegisterMapper;
import java.util.List;
/**
* @author zhaozha
* @date 2019/10/30 下午12:58
*/
@RegisterMapper
public interface InsertListMapper<T> {
@Options(
useGeneratedKeys = true
)
@InsertProvider(
type = InsertListProvider.class,
method = "dynamicSQL"
)
int insertList(List<? extends T> var1);
}
package com.virgo.dao.provider;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import java.util.Iterator;
import java.util.Set;
/**
* @author zhaozha
* @date 2019/10/30 下午1:00
*/
public class InsertListProvider extends MapperTemplate {
public InsertListProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
super(mapperClass, mapperHelper);
}
public String insertList(MappedStatement ms) {
Class<?> entityClass = this.getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append("<bind name=\"listNotEmptyCheck\" value=\"@tk.mybatis.mapper.util.OGNL@notEmptyCollectionCheck(list, '" + ms.getId() + " 方法參數爲空')\"/>");
sql.append(SqlHelper.insertIntoTable(entityClass, this.tableName(entityClass), "list[0]"));
sql.append(SqlHelper.insertColumns(entityClass, false, false, false));
sql.append(" VALUES ");
sql.append("<foreach collection=\"list\" item=\"record\" separator=\",\" >");
sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">");
Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
Iterator var5 = columnList.iterator();
while (var5.hasNext()) {
EntityColumn column = (EntityColumn) var5.next();
if (column.isInsertable()) {
sql.append(column.getColumnHolder("record") + ",");
}
}
sql.append("</trim>");
sql.append("</foreach>");
EntityHelper.setKeyProperties(EntityHelper.getPKColumns(entityClass), ms);
return sql.toString();
}
}
複製代碼
package com.virgo.dao.mapper;
import com.virgo.dao.provider.UpdateListProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.UpdateProvider;
import tk.mybatis.mapper.annotation.RegisterMapper;
import java.util.List;
/**
* @author zhaozha
* @date 2019/10/30 上午10:13
*/
@RegisterMapper
public interface UpdateListMapper<T> {
@Options(
useGeneratedKeys = true
)
@UpdateProvider(
type = UpdateListProvider.class,
method = "dynamicSQL"
)
int updateList(List<? extends T> var1);
}
package com.virgo.dao.provider;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import java.util.Iterator;
import java.util.Set;
/**
* @author zhaozha
* @date 2019/10/30 上午10:13
*/
public class UpdateListProvider extends MapperTemplate {
public UpdateListProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
super(mapperClass, mapperHelper);
}
public String updateList(MappedStatement ms) {
Class<?> entityClass = this.getEntityClass(ms);
StringBuilder sql = new StringBuilder();
sql.append("<bind name=\"listNotEmptyCheck\" value=\"@tk.mybatis.mapper.util.OGNL@notEmptyCollectionCheck(list, '" + ms.getId() + " 方法參數爲空')\"/>");
sql.append("<foreach collection=\"list\" item=\"record\" separator=\";\" >");
sql.append(SqlHelper.updateTable(entityClass, this.tableName(entityClass), "list[0]"));
sql.append("<set>");
Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
Iterator var5 = columnList.iterator();
while (var5.hasNext()) {
EntityColumn column = (EntityColumn) var5.next();
if (!column.isId() && column.isInsertable()) {
sql.append(column.getColumn() + "=" + column.getColumnHolder("record") + ",");
}
}
sql.append("</set>");
sql.append("<where>");
Set<EntityColumn> columnSet = EntityHelper.getPKColumns(entityClass);
Iterator var6 = columnSet.iterator();
while (var6.hasNext()) {
EntityColumn column = (EntityColumn) var6.next();
sql.append(" AND ").append(column.getColumnEqualsHolder("record"));
}
sql.append("</where>");
sql.append("</foreach>");
EntityHelper.setKeyProperties(EntityHelper.getPKColumns(entityClass), ms);
return sql.toString();
}
}
複製代碼
package com.virgo.dao.mapper;
import tk.mybatis.mapper.annotation.RegisterMapper;
/**
* @author zhaozha
* @date 2019/10/30 下午1:03
*/
@RegisterMapper
public interface QueryMapper<T> extends InsertListMapper<T>, UpdateListMapper<T> {
}
複製代碼
public interface TblRoleMenuMapper extends Mapper<TblRoleMenu>, QueryMapper<TblRoleMenu> {
}
複製代碼
批量插入 github
批量更新web
須要jdbc的url補上allowMultiQueries=true,容許一次執行多條sql。另外的批量更新還有另一種語法spring
UPDATE tbl_role_menu
SET status = CASE role_menu_id
WHEN 639138503447281697 THEN 0
WHEN 639138503447282721 THEN 1
END,
status = CASE role_menu_id
WHEN 639138503447281697 THEN 1
WHEN 639138503447282721 THEN 0
END
WHERE role_menu_id IN (639138503447281697,639138503447282721)
複製代碼
讀者能夠放開springboot的日誌,檢查本身的sql。sql
logging:
level:
# dao層地址
com.virgo.mapper4demo.dao: debug
複製代碼
文章如有謬誤之處,但願廣大讀者指正,互相交流,共同提升。apache