背景
使用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;
}