Mybatis通用的Insert/Update

论坛 期权论坛 脚本     
匿名技术用户   2021-1-5 13:29   27   0

Mybatis写mapper时,每个表都需要INSERT UPDATE写很长的语句比较麻烦,80%的表和java bean是简单的一一对应关系,因此考虑使用mybatis的SQLProvider注解减少这部分重复劳动。
SELECT和DELETE语句由于涉及字段较少,写起来还在可接收范围内,所以没做通用的。

也可以把javabean增加写注解,类似JPA那样指定表名、字段名、主键等,但做得太多就失去了mybatis依赖SQL的灵活性了,而且自己写出来个hibernate/jpa,还不如直接用hibernate/jpa

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.ibatis.jdbc.SQL;
 
 
/**
 * 使用此类有几个默认规则: 表名和类名相对应,属性名和字段名对应
 * java类内用驼峰命名法;数据库表和字段都用下划线
 * 类的属性定义的变量名和get/set方法名,以及set的参数类型一致,才会映射到数据库字段
 * 例如: private String name; 而且有 public void setName(String name) 和 public String getName()方法
 * 如果不是严格按照此规则定义的属性,不会被影射到数据库字段上
 */
public class InsertUpdateSqlProvider {
 
    /**
     * id如果传入了值,会被insert使用;如果id为null,不会被insert的columns列出
     */
    public static String insert(Object obj) {
        Map<String, String> map;
        try {
            map = getFieldsMap(obj, true);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return new SQL() {
            {
                INSERT_INTO(getTableName(obj));
                for (String col : map.keySet()) {
                    VALUES(col, map.get(col));
                }
            }
        }.toString();
    }
     
    private static String updateById(Object obj, boolean includeNullValueField) {
        Map<String, String> map;
        try {
            map = getFieldsMap(obj, includeNullValueField);
            map.remove("id");
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return new SQL() {
            {
                UPDATE(getTableName(obj));
                for (String col : map.keySet()) {
                    SET(col + "=" + map.get(col));
                }
                WHERE("id = #{id}");
            }
        }.toString();
    }
 
    public static String updateById(Object obj) {
        return updateById(obj, true);
    }
 
    public static String updateNonNullById(Object obj) {
        return updateById(obj, false);
    }
 
    private static Map<String, String> getFieldsMap(Object obj, boolean includeNullValue)
            throws IllegalArgumentException, IllegalAccessException {
        HashMap<String, String> result = new HashMap<>();
        Class<?> cls = obj.getClass();
        Field[] fields = cls.getDeclaredFields(); // getDeclaredFields
        for (Field f : fields) {
            String col = f.getName();
            String colName = col.substring(0, 1).toUpperCase() + col.substring(1);
            f.setAccessible(true);
            try {
                cls.getMethod("get" + colName);
                cls.getMethod("set" + colName, f.getType());
            } catch (NoSuchMethodException | SecurityException e) {
                continue;
            }
            if ((!"id".equals(col) && includeNullValue) || f.get(obj) != null) {
                result.put(camelCase2Underscore(col), "#{" + col + "}");
            }
        }
        return result;
    }
 
    public static String getTableName(Object obj) {
        return camelCase2Underscore(obj.getClass().getSimpleName());
    }
 
    public static String camelCase2Underscore(String s) {
        StringBuffer buf = new StringBuffer();
        for (String w : s.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")) {
            buf.append("_").append(w.toLowerCase());
        }
        return buf.substring(1);
    }
}

mybatis Mapper类

import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.UpdateProvider;
import org.springframework.stereotype.Repository;
 
@Repository
public interface GenericDao {
 
    @UpdateProvider(type=InsertUpdateSqlProvider.class, method="updateById")
    public int updateById(Object bean);
     
    @UpdateProvider(type=InsertUpdateSqlProvider.class, method="updateNonNullById")
    public int updateNonNullById(Object bean);
     
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    @InsertProvider(type=InsertUpdateSqlProvider.class, method="insert")
    public int insert(Object bean);
     
    @InsertProvider(type=InsertUpdateSqlProvider.class, method="insert")
    public int insertWithoutGeneratedKey(Object bean);
}

原文

最后,做个小广告,免费课程:用Spring Boot编写RESTful API

http://study.163.com/course/introduction.htm?courseId=1005213034

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP