MyBatis中map的應(yīng)用與模糊查詢(xún)實(shí)現(xiàn)代碼
1.MyBatis中map的應(yīng)用
1.1.應(yīng)用場(chǎng)景
假設(shè),實(shí)體類(lèi),或者數(shù)據(jù)庫(kù)中的表,字段或者參數(shù)過(guò)多,應(yīng)當(dāng)考慮使用Map?。。?/p>
1.2.具體實(shí)現(xiàn)
//萬(wàn)能map int addUser2(Map<String,Object> map);
<!--對(duì)象中的屬性,可以直接取出來(lái) parameterType=傳遞map中的key-->
<insert id="addUser" parameterType="map">
insert into mybatis.user (id, name, pwd) values (#{userId},#{userName},#{passWord});
</insert>
@Test
public void addUser(){
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String,Object> map = new HashMap<String,Object>();
map.put("userid",5);
map.put("userName", "Hello");
map.put("passWord","123456");
userMapper.addUser2(map);
sqlSession.commit();
}catch(Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
}
1.3.注意點(diǎn)!?。?/h3>
- Map傳遞參數(shù),直接在sql中取出key即可!【parameterType=“map”】
- 對(duì)象傳遞參數(shù),直接在sql中取對(duì)象的屬性即可!【parameterType=“Object”】
- 只有一個(gè)基本類(lèi)型參數(shù)的情況下,可以直接在sql中取到! 多個(gè)參數(shù)用Map,或者注解!
2.模糊查詢(xún)
User gteUserById(Map<String,Object> map);
<select id="getUserLike" resultType="com.pojo.User">
select * from mybatis.user where name like #{value}
</select>
@Test
public void getUserLike(){
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserLike("%lyh%");
for(User user : userList){
System.out.println(user);
}
}catch(Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
}
到此這篇關(guān)于MyBatis中map的應(yīng)用與模糊查詢(xún)實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)MyBatis map模糊查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatis-plus?查詢(xún)傳入?yún)?shù)Map,返回List<Map>方式
- Mybatis中的mapper模糊查詢(xún)語(yǔ)句LIKE
- 解決mybatis一對(duì)多查詢(xún)r(jià)esultMap只返回了一條記錄問(wèn)題
- mybatis mapper.xml 區(qū)間查詢(xún)條件詳解
- MyBatis的模糊查詢(xún)mapper.xml的寫(xiě)法講解
- mybatis的mapper特殊字符轉(zhuǎn)移及動(dòng)態(tài)SQL條件查詢(xún)小結(jié)
- Mybatis如何通過(guò)出入Map參數(shù)作為條件進(jìn)行查詢(xún)
相關(guān)文章
java中1+1d/5和1+1/5的區(qū)別說(shuō)明
這篇文章主要介紹了java中1+1d/5和1+1/5的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java學(xué)習(xí)教程之定時(shí)任務(wù)全家桶
這篇文章主要給大家介紹了關(guān)于Java學(xué)習(xí)教程之定時(shí)任務(wù)全家桶的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談Spring框架中@Autowired和@Resource的區(qū)別
最近review別人代碼的時(shí)候,看到了一些@Autowired不一樣的用法,覺(jué)得有些意思,下面這篇文章主要給大家介紹了關(guān)于Spring框架中@Autowired和@Resource區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-10-10
一文帶你學(xué)會(huì)Java中ScheduledThreadPoolExecutor使用
ScheduledThreadPoolExecutor是Java并發(fā)包中的一個(gè)類(lèi),同時(shí)也是?ThreadPoolExecutor的一個(gè)子類(lèi),本文主要為大家介紹一下ScheduledThreadPoolExecutor使用,需要的可以參考下2024-12-12
java基于正則提取字符串中的數(shù)字功能【如提取短信中的驗(yàn)證碼】
這篇文章主要介紹了java基于正則提取字符串中的數(shù)字功能,可用于提取短信中的驗(yàn)證碼,涉及java基于正則的字符串匹配相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
手把手帶你理解java線(xiàn)程池之工作隊(duì)列workQueue
這篇文章主要介紹了java線(xiàn)程池之工作隊(duì)列workQueue,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

