当前位置:网站首页>在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();
}
查询结果

边栏推荐
- 关于单应性矩阵的若干思考
- 公用函数----mfc
- Solve the problem that MySQL cannot insert Chinese data
- What is the implementation principle of Go iota keyword and enumeration type
- The elder brother of the goldfish RHCA memoirs: CL210 experiment management it network - chapter
- 小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
- 【翻译】CNCF培养的OpenMetrics成为一个孵化项目
- typora操作手册
- C#/VB.NET: extracted from the PDF document all form
- QT_QDialog dialog
猜你喜欢
随机推荐
opencv syntax Mat type summary
tooltip control
2022年 PHP面试问题记录
三维空间中点的插值
Topology Parts Disassembly 3D Visualization Solution
【Day_11 0506】求最大连续bit数
【翻译】CNCF培养的OpenMetrics成为一个孵化项目
Leetcode74. Search 2D Matrix
How to solve the dynamic binding of el-form-item prop attribute does not take effect
QT常用全局宏定义
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) 题解
opencv real-time face detection
MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
How to make the fixed-point monitoring equipment display the geographic location on the EasyCVR platform GIS electronic map?
ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions
How many steps does it take to convert an ENS domain name into music?
typora操作手册
2022年SQL大厂高频实战面试题(详细解析)
md5sum源码 可多平台编译
opencv语法Mat类型总结









