当前位置:网站首页>Fuzzy query in Map pass-by-value and object pass-by-value
Fuzzy query in Map pass-by-value and object pass-by-value
2022-08-01 18:20:00 【Small and cool】
文章目录
一、在MapFuzzy query in pass-by-value
- 适用于字段或者参数过多,我们应当考虑使用Map!
- 在xml的sql语句当中,parametertaypeThe type of directly writemap,No need to write the full class name of the entity class.
- #{} Variables can be named arbitrarily within parentheses,It is no longer necessary to match the variable name of the entity class.
- 在测试时,只需要 new 一个hashmap ,Treat variables inside parentheses as key值,valuefor the value you want to add,然后put进map里就可以了.Saves the step of building multiple constructors.
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);
}
//In addition, deletion and modification, the transaction must be submitted! sqlSession.commit();
sqlSession.close();
}
二、Fuzzy query in object pass-by-value
- bound to the interfacexml中,#{} 括号里面的变量,Must correspond to the variable name defined by the entity class.
- 在测试类当中,要new对象时,The parameters of the constructor must also correspond one-to-one with the constructor created by the entity class.
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);
}
//In addition, deletion and modification, the transaction must be submitted! sqlSession.commit();
sqlSession.close();
}
查询结果

边栏推荐
猜你喜欢
随机推荐
bat 批示处理详解-2
以消费场景为驱动的CMDB要怎么建?
生命周期和作用域
C#/VB.NET:从 PDF 文档中提取所有表格
sql添加索引
Zabbix6.0 DingTalk robot alarm
AntDB数据库亮相24届高速展,助力智慧高速创新应用
C language theory--a solid foundation for the written test and interview
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
LeetCode 1374.生成每种字符都是奇数个的字符串
B001 - Intelligent ecological fish tank based on STM32
解决MySQL插入不了中文数据问题
QT基础功能,信号、槽
云原生全景图详解
Topology Parts Disassembly 3D Visualization Solution
B005 - STC8 based single chip microcomputer intelligent street light control system
【Day_11 0506】 最近公共祖先
XAML WPF item groupBox control
opencv实时人脸检测
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践








