自定义枚举typeHandler
生活随笔
收集整理的這篇文章主要介紹了
自定义枚举typeHandler
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.定義枚舉類
package com.learn.ssm.chapter4.enumeration; public enum SexEnum {MALE(1, "男"),FEMALE(0, "女");private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}SexEnum(int id, String name) {this.id = id;this.name = name;}public static SexEnum getSexById(int id) {for (SexEnum sex : SexEnum.values()) {if (sex.getId() == id) {return sex;}}return null;} }2.實現typeHandler類
package com.learn.ssm.chapter4.typehandler; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler;import com.learn.ssm.chapter4.enumeration.SexEnum;@MappedTypes(SexEnum.class) @MappedjdbcTypes(jdbcType.INTEGER) public class SexEnumTypeHandler implements TypeHandler<SexEnum> {@Overridepublic void setParameter(PreparedStatement ps, int i, SexEnum parameter,JdbcType jdbcType) throws SQLException {ps.setInt(i, parameter.getId());}@Overridepublic SexEnum getResult(ResultSet rs, String columnName)throws SQLException {int id = rs.getInt(columnName);return SexEnum.getSexById(id);}@Overridepublic SexEnum getResult(ResultSet rs, int columnIndex) throws SQLException {int id = rs.getInt(columnIndex);return SexEnum.getSexById(id);}@Overridepublic SexEnum getResult(CallableStatement cs, int columnIndex)throws SQLException {int id = cs.getInt(columnIndex);return SexEnum.getSexById(id);}}3. 配置
<typeHandlers><!-- <typeHandler jdbcType="VARCHAR" javaType="string" handler="com.learn.ssm.chapter4.typehandler.SexEnumTypeHandler" /> --><package name="com.learn.ssm.chapter4.typehandler" /></typeHandlers>?
總結
以上是生活随笔為你收集整理的自定义枚举typeHandler的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贪婪算法在解决哈夫曼树及编码问题中的应用
- 下一篇: org.xml.sax.SAXParse