Requirements Scenario: When some of the data stored in the database needs to be encrypted, the page needs to be displayed normally. That's what we need. A custom type converter that decrypts char or VARCHAR2 through a custom type converter when MyBatis executes SQL results, the Java code is as follows:
/** Custom typehandler<br/> * 1 Insert Database, encrypt * 2 query, Decrypt * @author Administrator */public class Crypttypehandler Imp
Lements 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 Database * @param value * @return/private string Encrypt (string value) {Value=cryptutils.encrypt (value);
return value;
/** * @param value * @return/private string decrypt (string value) {Value=cryptutils.decrypt (value) is read from the database;
return value; }
}
Custom type
Import java.io.Serializable;
/**
* Custom type
* defined for the type of entity attributes will go Crypttypehandler.java do add decryption processing
* *
@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 a query condition that is also encrypted, the conditional judgment in MyBatis SQL will not match, and a temporary solution is to
crypttype GetName () {
if () {if name!=null && name.getvalue (). Equals ("")) {return
null;
} else {return
name;
}
}
public void SetName (crypttype name) {
this.name = name;
}
The above is a small set to introduce the MyBatis custom type converter to achieve encryption and decryption, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!