最近在搭建maven多模块项目: http://git.oschina.net/alexgaoyh/MutiModule-parent
项目整体代码部分在上文链接的git中,下面的代码段落只是部分个人觉得比较重要的段落
在选择持久化DAO部分的时候,选择了mybatis,实体中变量的定义,想来来使用Enum代替常量的某些好处。所以才有下文的这些功能:
package com.alexgaoyh.MutiModule.domain; import java.io.Serializable; import com.alexgaoyh.MutiModule.domain.enumState.DeleteFlagState; public class BaseEntity implements Serializable{ /** * */ private static final long serialVersionUID = 997102449095543774L; /** * 编号,主键标示 */ private Integer id; /** * 是否删除状态 */ private DeleteFlagState deleteFlagState = DeleteFlagState.NO; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public DeleteFlagState getDeleteFlagState() { return deleteFlagState; } public void setDeleteFlagState(DeleteFlagState deleteFlagState) { this.deleteFlagState = deleteFlagState; } }
package com.alexgaoyh.MutiModule.persist.handler; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import com.alexgaoyh.MutiModule.domain.enumState.DeleteFlagState; /** * 自定义DeleteFlagState转换类 <br> * 存储属性:DeleteFlagState.getCode() <br> */ public class DeleteFlagStateHandler extends BaseTypeHandler<DeleteFlagState> { private Class<DeleteFlagState> type; private final DeleteFlagState[] enums; /** * 设置配置文件设置的转换类以及枚举类内容,供其他方法更便捷高效的实现 * @param type 配置文件中设置的转换类 */ public DeleteFlagStateHandler(Class<DeleteFlagState> type) { if (type == null) throw new IllegalArgumentException("Type argument cannot be null"); this.type = type; this.enums = type.getEnumConstants(); if (this.enums == null) throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type."); } @Override public DeleteFlagState getNullableResult(ResultSet rs, String columnName) throws SQLException { // 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型 int i = rs.getInt(columnName); if (rs.wasNull()) { return null; } else { // 根据数据库中的code值,定位DeleteFlagState子类 return locateDeleteFlagState(i); } } @Override public DeleteFlagState getNullableResult(ResultSet rs, int columnIndex) throws SQLException { // 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型 int i = rs.getInt(columnIndex); if (rs.wasNull()) { return null; } else { // 根据数据库中的code值,定位DeleteFlagState子类 return locateDeleteFlagState(i); } } @Override public DeleteFlagState getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { // 根据数据库存储类型决定获取类型,本例子中数据库中存放INT类型 int i = cs.getInt(columnIndex); if (cs.wasNull()) { return null; } else { // 根据数据库中的code值,定位DeleteFlagState子类 return locateDeleteFlagState(i); } } @Override public void setNonNullParameter(PreparedStatement ps, int i, DeleteFlagState parameter, JdbcType jdbcType) throws SQLException { // baseTypeHandler已经帮我们做了parameter的null判断 ps.setInt(i, parameter.getCode()); } /** * 枚举类型转换,由于构造函数获取了枚举的子类enums,让遍历更加高效快捷 * @param code 数据库中存储的自定义code属性 * @return code对应的枚举类 */ private DeleteFlagState locateDeleteFlagState(int code) { for(DeleteFlagState status : enums) { if(status.getCode().equals(Integer.valueOf(code))) { return status; } } throw new IllegalArgumentException("未知的枚举类型:" + code + ",请核对" + type.getSimpleName()); } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace 必须是DemoMapper接口的路径 --> <mapper namespace="com.alexgaoyh.MutiModule.persist.demo.DemoMapper"> <resultMap type="Demo" id="DemoResultMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="deleteFlagState" property="deleteFlagState" typeHandler="com.alexgaoyh.MutiModule.persist.handler.DeleteFlagStateHandler"/> </resultMap> <insert id="insertDemo" parameterType="Demo"> insert into Demo(deleteFlagState, name) values( #{deleteFlagState, typeHandler=com.alexgaoyh.MutiModule.persist.handler.DeleteFlagStateHandler}, #{name}) </insert> <select id="selectDemoById" resultMap="DemoResultMap" parameterType="java.lang.Integer"> select * from Demo where id=#{id} </select> </mapper>
时间: 2024-09-30 20:37:59