您的位置:首页 > 房产 > 家装 > 衢州建筑地基加固_免费一百个空间访客领取网站_新浪nba最新消息_alexa全球网站排名分析

衢州建筑地基加固_免费一百个空间访客领取网站_新浪nba最新消息_alexa全球网站排名分析

2025/5/5 20:20:55 来源:https://blog.csdn.net/qq_26634873/article/details/147423810  浏览:    关键词:衢州建筑地基加固_免费一百个空间访客领取网站_新浪nba最新消息_alexa全球网站排名分析
衢州建筑地基加固_免费一百个空间访客领取网站_新浪nba最新消息_alexa全球网站排名分析

重点: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>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com