SSM项目使用example查询时多次查询条件相同
生活随笔
收集整理的這篇文章主要介紹了
SSM项目使用example查询时多次查询条件相同
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2021年2月17日
用戶登錄Service層接收到Controller傳來的username和password然后到Dao層進行查詢時,查詢條件本應是Controller傳下來的username和password,但是查詢條件卻是上一次請求的username和password,導致如果請求多次,第一次請求如果輸入正確的用戶名和密碼,那么后續請求無論用戶名和密碼是否正確,都可以成功,因為查詢條件始終是第一次請求的用戶名和密碼。初步判斷原因是Service層的某個實例的作用域為單例所導致。根據初步判斷結果得到的初步解決方案為修改Servicez中某個實例的作用域為原型可解決。
2021年2月18日已解決,原因是我使用example組裝查詢條件(example為mybatis generator生成),spring的ioc容器默認的作用域為單例,導致每次請求的example都是同一個實例,查詢條件也都是同一個。解決方法為修改example這個類的作用域為request即每次請求創建一個新的example用來查詢。
這里需要注意的地方有:
如果你在閱讀本文章時發現其中錯誤請及時聯系我,希望我們共同進步
解決后的代碼
Controller:
Service:
@Service public class UserLoginServiceImpl implements UserLoginService {@Autowiredprivate UserMapper userMapper;@Autowiredprivate UserExample userExample;public User selectUserByUsernameAndPwd(String username, String password) {List<User> list = null;User user = null;UserExample.Criteria userCriteria = userExample.createCriteria();userCriteria.andUsernameEqualTo(username).andPasswordEqualTo(password);list = userMapper.selectByExample(userExample);if(list != null && list.size() != 0) {user = list.get(0);}return user;} }example(太長,部分):
@Component @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) public class UserExample {protected String orderByClause;protected boolean distinct;protected List<Criteria> oredCriteria;public UserExample() {oredCriteria = new ArrayList<>();} 新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的SSM项目使用example查询时多次查询条件相同的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HashMap的hash冲突解决方案
- 下一篇: jq实现点击导航栏中的任意一个跳转后被点