MyBatis数据库字段该如何映射Java枚举
发表于:2024-10-20 作者:千家信息网编辑
千家信息网最后更新 2024年10月20日,本篇内容主要讲解"MyBatis数据库字段该如何映射Java枚举",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"MyBatis数据库字段该如何映射Java枚
千家信息网最后更新 2024年10月20日MyBatis数据库字段该如何映射Java枚举
本篇内容主要讲解"MyBatis数据库字段该如何映射Java枚举",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"MyBatis数据库字段该如何映射Java枚举"吧!
有时候我们需要将数据库的字段对Java的枚举类型进行映射,比如说我们有一个汽车配件类型的枚举
public enum ProductType implements Localisable {TYPE1("配件"), TYPE2("车品"); private String value; private ProductType(String value) {this.value = value; }@Override public String getValue() {return this.value; }}
该枚举类型实现了一个接口
public interface Localisable { String getValue();}
有一个配件分类的实体类,包含了该枚举字段(此处只包含部分字段属性)
/** * 配件分类 */@AllArgsConstructor@NoArgsConstructorpublic class ProviderProductLevel {@Getter @Setter private Long id; @Getter @Setter private String code; @Getter @Setter private String name; @Getter @Setter private Integer sort; @Getter @Setter private Integer level; @Getter @Setter private ProductType productType; @Getter @Setter private String pictureUrl;}
而在数据库中的表结构如下
dao方法如下(通过id查找一个配件分类,并实例化)
@Mapperpublic interface LevelDao { ProviderProductLevel findLevel1(Long id);}
mapper映射文件如下
我们可以看到这里有一个映射处理器typeHandler="com.cloud.productprovider.untils.DbEnumTypeHandler"
该映射处理器的代码如下
@AllArgsConstructorpublic class DbEnumTypeHandler extends BaseTypeHandler{private Class type; @Override public void setNonNullParameter(PreparedStatement ps, int i, Localisable parameter, JdbcType jdbcType) throws SQLException { ps.setString(i,parameter.getValue()); }@Override public Localisable getNullableResult(ResultSet rs, String columnName) throws SQLException { String value = rs.getString(columnName); if (rs.wasNull()) {return null; }else {return convert(value); } }@Override public Localisable getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String value = rs.getString(columnIndex); if (rs.wasNull()) {return null; }else {return convert(value); } }@Override public Localisable getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String value = cs.getString(columnIndex); if (cs.wasNull()) {return null; }else {return convert(value); } }private Localisable convert(String value) { Localisable[] dbEnums = type.getEnumConstants(); for (Localisable dbEnum : dbEnums) {if (dbEnum.getValue().equals(value)) {return dbEnum; } }return null; }}
经测试返回的结果对象的Json字符串如下
{"code":"0000001","id":1,"name":"油品","productType":"TYPE1","sort":1}
到此,相信大家对"MyBatis数据库字段该如何映射Java枚举"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
字段
数据
数据库
配件
类型
分类
内容
处理器
方法
处理
学习
实用
更深
代码
兴趣
字符
字符串
实体
实例
实用性
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
企业谷软件开发
北京微租共享网络技术有限公司
win7装载数据库慢
java怎么连数据库
汽车电子软件开发规范
计算机网络技术管理答案
软件开发属什么学校
网络安全分析推理言情
国外大学的毕业论文数据库
虹口区个人软件开发哪个好
用qt向数据库添加信息
gccp6.0有没有本地数据库
阿里 代理服务器
软件开发与工艺技术结合
软件开发设计项目概算表
中国工业网络安全标准
兆言网络技术
云服务器可以转让么
珠海通讯软件开发零售价
山东网络安全大赛名单
该服务器的安全证书不信任
塔城网络技术调试
删除数据库中的第几行数据库
北京应用软件开发怎么样
绍兴海马网络技术有限公
软件开发培训电话咨询
信息化网络技术服务
手机如何用云服务器下载歌曲
吉林智慧养老软件开发电话
不限流量vps服务器