詳解Mybatis中萬能的Map和模糊查詢寫法
假設(shè),我們的實體類,或者數(shù)據(jù)庫中的表,字段或參數(shù)過多,我們接口參數(shù)以前用的是實體類,現(xiàn)在考慮使用下Map!
接口:
//萬能的Mapint addUser2(Map<String,Object> map);
mapper.xml:
<!--Map中的key--><insert parameterType='map'> insert into mybatis.user (id,name,pwd) values (#{userid},#{userName},#{passWord});</insert>
測試方法:
@Testpublic void addUser2(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Map<String, Object> map = new HashMap<>(); map.put('userid',5); map.put('userName','Hello'); map.put('passWord','2222333'); mapper.addUser2(map); sqlSession.commit(); sqlSession.close();}
現(xiàn)在通過Map傳遞參數(shù),直接再sql中取出key即可!

而以前對象傳遞參數(shù),需要再sql取對象相對應(yīng)的屬性名才行!

而以前對象傳遞參數(shù),需要再sql取對象相對應(yīng)的屬性名才行!

只有一個基本類型參數(shù)的情況下,可以直接在sql中取到!(parameterType不寫都可以)

反正多個參數(shù)用Map,或者注解(后面記錄的文章會說到)!
2、模糊查詢寫法模糊查詢怎么寫?
1.java代碼執(zhí)行的時候傳遞通配符% %(比較安全,能防止sql注入,推薦)
List<User> userList = mapper.getUserLike('%李%');

2.在sql拼接中使用通配符!(不能防sql注入)
<select resultType='com.kuang.pojo.User'> select * from mybatis.user where name like '%'#{value}'%'</select>

以上就是詳解Mybatis中萬能的Map和模糊查詢寫法的詳細內(nèi)容,更多關(guān)于Mybatis Map和模糊查詢寫法的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. mysql的like模式2. MySQL分區(qū)的優(yōu)點3. 加密你的Access數(shù)據(jù)庫asp打開方法4. 什么是Access數(shù)據(jù)庫5. MySQL 字符串函數(shù):字符串截取6. 如何實現(xiàn)MySQL數(shù)據(jù)庫的備份與恢復(fù)7. mysql-bin.000001文件的來源及處理方法8. Oracle根據(jù)逗號拆分字段內(nèi)容轉(zhuǎn)成多行的函數(shù)說明9. mysql數(shù)據(jù)庫中最常用的時間轉(zhuǎn)換函數(shù)的用法10. mysql like語句問題

網(wǎng)公網(wǎng)安備