当前位置:网站首页>在Map传值与对象传值中模糊查询
在Map传值与对象传值中模糊查询
2022-08-01 18:18:00 【酷小亚】
一、在Map传值中模糊查询
- 适用于字段或者参数过多,我们应当考虑使用Map!
- 在xml的sql语句当中,parametertaype的类型直接写map,不用写实体类的全类名了。
- #{} 括号内可以随意命名变量,不需要再与实体类的变量名一致。
- 在测试时,只需要 new 一个hashmap ,把括号内的变量当作key值,value为你要添加的值,然后put进map里就可以了。省去了建造多个构造器的步骤。
1、Mapper接口
List<Map> UserList(Map<String,Object> map);
2、XML
<select id="UserList" resultType="map">
select *
from mybatis01.user where name like #{value};
</select>
3、Test
@Test
public void getUserList() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("value", "%亚%");
List<Map> maps = mapper.UserList(map);
for (Map map1 : maps) {
System.out.println(map1);
}
//增删改中一定要提交事务! sqlSession.commit();
sqlSession.close();
}
二、在对象传值中模糊查询
- 在接口绑定的xml中,#{} 括号里面的变量,必须与实体类定义的变量名相对应。
- 在测试类当中,要new对象时,构造器的参数也必须与实体类创建的构造器一一对应。
1、实体类
package com.jin.pojo;
public class User {
private int id;
private String name;
private String pwd;
...
// (无参、有参、get和set、toString)
...
}
2、Mapper接口
List<User> UserList02(String name);
3、XML
<select id="UserList02" resultType="com.jin.pojo.User" parameterType="String">
select *
from mybatis01.user where name like #{name};
</select>
4、Test
@Test
public void UserList02(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.UserList02("%亚%");
for (User user : userList) {
System.out.println(user);
}
//增删改中一定要提交事务! sqlSession.commit();
sqlSession.close();
}
查询结果

边栏推荐
猜你喜欢
随机推荐
QLineEdit学习与使用
How to solve the dynamic binding of el-form-item prop attribute does not take effect
How to use the Golang coroutine scheduler scheduler
sql添加索引
Clip-on multimeter use method, how to measure the voltage, current, resistance?
EpiSci | Deep Reinforcement Learning for SoCs: Myth and Reality
opencv实时人脸检测
COS 用户实践征文
opencv语法Mat类型总结
【Day_08 0426】两种排序方法
消息模板占位符的使用
What is the JVM runtime data area and the JMM memory model
typora操作手册
Friends who want to use the database anytime, anywhere and as you like, all attention!
【Day_10 0428】密码强度等级
LeetCode 0151.颠倒字符串中的单词
2022年SQL大厂高频实战面试题(详细解析)
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
Leetcode75. 颜色分类
C#/VB.NET:从 PDF 文档中提取所有表格









