MyBatis中map的应用与模糊查询实现代码

网友投稿 676 2022-11-24

MyBatis中map的应用与模糊查询实现代码

MyBatis中map的应用与模糊查询实现代码

目录1.MyBatis中map的应用1.1.应用场景1.2.具体实现1.3.注意点!!!2.模糊查询

1.MyBatis中map的应用

1.1.应用场景

假设,实体类,或者数据库中的表,字段或者参数过多,应当考虑使用Map!!!

1.2.具体实现

//万能map

int addUser2(Map map);

insert into mybatis.user (id, name, pwd) values (#{userId},#{userName},#{passWord});

@Test

public void addUser(){

SqlSession sqlSession = null;

try{

sqlSession = MybatisUtils.getSqlSession();

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

Map map = new HashMap();

map.put("userid",5);

map.put("userName", "Hello");

http:// 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 map);

select * from mybatis.user where name like #{value}

&http://lt;/select>

@Test

public void getUserLike(){

SqlSession sqlSession = null;

try{

sqlSession = MybatisUtils.getSqlSession();

UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

List userList = userMapper.getUserLike("%lyh%");

for(User user : userList){

System.out.println(user);

}

}catch(Exception e){

e.printStackTrace();

}finally {

sqlSession.close();

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【前端】Bootstrap
下一篇:【C++】*ptemp++的作用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~