mybatis逆向工程问题:多次访问同一example类总是缓存第一次的结果。

背景

使用ajax完成name是否存在的功能,结果每次得到的result都是第一次异步请求的result。一开始想到的是ajax缓存的问题,于是在请求url上加上随机数,结果失败。多次调试Controller类无果后,开始怀疑example类的方法带来的问题。

进展

考虑到对逆向工程不熟悉,我就采用手动写mapper的方式,最后竟然成功解决问题。于是把罪魁祸首锁定在example类。

结果

通过对serviceimpl类的观察,发现我把example类当成私有变量了。。也就是说,每次使用example类进行操作时,都使用这个已经创建好的example类,而这个example类也许就存储了以前parameter。问题迎刃而解。

    // 这里是以前的代码
    private ProducttypeExample producttypeExample = new ProducttypeExample();
    /**
     * 根据商品分类名查找商品
     */
    public Producttype findByName(String name) {
        // 这个example类已经定义为成员变量了
 producttypeExample.createCriteria().andNameEqualTo(name);
        List<Producttype> producttypes = producttypeMapper.selectByExample(producttypeExample);
        if (producttypes != null && producttypes.size() > 0) {
            return producttypes.get(0);
        }
        return null;
    }
    // 改正的代码
    /**
     * 根据商品分类名查找商品
     */
    public Producttype findByName(String name) {
        // 把example定义成局部变量,这样,每次访问该方法时,都会创建example对象。
        ProducttypeExample producttypeExample = new ProducttypeExample();
        producttypeExample.createCriteria().andNameEqualTo(name);
        List<Producttype> producttypes = producttypeMapper.selectByExample(producttypeExample);
        if (producttypes != null && producttypes.size() > 0) {
            return producttypes.get(0);
        }
        return null;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值