先创建一个枚举类型,如下:
public enum SexEnum {
man(0, "男"),
woman(1, "女");
private Integer code;
private String codeName;
SexEnum(Integer code, String codeName) {
this.code = code;
this.codeName = codeName;
}
public static SexEnum getSexEnumById(int sex) {
switch (sex) {
case 0:
return SexEnum.man;
case 1:
return SexEnum.woman;
default:
return null;
}
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getCodeName() {
return codeName;
}
public void setCodeName(String codeName) {
this.codeName = codeName;
}
}
再创建一个拥有该枚举属性的类UserInfo类,如下:
public class UserInfo {
private int id;
private String userName;
private int age;
private SexEnum sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public SexEnum getSex() {
return sex;
}
public void setSex(SexEnum sex) {
this.sex = sex;
}
@Override
public String toString() {
return "id=" + id + ",username=" + userName + ",age=" + age + ",sex=" + sex;
}
}
接着定义一个mapper接口来访问数据库,如下:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springboot.codedemo.UserInfo;
import org.springframework.stereotype.Repository;
@Repository
public interface UserInfoDao extends BaseMapper<UserInfo> {
}
创建一个Controller类,定义一个接口来测试,如下:
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.mapper.UserInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/test")
public class ServiceController {
@Autowired
private UserInfoDao userInfoDao;
@GetMapping
public void getList() {
List<UserInfo> userInfos = userInfoDao.selectList(new QueryWrapper<>());
System.out.println(userInfos);
}
}
在spring boot的启动类SpringbootApplication上添加@MapperScan注解扫描mapper包,如下:
接着自定义一个SexTypeHandler类来做性别的枚举转换,如下:
package com.example.springboot.typehandler;
import com.example.springboot.codedemo.SexEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//指定数据库的类型
@MappedJdbcTypes(JdbcType.INTEGER)
//指定java类型
@MappedTypes(value = SexEnum.class)
public class SexTypeHandler extends BaseTypeHandler<SexEnum> {
/**
* 通过列名读取性别
*
* @param resultSet
* @param s
* @return
* @throws SQLException
*/
@Override
public SexEnum getNullableResult(ResultSet resultSet, String s) throws SQLException {
int sex = resultSet.getInt(s);
if (sex != 0 && sex != 1) {
return null;
}
return SexEnum.getSexEnumById(sex);
}
/**
* 通过下标读取性别
*
* @param resultSet
* @param i
* @return
* @throws SQLException
*/
@Override
public SexEnum getNullableResult(ResultSet resultSet, int i) throws SQLException {
int sex = resultSet.getInt(i);
if (sex != 0 && sex != 1) {
return null;
}
return SexEnum.getSexEnumById(sex);
}
/**
* 通过存储过程读取性别
*
* @param callableStatement
* @param i
* @return
* @throws SQLException
*/
@Override
public SexEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
int sex = callableStatement.getInt(i);
if (sex != 0 && sex != 1) {
return null;
}
return SexEnum.getSexEnumById(sex);
}
/**
* 设置非空的性别参数
*
* @param preparedStatement
* @param i
* @param sexEnum
* @param jdbcType
* @throws SQLException
*/
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, SexEnum sexEnum, JdbcType jdbcType) throws SQLException {
preparedStatement.setInt(i, sexEnum.getCode());
}
}
最后在application.yml配置文件里定义类型转换的文件夹以及xml文件夹的路径,如下:
数据库的USER_INFO表有如下数据:
重启服务,访问接口,结果如下:
[id=1,username=赵一杰,age=17,sex=man, id=100,username=钱二杰,age=70,sex=woman, id=110,username=孙三杰,age=100,sex=man, id=2,username=李五杰,age=18,sex=woman, id=3,username=吴六杰,age=30,sex=man, id=70,username=王七杰,age=70,sex=woman]
可以看到在数据库中为int类型的数据,顺利被转换成了枚举展示。
总结
可以发现自定义TypeHandler类的关键在于继承BaseTypeHandler类,重写其相应方法,然后通过@MappedJdbcTypes和@MappedTypes两个注解指定转换的类型,最后在配置文件中指定定义TypeHandler类的包路径即可。