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

边栏推荐
猜你喜欢

JVM运行时数据区与JMM内存模型是什么

B001 - 基于STM32的智能生态鱼缸
Stop using MySQL online DDL

XAML WPF item groupBox control

MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?

【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)

B001 - Intelligent ecological fish tank based on STM32

基于flowable的upp(统一流程平台)运行性能优化

XAML WPF项目groupBox控件
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
随机推荐
483-82(23、239、450、113)
三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.
AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
暑假第二周总结博客
想随时、随地、随心使用数据库的朋友们,全体注意!
SQL function TO_DATE (2)
QT_事件类
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
钳形万用表使用方法,如何测量电压、电流、电阻?
生命周期和作用域
【Day_11 0506】 最近公共祖先
SQL的ROUND函数用法及其实例
LeetCode 1374.生成每种字符都是奇数个的字符串
AntDB数据库亮相24届高速展,助力智慧高速创新应用
塔防海岸线用户协议
How to solve the dynamic binding of el-form-item prop attribute does not take effect
【Day_08 0426】两种排序方法
Review实战经典:2 种封装风格,你偏爱哪种?
消息模板占位符的使用
How opencv implements image skew correction