一、自定义MyTypeHandle实现TypeHandle接口
package com.mybatis.entity;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
/**
* 自定义类型处理器
* @author dgw
*
*/
public class MyTypeHandle implements TypeHandler{
@Override
public void setParameter(PreparedStatement ps, int i, UserGender parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter.getCode().toString());
}
@Override
public UserGender getResult(ResultSet rs, String columnName) throws SQLException {
//从数据库中得到的值
int code = rs.getInt(columnName);
//从数据库中得到的枚举状态码返回枚举对象
return UserGender.getUserGender(code);
}
@Override
public UserGender getResult(ResultSet rs, int columnIndex) throws SQLException {
//从数据库中得到的值
int code = rs.getInt(columnIndex);
//从数据库中得到的枚举状态码返回枚举对象
return UserGender.getUserGender(code);
}
@Override
public UserGender getResult(CallableStatement cs, int columnIndex) throws SQLException {
//从数据库中得到的值
int code = cs.getInt(columnIndex);
//从数据库中得到的枚举状态码返回枚举对象
return UserGender.getUserGender(code);
}
}
二、自定义枚举类型
package com.mybatis.entity;
public enum UserGender {
MAN(100,"男人"),WEMEN(38,"女人");
private Integer code;
private String msg;
private UserGender(Integer code,String msg){
this.code=code;
this.msg=msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public static UserGender getUserGender(Integer code){
switch (code) {
case 100:
return MAN;
case 38:
return WEMEN;
default:
return WEMEN;
}
}
}
三、在User类中添加枚举属性
package com.mybatis.entity;
public class User {
private Integer id;
private String name;
private Integer age;
private String address;
private Boolean isDelete;
private UserGender gender;
public User() {
super();
}
public User(Integer id, String name, Integer age, String address, Boolean isDelete, UserGender gender) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.isDelete = isDelete;
this.gender = gender;
}
public User(Integer id, String name, Integer age, String address, Boolean isDelete) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.isDelete = isDelete;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Boolean getIsDelete() {
return isDelete;
}
public void setIsDelete(Boolean isDelete) {
this.isDelete = isDelete;
}
public UserGender getGender() {
return gender;
}
public void setGender(UserGender gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", isDelete=" + isDelete
+ ", gender=" + gender + "]";
}
}
四、在UserMapper.xml中写添加方法
insert into user(name,age,is_delete,gender) value(#{name},#{age},#{isDelete},#{gender});
五、在全局配置文件中注册类型处理器
六、测试新增和查询时效果
1、新增时我们希望存入我们自定义的编码
2、查询时我们希望返回编码对应的枚举对象