mybatis-通用mapper

自定义通用mapper

由于mapper做了分层结构,我们的mapper接口可不继承Mapper,而是有选择性的使用需要用到的封装好的相关方法。即自定义通用mapper
封装好的方法

配置数据库

spring:
    datasource:
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8&&serverTimezone=GMT&&allowMultiQueries=true
        driver-class-name: com.mysql.jdbc.Driver

allowMultiQueries=true 允许一次执行多行sql语句

1-编写自定义通用mapper接口并选择性的继承封装好的方法。

public interface MyMapper<T> extends
        SelectAllMapper<T>,
        SelectByExampleMapper<T>{

}

2-个人mapper接口继承自定义通用mapper

public interface EmployeeMapper extends MyMapper<Employee>{
}

3-测试

@Autowired
EmployeeMapper employeeMapper;

@RequestMapping(value = "/selectByExample",method = RequestMethod.GET)
public  List<Employee> selectByExample(Employee employee) {
    // 创建example对象
    Example example = new Example(Employee.class);
    //通过example设置查询条件
    Example.Criteria criteria1 = example.createCriteria();
    Example.Criteria criteria2 = example.createCriteria();

    criteria1.andGreaterThan("empSalary",3000)
            .andLessThan("empAge",25);
    criteria2.andLessThan("empSalary",5000)
            .andGreaterThan("empAge",30);
    example.or(criteria2);// 组装查询条件
    example.orderBy("empSalary").asc().orderBy("empAge").desc();// 排序
    example.setDistinct(true);//去重
    // 执行查询
    List<Employee> employees = employeeMapper.selectByExample(example);
    return employees;
}

扩展通用mapper接口

个人mapper接口:普通dao层接口
通用mapper接口或自定义通用mapper接口:已封装好相关sql操作的接口,拿来即用那种
扩展通用mapper接口:个人自定义的sql操作接口

1-定义【扩展接口】,使用相关注解注释并指定type和method(值为dynamicSQL),如: @UpdateProvider 加上@RegisterMapper

@RegisterMapper
public interface MyBatchUpdateMapper<T> {
    @UpdateProvider(type = MyBatchUpdateProvider.class ,method = "dynamicSQL")
    void batchUpdate(List<T> list);
}

2-定义【接口方法实现类】,返回相关拼接好的sql语句

public class MyBatchUpdateProvider extends MapperTemplate{
    public MyBatchUpdateProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }
    /**
     * <foreach collection="list" item="record" separator=";" >
            UPDATE tabple_emp
            SET emp_name=#f{record.empName},
            emp_age=#{record.empAge},
            emp_salary=#{record.empSalary}
            where emp_id=#{record.empId}
        </foreach>
     * @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);
        String updateClause = SqlHelper.updateTable(entityClass, tableName);
        builder.append(updateClause);
        builder.append("<set>");
        Set<EntityColumn> columns = EntityHelper.getColumns(entityClass);

        String idColumn = null;
        String idHodler = null;
        for (EntityColumn entityColumn : columns){
            boolean isPrimaryKey = entityColumn.isId();
            if (isPrimaryKey){
                idColumn = entityColumn.getColumn();
                idHodler = entityColumn.getColumnHolder("record");
            }else {
                String column = entityColumn.getColumn();
                String columnHolder = entityColumn.getColumnHolder("record");

                builder.append(column).append("=").append(columnHolder).append(",");
            }
        }
        builder.append("</set>");

        builder.append("WHERE ").append(idColumn).append("=").append(idHodler);
        builder.append("</foreach>");

        return builder.toString();
    }
}

3-【自定义通用mapper接口继承扩展接口】以及个人mapper接口继承自定义通用mapper接口

public interface MyMapper<T> extends
        SelectAllMapper<T>,
        SelectByExampleMapper<T> ,
        MyBatchUpdateMapper<T> {

}

public interface EmployeeMapper extends MyMapper<Employee>{}

或者【个人mapper接口直接继承扩展接口】

public interface EmployeeMapper extends MyMapper<Employee> ,
        MyBatchUpdateMapper<Employee> {

}

4-测试

@Autowired
EmployeeMapper employeeMapper;

@RequestMapping(value = "/batchUpdate",method = RequestMethod.GET)
public void batchUpdate() {
    List<Employee> employees = new ArrayList<>();
    employees.add(new Employee(4,"newName01",111.11,11));
    employees.add(new Employee(5,"newName02",222.22,22));
    employees.add(new Employee(6,"newName03",222.22,33));
    employeeMapper.batchUpdate(employees);
}
posted @ 2021-01-07 00:24  小艾影  阅读(196)  评论(0编辑  收藏  举报