当前位置: 首页 > news >正文

mybatis mapper.xml中使用枚举

重点:application.propertis配置类

#TypeEnumHandler 这个类的包名,不是全路径
mybatis.type-handlers-package=com.fan.test.handler

两个枚举类:

public enum StatusEnum {DELETED(0),ACTIVE(1);private final int code;StatusEnum(int code) {this.code = code;}public int getCode() {return code;}
}
public enum TypeEnum {ONE(1, "one"),TWO(2, "two"),THREE(3, "three");private int code;private String desc;TypeEnum(int code, String desc) {this.code = code;this.desc = desc;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getDesc() {return desc;}public static TypeEnum fromCode(int code) {for (TypeEnum type : values()) {if (type.code == code) {return type;}}throw new IllegalArgumentException("Invalid TypeEnum code: " + code);}
}

实体类:

@Data
public class User {private Long id;private TypeEnum type;private String username;private String password;
}

handler 转换类【TypeEnumHandler】

@MappedTypes(TypeEnum.class)
@MappedJdbcTypes(JdbcType.INTEGER)
public class TypeEnumHandler implements TypeHandler<TypeEnum> {@Overridepublic void setParameter(PreparedStatement ps, int i, TypeEnum parameter, JdbcType jdbcType) throws SQLException {System.out.println("TypeHandler called with value: " + parameter);ps.setInt(i, parameter.getCode());}@Overridepublic TypeEnum getResult(ResultSet rs, String columnName) throws SQLException {return TypeEnum.fromCode(rs.getInt(columnName));}@Overridepublic TypeEnum getResult(ResultSet rs, int columnIndex) throws SQLException {return TypeEnum.fromCode(rs.getInt(columnIndex));}@Overridepublic TypeEnum getResult(CallableStatement cs, int columnIndex) throws SQLException {return TypeEnum.fromCode(cs.getInt(columnIndex));}
}

mapper接口

@Mapper
public interface UserMapper {List<User> selectAll();void insertUser(User user);
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="UserMapper"><!--查询(自动映射)--><select id="selectAll" resultType="com.fan.test.entity.User"><bind name="ACTIVE" value="@com.fan.test.enums.StatusEnum@ACTIVE.code"/>SELECT * FROM user WHERE status = #{ACTIVE}</select><!--插入(自动传入枚举的 code)--><insert id="insertUser" parameterType="com.fan.test.entity.User">INSERT INTO user (type, username, password)VALUES (#{type}, #{username}, #{password})</insert>
</mapper>

这里枚举转换还有其他两种写法:
第一种:

 @TypeHandler(TypeEnumHandler.class)private TypeEnum type;

第二种:

<insert id="insertUser" parameterType="com.fan.test.entity.User">INSERT INTO user (type, username, password)VALUES (#{type, typeHandler=com.fan.test.handler.TypeEnumHandler}, #{username}, #{password})
</insert>

相关文章:

  • AVX2与onnx量化加速
  • 【MCP Node.js SDK 全栈进阶指南】中级篇(4):MCP错误处理与日志系统
  • Python SQL 工具包:SQLAlchemy介绍
  • UML 状态图:以共享汽车系统状态图为例
  • osxcross 搭建 macOS 交叉编译环境
  • 【数据结构】励志大厂版·初级(二刷复习)双链表
  • Mongodb分布式文件存储数据库
  • NineData 与飞书深度集成,企业级数据管理审批流程全面自动化
  • IDEA热加载
  • 逐位逼近法计算对数的小数部分
  • SpringClound 微服务分布式Nacos学习笔记
  • Docker--Docker网络原理
  • day35图像处理OpenCV
  • Java面向对象的三大特性
  • ClickHouse 设计与细节
  • Python 设计模式:模板模式
  • 安宝特方案 | 医疗AR眼镜,重新定义远程会诊体验
  • Qt -对象树
  • CSS预处理器对比:Sass、Less与Stylus如何选择
  • 操作系统之shell实现(下)
  • 五大光伏龙头一季度亏损超80亿元,行业冬天难言结束
  • 魔都眼|静安光影派对五一启幕:苏河湾看徐悲鸿艺术画作
  • 锦江酒店:第一季度营业收入约29.42亿元,境内酒店出租率同比增长
  • 解密62个“千亿县”:强者恒强,新兴产业助新晋县崛起
  • 中国银行副行长刘进任该行党委副书记
  • 国家统计局:一季度全国规模以上文化及相关产业企业营业收入增长6.2%