<file_sep><?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>org.mapper</groupId>
    <artifactId>Mapper</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>


            <!--MBC逆向工程
                使用 : mvn mybatis-generator:generate 命令在pom.xml那一层的Terminal中运行
            -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>
                        <!-- basedir表示工程路径 -->
                        ${basedir}/src/main/resources/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.47</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>




    <dependencies>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <!--mybatis-spring-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.5</version>
        </dependency>
        <!--mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.5</version>
        </dependency>

        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.26</version>
        </dependency>
        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <!--性能分析插件-->
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.9.1</version>
        </dependency>

    </dependencies>

</project><file_sep>package com.mapper.provider;

import org.apache.ibatis.mapping.MappedStatement;
import org.springframework.expression.spel.ast.NullLiteral;
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.Set;

/**
 * @author lijichen
 * @date 2020/12/12 - 20:21
 */
public class MyBatchUpdateProvider extends MapperTemplate {
    public MyBatchUpdateProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 该方法的名字必须与:MyBatchUpdateMapper接口中的:batchUpdate()方法名一致
     *           @UpdateProvider(type = MyBatchUpdateProvider.class,method = "dynamicSQL")
     *           void batchUpdate(List<T> list);
     * @return 必须返回当前的字符串,否则会报错,找不到该方法的错
     * @param statement
     */
    public String batchUpdate(MappedStatement statement){
        StringBuilder builder = new StringBuilder();
        builder.append("<foreach collection=\"list\" item=\"record\" separator=\";\">");

        Class<?> entityClass = super.getEntityClass(statement);

        String tableName = super.tableName(entityClass);

        //生成update
        String updateClause = SqlHelper.updateTable(entityClass, tableName);

        builder.append(updateClause);

        builder.append("<set>");

        Set<EntityColumn> columns = EntityHelper.getColumns(entityClass);

        String idColumn = null;
        String idHolder = null;

        //处理修改字段
        for (EntityColumn column : columns) {

            boolean isPrimaryKey = column.isId();
            if (isPrimaryKey) {
                idColumn = column.getColumn();
                idHolder = column.getColumnHolder("record");
                continue;
            }

            String column1 = column.getColumn();
            //获取返回格式是:#{entityName.age,jdbcType=NUMERIC,typeHandler=MyTypeHandler}的字符串
            String columnHolder = column.getColumnHolder("record");
            builder.append(column1).append("=").append(columnHolder).append(",");
        }

        builder.append("</set>");

        //拼接上where语句
        builder.append("where ").append(idColumn).append("=").append(idHolder);

        builder.append("</foreach>");

        return builder.toString();
    }
}
<file_sep>import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.generator.dao.EmployeeMapper;
import org.generator.pojo.Employee;
import org.junit.Test;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

/**
 * @author lijichen
 * @date 2020/12/12 - 19:49
 */
public class TestMGB {
    @Test
    public void test() {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(TestMGB.class.getClassLoader().getResourceAsStream("mybatis-config.xml"));

        SqlSession sqlSession = build.openSession();

        //需要 通用mapper的MapperHelper处理一下sqlSession
        //不然会报错
        MapperHelper mapperHelper = new MapperHelper();
        Configuration configuration = sqlSession.getConfiguration();
        mapperHelper.processConfiguration(configuration);
        //============

        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

        System.out.println(mapper.selectAll());
    }
}
<file_sep>package com.mapper.dao;

import com.mapper.mymapper.MyMapper;
import com.mapper.pojo.Employee;
import org.apache.ibatis.annotations.CacheNamespace;
import tk.mybatis.mapper.common.Mapper;

/**
 * @author lijichen
 * @date 2020/12/12 - 15:33
 */
/*
* 使用通用mapper继承mapper接口
* */
@CacheNamespace//该mapper使用二级缓存
public interface EmployeeMapper extends Mapper<Employee>, MyMapper<Employee> {
}
<file_sep>package com.mapper.service;

import com.mapper.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lijichen
 * @date 2020/12/12 - 15:34
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

}
<file_sep>package org.generator.pojo;

import javax.persistence.*;

@Table(name = "tb_employee")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "last_name")
    private String lastName;

    private String gender;

    private String email;

    @Column(name = "dept_id")
    private Integer deptId;

    @Column(name = "emp_state")
    private String empState;

    private Integer version;

    @Column(name = "logic_flag")
    private Integer logicFlag;

    /**
     * @return id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return last_name
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return dept_id
     */
    public Integer getDeptId() {
        return deptId;
    }

    /**
     * @param deptId
     */
    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    /**
     * @return emp_state
     */
    public String getEmpState() {
        return empState;
    }

    /**
     * @param empState
     */
    public void setEmpState(String empState) {
        this.empState = empState;
    }

    /**
     * @return version
     */
    public Integer getVersion() {
        return version;
    }

    /**
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * @return logic_flag
     */
    public Integer getLogicFlag() {
        return logicFlag;
    }

    /**
     * @param logicFlag
     */
    public void setLogicFlag(Integer logicFlag) {
        this.logicFlag = logicFlag;
    }
}