我有以下几点:
public class Stat {
public enum HitType {
MOBILE1(0), MOBILE2(1), DESKTOP(2);
public final int value;
public int value() { return value; }
HitType(int val) {
value = val;
}
public static HitType parseInt(int i) {
switch (i) {
case 0: return MOBILE1;
case 1: return MOBILE2;
case 2: return DESKTOP;
default: return null;
}
}
}
public HitType hitType;
public long sourceId;
public Stat(... int hitType, BigInteger sourceId) {
this.hitType = HitType.parseInt(hitType);
this.sourceId = sourceId.longValueExact();@Mapper
public interface StatMapper {
@Select("select * from stats where id = #{id}")
@Results(value = {
@Result(property = "hitType", column = "hit_type"),
...
})
public Stat findById(@Param("id") long id); Stat s = statMapper.findById(1);
response.getOutputStream().print(s.toString());它仍然给出了这个错误:
由Handler执行引起的解析异常: org.mybatis.spring.MyBatisSystemException:嵌套异常是试图从结果集获取列'hit_type‘的org.mybatis.spring.MyBatisSystemException错误。原因: java.lang.IllegalArgumentException:没有枚举常量com.company.app.model.Stat.HitType.2
我试着读了http://stackoverflow.com/questions/5878952/ddg#5878986和Convert integer value to matching Java Enum。
如果我将构造函数签名更改为
public Stat(..., int hitType, long sourceId) {
this.sourceId = sourceId;然后给出了误差。
嵌套异常是org.apache.ibatis.executor.ExecutorException:在com.company.app.model.Stat匹配的java.math.BigInteger、java.lang.String、java.sql.Timestamp、java.lang.Integer、java.math.BigInteger中找不到构造函数
因此,在第一种情况下,它可能直接设置属性,而在第二种情况下,它使用的是构造函数。
我尝试将HitType hitType放入构造函数签名中,但它仍然给出了No constructor found...错误。
MyBatis 3.4.5,MyBati-Spring1.3.1,Spring-Boot1.5.13
发布于 2018-10-09 19:43:05
我为这种类型添加了getter & setter。
public HitType getHitType() {
return hitType;
}
public void setHitType(int hitType) {
this.hitType = HitType.parseInt(hitType);
}然后它就开始运作了。这很奇怪,因为它在抱怨构造函数签名。如果它使用的是构造函数,为什么需要getter和setter?
https://stackoverflow.com/questions/52727374
复制相似问题