1、新建类xxxTypeHandler,实现TypeHandler接口 或 继承 BaseTypeHandler类
package com.william.typehandler;
import com.william.mapper.IdCardType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Base64;
/**
* 身份证加密解密类型转换器
*/
public class EncryptTypeHandler implements TypeHandler<IdCardType> {
@Override
public void setParameter(PreparedStatement preparedStatement, int i, IdCardType idCardType, JdbcType jdbcType) throws SQLException {
String cryptIdCard = Base64.getEncoder().encodeToString(idCardType.getIdCard().getBytes());
preparedStatement.setString(i, cryptIdCard);
}
@Override
public IdCardType getResult(ResultSet resultSet, String s) throws SQLException {
String cryptIdCard = resultSet.getString(s);
String idCard = new String(Base64.getDecoder().decode(cryptIdCard));
IdCardType idCardType = new IdCardType(idCard);
return idCardType;
}
@Override
public IdCardType getResult(ResultSet resultSet, int i) throws SQLException {
String cryptIdCard = resultSet.getString(i);
String idCard = new String(Base64.getDecoder().decode(cryptIdCard));
IdCardType idCardType = new IdCardType(idCard);
return idCardType;
}
@Override
public IdCardType getResult(CallableStatement callableStatement, int i) throws SQLException {
String cryptIdCard = callableStatement.getString(i);
String idCard = new String(Base64.getDecoder().decode(cryptIdCard));
IdCardType idCardType = new IdCardType(idCard);
return idCardType;
}
}
2、在Mybatis-config.xml中注册类型转换器
<typeHandlers>
<typeHandler handler="com.william.typehandler.EncryptTypeHandler" javaType="IdCardType"></typeHandler>
</typeHandlers>
3、直接使用
package com.william;
import com.william.mapper.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Data
public class Main {
static public void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
System.out.println(inputStream.toString());
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession(true);
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student = new Student();
student.setAddress("Beijing");
student.setAge(99);
student.setName("FUCK");
student.setPassword("123123");
student.setIdCardType(new IdCardType("this is unencryptedPassword!"));
mapper.insertTestTypeHandler(student);
System.out.printf("自增主键为: %d\n", student.getId());
student = mapper.selectTestTypeHandler(4);
System.out.println(student);
}
}
结果:
==> Preparing: INSERT INTO students(`password`, `age`, `salary`, `address`, `name`, `idcard`) VALUES (?, ?, ?, ?, ?, ?)
==> Parameters: 123123(String), 99(Integer), null, Beijing(String), FUCK(String), dGhpcyBpcyB1bmVuY3J5cHRlZFBhc3N3b3JkIQ==(String)
<== Updates: 1
自增主键为: 9
==> Preparing: select * from students where id=?
==> Parameters: 4(Integer)
<== Columns: id, password, age, salary, address, name, idcard
<== Row: 4, 123123, 99, null, Beijing, FUCK, dGhpcyBpcyB1bmVuY3J5cHRlZFBhc3N3b3JkIQ==
<== Total: 1
Student{idCardType=IdCardType(idCard=this is unencryptedPassword!), roles=null, id=4, password='123123', age=99, salary=null, address='Beijing', name='FUCK'}