Requirement scenario: When some of the data saved in the database needs to be encrypted and the page needs to be displayed normally. This requires us to customize the type converter. When Mybatis executes SQL to get the result, CHAR or VARCHAR2 is encrypted and decrypted through the custom type converter. The Java code is as follows:
/**Custom typeHandler<br/> * 1 Insert the database, encrypt * 2 queries, decrypt * @author Administrator * */ public class CryptTypeHandler implements TypeHandler<CryptType> { public CryptType getResult(ResultSet rs, String columnName) throws SQLException { String value=""; CryptType v=new CryptType(value); value=rs.getString(columnName); if(value!=null){ value=decrypt(value.toString()); v.setValue(value); } return v; } public CryptType getResult(ResultSet rs, int columnIndex) throws SQLException { String value=""; CryptType v=new CryptType(value); value =rs.getString(columnIndex); if(value!=null){ v.setValue(value); } return v; } public CryptType getResult(CallableStatement cs, int columnIndex) throws SQLException { String value=""; CryptType v=new CryptType(); value =cs.getString(columnIndex); if(value!=null){ v.setValue(value); } return v; } public void setParameter(PreparedStatement ps, int i, CryptType parameter, JdbcType arg3) throws SQLException { String value=""; if(parameter!=null && parameter.toString()!=null){ value=encrypt(parameter.toString()); } ps.setString(i, value.toString()); } /**Insert the database* @param value * @return */ private String encrypt(String value) { value=CryptUtils.encrypt(value); return value; } /** Read from the database* @param value * @return */ private String decrypt(String value){ value=CryptUtils.decrypt(value); return value; } }Custom Type
import java.io.Serializable; /** * Custom type* The entity attribute defined as this type will be encrypted and decrypted by CryptTypeHandler.java* * @author yy * */ public class MyString implements Serializable, CharSequence, Comparable<String>{ private static final long serialVersionUID = 1L; private String value; public MyString (){ } public CryptType(String value){ this.value=value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public int compareTo(String arg0) { // TODO Auto-generated method stub return 0; } public char charAt(int arg0) { // TODO Auto-generated method stub return 0; } public int length() { // TODO Auto-generated method stub return 0; } public CharSequence subSequence(int arg0, int arg1) { // TODO Auto-generated method stub return null; } @Override public String toString() { return value; } }mybatis custom type configuration
<!-- Custom Type--> <typeHandlers> <typeHandler javaType="com.***.MyString" handler="com.***.MyTypeHandler"/> </typeHandlers>
Used in entities
public class LoanEnterprise{ private MyString name; //Note: //If the page has query conditions and is also encrypted, the conditional judgment in mybatis sql will not match. A temporary solution is to use public CryptType getName() { if(name!=null && name.getValue().equals("")){ return null; }else { return name; } } public void setName(CryptType name) { this.name = name; } }The above is the MyBatis custom type converter introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!