MyBatis中map的應用與模糊查詢實現(xiàn)代碼
更新時間:2021年11月08日 10:26:56 作者:\u674e\u80b2\u6b22
這篇文章主要介紹了MyBatis中map的應用與模糊查詢實現(xiàn)代碼,文中通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
1.MyBatis中map的應用
1.1.應用場景
假設,實體類,或者數據庫中的表,字段或者參數過多,應當考慮使用Map?。?!
1.2.具體實現(xiàn)
//萬能map int addUser2(Map<String,Object> map);
<!--對象中的屬性,可以直接取出來 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.注意點!??!
- Map傳遞參數,直接在sql中取出key即可!【parameterType=“map”】
- 對象傳遞參數,直接在sql中取對象的屬性即可!【parameterType=“Object”】
- 只有一個基本類型參數的情況下,可以直接在sql中取到! 多個參數用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();
}
}
到此這篇關于MyBatis中map的應用與模糊查詢實現(xiàn)代碼的文章就介紹到這了,更多相關MyBatis map模糊查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
淺談Spring框架中@Autowired和@Resource的區(qū)別
最近review別人代碼的時候,看到了一些@Autowired不一樣的用法,覺得有些意思,下面這篇文章主要給大家介紹了關于Spring框架中@Autowired和@Resource區(qū)別的相關資料,需要的朋友可以參考下2022-10-10
一文帶你學會Java中ScheduledThreadPoolExecutor使用
ScheduledThreadPoolExecutor是Java并發(fā)包中的一個類,同時也是?ThreadPoolExecutor的一個子類,本文主要為大家介紹一下ScheduledThreadPoolExecutor使用,需要的可以參考下2024-12-12
java基于正則提取字符串中的數字功能【如提取短信中的驗證碼】
這篇文章主要介紹了java基于正則提取字符串中的數字功能,可用于提取短信中的驗證碼,涉及java基于正則的字符串匹配相關操作技巧,需要的朋友可以參考下2017-01-01

