MyBatis中map的應(yīng)用與模糊查詢實(shí)現(xiàn)代碼
1.MyBatis中map的應(yīng)用
1.1.應(yīng)用場(chǎng)景
假設(shè),實(shí)體類,或者數(shù)據(jù)庫中的表,字段或者參數(shù)過多,應(yīng)當(dāng)考慮使用Map?。?!
1.2.具體實(shí)現(xiàn)
//萬能map int addUser2(Map<String,Object> map);
<!--對(duì)象中的屬性,可以直接取出來 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)?。?!
- Map傳遞參數(shù),直接在sql中取出key即可!【parameterType=“map”】
- 對(duì)象傳遞參數(shù),直接在sql中取對(duì)象的屬性即可!【parameterType=“Object”】
- 只有一個(gè)基本類型參數(shù)的情況下,可以直接在sql中取到! 多個(gè)參數(shù)用Map,或者注解!
2.模糊查詢
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)用與模糊查詢實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)MyBatis map模糊查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java學(xué)習(xí)教程之定時(shí)任務(wù)全家桶
這篇文章主要給大家介紹了關(guān)于Java學(xué)習(xí)教程之定時(shí)任務(wù)全家桶的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
淺談Spring框架中@Autowired和@Resource的區(qū)別
最近review別人代碼的時(shí)候,看到了一些@Autowired不一樣的用法,覺得有些意思,下面這篇文章主要給大家介紹了關(guān)于Spring框架中@Autowired和@Resource區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-10-10
一文帶你學(xué)會(huì)Java中ScheduledThreadPoolExecutor使用
ScheduledThreadPoolExecutor是Java并發(fā)包中的一個(gè)類,同時(shí)也是?ThreadPoolExecutor的一個(gè)子類,本文主要為大家介紹一下ScheduledThreadPoolExecutor使用,需要的可以參考下2024-12-12
java基于正則提取字符串中的數(shù)字功能【如提取短信中的驗(yàn)證碼】
這篇文章主要介紹了java基于正則提取字符串中的數(shù)字功能,可用于提取短信中的驗(yàn)證碼,涉及java基于正則的字符串匹配相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
手把手帶你理解java線程池之工作隊(duì)列workQueue
這篇文章主要介紹了java線程池之工作隊(duì)列workQueue,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

