mybatis3.2.6版本的bug

mybatis3.2.6注解@Result设置映射关系,使用one=@One报错。

一开始目标从数据库文章表获取数据,一篇文章对应一个用户,一篇文章对应一个分类。从文章表获取的user_id要在到用户表中获取用户信息。

文章类article

public class Article implements Serializable {
    private int id;
    private String title;
    private String content;
    private String createTime;
    //一个标签对应多个文章
    private Tag tag;
    //一个用户对应多个文章
    private User user;
    ...
}

User

public class User implements Serializable {
    private int id;
    private String loginname;
    private String password;
    private String username;
    private String email;
    private String createTime;
    ...
}

ArticleDao:

    @SelectProvider(type = ArticleDynaSqlProvider.class, method = "selectWithUserId")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "content", property = "content"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "user_id", property = "user", jdbcType = JdbcType.INTEGER,
                one = @One(select = "com.sweat.dao.UserDao.selectById")),
            @Result(column = "tag_id", property = "tag",
                one = @One(select = "com.sweat.dao.TagDao.selectTagWithId"))
    })
    List<Article> selectWithUserId(Map<String, Object> param);

one = @One(select = "com.sweat.dao.UserDao.selectById"))对应UserDaoselectById方法

    @Select("select * from " + BlogConstants.USERTABLE + " where id = #{id}")
    User selectById(int id);

TagDao类似。

ArticleDynaSqlProviderselectWithUserId方法

    public String selectWithUserId(final Map<String, Object> params) {
        String sql = new SQL(){
            {
                SELECT("*");
                FROM(BlogConstants.ARTICLETABLE);
                if(params.get(BlogConstants.USERSESSION) != null) {
                    User user = (User) params.get(BlogConstants.USERSESSION);
                    WHERE(" user_id = #{user.id} ");
                }

                ORDER_BY(" create_time desc ");
            }
        }.toString();
        return sql;
    }

一切看着都很正常。
然后调用ArticleDaoselectWithUserId方法:
访问URL报错

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.sweat.dao.ArticleDao.selectWithUserId

当时非常奇怪为什么会报错,各种尝试,后来发现one=@One()部分有问题。 百度没有查到什么,然后谷歌也没有任何相关的。= =!

后来重新创建个项目,继续使用one=@One,然后报错

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Cannot use both @One and @Many annotations in the same @Result

很惊讶错误变了,查了一下这个错误,就查到一篇 MyBatis 3.2.6 not work annotation @One #167,发现是这个版本的bug,然后将2个项目的mybatis版本都切换到 3.3.0后(代码没有改),成功运行。
= =。。。 前前后后花了很久时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值