Mapper icon indicating copy to clipboard operation
Mapper copied to clipboard

首次使用查询date类型的时候报错了

Open hanyahui88 opened this issue 10 years ago • 9 comments

2016-06-30 10:36:09.807 DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 145 debug - ==>  Preparing: SELECT ID,CREATE_DATE,UPDATE_DATE,NAME,AGE FROM TEST WHERE ID = ? AND CREATE_DATE = ? AND UPDATE_DATE = ? AND NAME = ? AND AGE = ? 
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='createDate', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Date

    protected Long id;
    @Column(name = "CREATE_DATE")
    protected Date createDate;    // 创建日期
    @Column(name = "UPDATE_DATE")
    protected Date updateDate;    // 更新日期
    @Transient
    protected Boolean newRecord;
    private String name;
    private int age;

CREATE TABLE `TEST` (
`id`  int(15) NOT NULL ,
`name`  varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`age`  tinyint(4) NOT NULL ,
`create_date`  datetime NOT NULL ,
`update_date`  datetime NOT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=DYNAMIC
;

如果我把date类型的删掉了 报不一样的错 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='name', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #2 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String 麻烦帮忙看下这个问题,还有我想问下,自动生成的参数类型是不是能匹配上 比如我int类型不加引号,字符串加引号 谢谢

hanyahui88 avatar Jun 30 '16 02:06 hanyahui88

还有用通用mapper的效率高还是用xml的高

hanyahui88 avatar Jun 30 '16 02:06 hanyahui88

@abel533 我用的spring boot

hanyahui88 avatar Jun 30 '16 04:06 hanyahui88

可以通过@ColumnType注解设置jdbcType

abel533 avatar Jun 30 '16 13:06 abel533

@abel533 我看你写的例子,string类型的也没有加,为什么我的string类型的都不行

hanyahui88 avatar Jul 01 '16 01:07 hanyahui88

我用T selectByPrimaryKey(Object key);方法的,但是sql为什么是这样的

2016-07-01 09:23:24.797 DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 145 debug - ==>  Preparing: SELECT ID,NAME,AGE FROM TEST WHERE ID = ? AND NAME = ? AND AGE = ? 
2016-07-01 09:23:24.829 DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 145 debug - ==> Parameters: 1(Long), 1(Long), 1(Long)
2016-07-01 09:23:24.846 DEBUG org.apache.ibatis.logging.jdbc.BaseJdbcLogger 145 debug - <==      Total: 0
耗时:61 ms - id:com.grass.module.test.mapper.TestMapper.selectByPrimaryKey - Sql:SELECT ID,NAME,AGE FROM TEST WHERE ID = 1 AND NAME = 1 AND AGE = 1

是不是还要配置什么东西,不可能每个都要配置吧,

hanyahui88 avatar Jul 01 '16 01:07 hanyahui88

@abel533 麻烦分析下这是什么原因

hanyahui88 avatar Jul 04 '16 04:07 hanyahui88

@abel533 我跟你的spring-boot的例子配置的,但是还是报错,麻烦看下,谢谢

hanyahui88 avatar Jul 04 '16 10:07 hanyahui88

如果没有 @Id注解,那么所有字段作为联合主键。

abel533 avatar Jul 05 '16 14:07 abel533

好了 万分感谢

hanyahui88 avatar Jul 06 '16 02:07 hanyahui88