日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SSM项目使用example查询时多次查询条件相同

發布時間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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用來查詢。
這里需要注意的地方有:

  • spring管理bean時默認的作用域就是單例。
  • controller組件會在每次請求時創建一個新的實例。
  • controller組件中的service組件如果時默認的作用域,那么不會每次在controller組件請求后得到新的service組件的實例,因為默認是單例。
  • 如果你在閱讀本文章時發現其中錯誤請及時聯系我,希望我們共同進步

    解決后的代碼
    Controller:

    @Controller @RequestMapping(value = "/user") public class UserLoginController {@AutowiredUserLoginService userLoginService;@AutowiredUser user;@AutowiredResponseMessage responseMessage;@RequestMapping(value = "/userLogin.app",method = RequestMethod.POST)@ResponseBodypublic ResponseMessage userLoginByUsernameAndPassword(@RequestBody User user) {this.user = userLoginService.selectUserByUsernameAndPwd(user.getUsername(),user.getPassword());if(this.user != null) {responseMessage.setCode("200");responseMessage.setMsg("登陸成功");responseMessage.setTimestamp(System.currentTimeMillis());return responseMessage;}responseMessage.setCode("100");responseMessage.setMsg("登陸失敗,請檢查用戶名或密碼是否有誤");responseMessage.setTimestamp(System.currentTimeMillis());return responseMessage;} }

    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查询时多次查询条件相同的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。