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

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>
http://www.xdnf.cn/news/1100.html

相关文章:

  • Mysql 读写分离(3)之 schema.xml基本配置
  • 简化K8S部署流程:通过Apisix实现蓝绿发布策略详解(上)
  • 物联网 (IoT) 安全简介
  • 《开源大模型选型全攻略:开启智能应用新征程》
  • Ubuntu 上安装 Conda
  • Spark-Streaming核心编程
  • 深度剖析神经网络:从基础原理到面试要点(二)
  • 游戏引擎学习第239天:通过 OpenGL 渲染游戏
  • 三餐四季、灯火阑珊
  • Oracle DBA 高效运维指南:高频实用 SQL 大全
  • 极狐GitLab 项目功能和权限解读
  • 大数据学习(112)-HIVE中的窗口函数
  • Cursor这类编程Agent软件的模型架构与工作流程
  • Flink介绍——实时计算核心论文之Dataflow论文详解
  • OpenCV物体计数示例
  • Go语言和Python 3的协程对比
  • Spark-Streaming
  • Linux kernel signal原理(下)- aarch64架构sigreturn流程
  • Spring Boot 主模块 spring-boot 核心技术解析:从启动类到内嵌容器的无缝支持
  • dify工作流之text-2-e-sql,大模型写sql并执行
  • 深入理解 Spring @Configuration 注解
  • SpringBoot原生实现分布式MapReduce计算
  • Java高频面试之并发编程-05
  • 新能源汽车可视化大屏系统毕业设计
  • Node.js 模块导入的基本流程
  • 【Harmony】常用工具类封装
  • Jupyter Notebook 中切换/使用 conda 虚拟环境的方式(解决jupyter notebook 环境默认在base下面的问题)
  • Python爬虫从入门到实战详细版教程Char01:爬虫基础与核心技术
  • 【网络】代理服务器收尾及高级IO
  • vue项目中axios统一或单独控制接口请求时间