当前位置: 移动技术网 > IT编程>开发语言>Java > MyBatis自定义类型转换器实现加解密

MyBatis自定义类型转换器实现加解密

2019年07月22日  | 移动技术网IT编程  | 我要评论
需求场景:当数据库中保存的部分数据需要加密,页面需要正常显示时。这是就需要我们自定义类型转换器,在mybatis执行sql得到结果时,通过自定义类型转换器将char或者va

需求场景:当数据库中保存的部分数据需要加密,页面需要正常显示时。这是就需要我们自定义类型转换器,在mybatis执行sql得到结果时,通过自定义类型转换器将char或者varchar2进行加解密处理,java代码如下:

/**自定义typehandler<br/> 
* 1 插入数据库, 加密 
* 2 查询,解密 
* @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()); 
} 
/**插入数据库 
* @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); 
return value; 
} 
}

自定义类型

import java.io.serializable; 
/** 
* 自定义类型 
* 定义为该类型的实体属性会走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自定义类型配置

<!-- 自定义类型 --> 
<typehandlers> 
<typehandler javatype="com.***.mystring" handler="com.***.mytypehandler"/> 
</typehandlers>

实体中使用

public class loanenterprise{ 
private mystring name; 
//注意: 
//如果页面有查询条件也被加密时,mybatis sql中的条件判断会无法匹配,暂时的一种解决办法是在 
public crypttype getname() { 
if(name!=null && name.getvalue().equals("")){ 
return null; 
}else { 
return name; 
} 
} 
public void setname(crypttype name) { 
this.name = name; 
} 
} 

以上所述是小编给大家介绍的mybatis自定义类型转换器实现加解密,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网