mybatis mysql枚举类型_mybatis自定义类型处理器处理枚举类型

本文介绍了如何在MyBatis中自定义类型处理器(MyTypeHandle)以处理MySQL枚举类型,包括设置参数、获取结果集中的枚举值,并在User实体类中使用枚举属性。同时,详细展示了在UserMapper.xml中配置插入语句以及在全局配置文件中注册类型处理器的步骤。
摘要由CSDN通过智能技术生成

一、自定义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、新增时我们希望存入我们自定义的编码

f27230f91db6a4ec548e8eda49c9a0bd.png

2、查询时我们希望返回编码对应的枚举对象

984cd9558576b07ac99052da6774c581.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值