chenjiahe
2022-01-18 93b505ef316dcb0b22bac49aea975a8302dcb3ec
新增数据库统一加密工具
3个文件已添加
4个文件已修改
173 ■■■■■ 已修改文件
src/main/java/com/hx/common/dao/CommonDao.java 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/common/dao/CommonMapper.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/common/service/CommonService.java 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/common/service/impl/CommonDaoImpl.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/common/service/impl/CommonServiceImpl.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/common/xml/CommonMapper.xml 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java 108 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/common/dao/CommonDao.java
@@ -103,5 +103,12 @@
     * @return 返回条数
     */
    <T extends Serializable> int deleteById(Class<?> mapperClass, Object object);
    /**更新sql语句(全语句)
     * @param sqlSentence 查询参数类
     * @return 返回条数
     */
    <T extends Serializable> int updateSentence( SqlSentence sqlSentence);
}
src/main/java/com/hx/common/dao/CommonMapper.java
New file
@@ -0,0 +1,17 @@
package com.hx.common.dao;
import com.hx.mybatisTool.SqlSentence;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
public interface CommonMapper {
    /**更新,返回更新数量*/
    int updateSentence(SqlSentence sqlSentence);
    /**查询列表,返回Map的List*/
    List<Map<String,Object>> selectListMap(SqlSentence sqlSentence);
}
src/main/java/com/hx/common/service/CommonService.java
@@ -104,5 +104,12 @@
     * @return 返回条数
     */
    <T extends Serializable> int deleteById(Class<?> mapperClass, Object object);
    /**更新sql语句(全语句)
     * @param sqlSentence 查询参数类
     * @return 返回条数
     */
    <T extends Serializable> int updateSentence(SqlSentence sqlSentence);
}
src/main/java/com/hx/common/service/impl/CommonDaoImpl.java
@@ -1,6 +1,7 @@
package com.hx.common.service.impl;
import com.hx.common.dao.CommonDao;
import com.hx.common.dao.CommonMapper;
import com.hx.mybatisTool.SqlSentence;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
@@ -102,4 +103,11 @@
        return sqlSessionFactory.openSession().delete(getStatement(mapperClass,"deleteById"),object);
    }
    /**更新sql语句(全语句)*/
    @Override
    public <T extends Serializable> int updateSentence(SqlSentence sqlSentence) {
        return sqlSessionFactory.openSession().delete(getStatement(CommonMapper.class,"updateSentence"),sqlSentence);
    }
}
src/main/java/com/hx/common/service/impl/CommonServiceImpl.java
@@ -97,4 +97,12 @@
        return commonDao.deleteById(mapperClass,object);
    }
    /**更新sql语句(全语句)*/
    @Override
    public <T extends Serializable> int updateSentence(SqlSentence sqlSentence) {
        return commonDao.updateSentence(sqlSentence);
    }
}
src/main/java/com/hx/common/xml/CommonMapper.xml
New file
@@ -0,0 +1,14 @@
<?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">
<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="com.hx.common.dao.CommonMapper">
    <update id="updateSentence" parameterType="com.hx.mybatisTool.SqlSentence" >
         ${sqlSentence}
    </update>
    <select id="selectListMap" resultType="java.util.Map" parameterType="com.hx.mybatisTool.SqlSentence" >
        ${sqlSentence}
    </select>
</mapper>
src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
New file
@@ -0,0 +1,108 @@
package com.hx.mybatis.aes.springbean;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.hx.common.annotations.MysqlHexAes;
import com.hx.common.dao.CommonMapper;
import com.hx.common.service.CommonService;
import com.hx.exception.ServiceException;
import com.hx.mybatisTool.SqlSentence;
import com.hx.util.StringUtils;
import com.hx.util.mysql.aes.MysqlHexAesTool;
import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class InitMysqlData {
    /**
     * 项目启动就执行后就执行该方法
     */
    @PostConstruct
    public static void initData(String packPath, CommonService commonService){
        //项目启动的时候填入
        if(!StringUtils.isEmpty(packPath)){
            Set<Class<?>> classes = VariableAesKey.classData(packPath);
            Map<String,String> aesKeysFild = new HashMap<>();
            boolean isAes = false;
            String tableName = null;
            String fildName = null;
            String fildValue = null;
            SqlSentence sqlSentence = new SqlSentence();
            Map<String,Object> values = new HashMap<>();
            for(Class<?> cl:classes){
                //表名称
                boolean hasAnnotation = cl.isAnnotationPresent(Table.class);
                if(!hasAnnotation){
                    continue;
                }
                Table table = cl.getAnnotation(Table.class);
                tableName = table.name();
                aesKeysFild = new HashMap<>();
                isAes = false;
                // 取得本类的全部属性
                Field[] fields = cl.getDeclaredFields();
                fields = VariableAesKey.getPatentFields(fields,cl);
                for (Field field:fields) {
                    fildName = null;
                    // 判断方法中是否有指定注解类型的注解
                    hasAnnotation = field.isAnnotationPresent(MysqlHexAes.class);
                    if (hasAnnotation) {
                        // 根据注解类型返回方法的指定类型注解
                        MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
                        //String aesKeyField = mysqlHexAes.aesKeyField();
                        String aesKey = mysqlHexAes.aesKey();
                        if(StringUtils.isEmpty(aesKey)){
                            aesKey = VariableAesKey.AES_KEY;
                            if(StringUtils.isEmpty(aesKey)){
                                throw new RuntimeException("mysql的AES秘钥不能为空:"+field.getName());
                            }
                        }
                        hasAnnotation = field.isAnnotationPresent(Column.class);
                        if(hasAnnotation){
                            Column column = field.getAnnotation(Column.class);
                            fildName = column.name();
                        }
                        if(StringUtils.isEmpty(fildName)){
                            fildName = field.getName();
                        }
                        sqlSentence.sqlSentence("SELECT id,"+fildName+" FROM "+tableName,values);
                        List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
                        for(Map<String,Object> map:list){
                            fildValue = (String)map.get(fildName);
                            System.out.println("fildValue:"+fildValue);
                            if(StringUtils.isEmpty(fildValue)){
                                continue;
                            }
                            if(fildValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(fildValue)){
                                continue;
                            }
                            values.clear();
                            values.put("id",map.get("id"));
                            values.put("filedData",fildValue);
                            sqlSentence.sqlSentence("UPDATE "+tableName+" SET "+fildName+" = #{m.filedData} WHERE id = #{m.id}",values);
                            if(commonService.updateSentence(sqlSentence)!=1){
                                throw new ServiceException("更新超过1条,更新失败!");
                            }
                        }
                    }
                }
            }
        }
    }
}