调用代码private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}

@Override
public User save(User u) {
try {
sqlSessionTemplate.insert("save",u);
} catch (DataAccessException e) {
e.printStackTrace();
}
return u;
}XML代码 <insert id="save" parameterType="userDo" keyProperty="id" useGeneratedKeys="true">
insert into
t_user(username,userpass,email,sex,province,city,area,birthday)
values(#{username,jdbcType=VARCHAR,jdbcType=VARCHAR},#{userpass,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR},#{sex,jdbcType=VARCHAR},#{province,jdbcType=VARCHAR},#{city,jdbcType=VARCHAR},#{area,jdbcType=VARCHAR},#{birthday,jdbcType=DATE})
</insert>
报错内容是:
java.lang.ClassCastException: java.lang.Integer cannot be cast to com.vbon.mybatis.pojo.User
这个问题很纠结啊,首先是我没有做过转换,就报错了。
求大神解答

解决方案 »

  1.   

    看一下你的User对象里是不是有什么属性是用int定义的,改成Integer类型
      

  2.   

    User类private Integer id;
    private String username;
    private String userpass;
    private String email;
    private String sex;
    private String province;
    private String city;
    private String area;
    private Date birthday;
      

  3.   

    本来今天说没事,来研究一下mybatis3的,看和ibatis2有什么区别,结果遇到这个问题。但是查询的时候没问题,哎
      

  4.   

    <insert id="save" parameterType="userDo" keyProperty="id" useGeneratedKeys="true">sqlSessionTemplate.insert("save",u);insert需要的是userDo,而传入的是User
    在MyBatis的配置文件里做过alias没有?
      

  5.   

    你是否应有spring框架,如果应有spring框架的话,要看看你dao接口返回类型应该修改成int
      

  6.   

    mybatis 你插入数据给你返回的Integer
    这个Integer就是你当前插入的记录数 一般的话都会是1 只要你没有插入失败
    如果你声明<selectKey resultType="int"  keyProperty="id" > 
                select LAST_INSERT_ID() 
    </selectKey> mybatis会给你自动填充插入后的ID到你的当前对象里
    所以你程序大可以把返回值设置成Integer,然后再打印看看你的user对象,必然有Id属性了
      

  7.   

    请问下你用 mybatis3 写过 分页插件没 , 不是用代码生成工具写的  用的是 拦截器写的
      

  8.   

    你是否应有spring框架,如果应有spring框架的话,要看看你dao接口返回类型应该修改成int