当前位置:网站首页>[map] universal map usage & two ways of fuzzy query
[map] universal map usage & two ways of fuzzy query
2022-07-26 12:15:00 【Songdanmin】
List of articles
Omnipotent Map
Map You can pass any number of parameters , Object must pass all parameters
For two parameters Map perhaps annotation
Map structure : Key value pair
purpose : Suppose our entity class , Or a table in a database , Too many fields or parameters , We'll consider using Map
Map Pass parameters , Take it directly from the parameter Key that will do ,Key It can be different from the attribute name in the database
UserMapper(or UserDao).java
// Omnipotent Map
int addUser1(Map<String,Object> map);
UserMapper.xml
- #{userid} Medium Userid It can be compared with the table in the database id The names are different , Because this is the key
<!-- Omnipotent Map Map Medium key You can take it out ,Map You can pass any parameter , Object must pass all parameters -->
<insert id="addUser1" parameterType="map">
INSERT INTO mybatis.`user`(id,NAME,pwd) VALUES(#{userid},#{userName},#{password})
</insert>
UserMapperTest.java
map.put("userid", 5); map.put("userName", "byby");Here, you can only pass two parameter values to the database
/* Omnipotent Map test Map You can pass any parameter , Object must pass all parameters */
@Test
public void adduser1(){
// obtain sqlsession
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// Get UserMapper example
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// Put the test value into the example
Map<String,Object> map = new HashMap<String,Object>();
map.put("userid", 5);
map.put("userName", "byby");
// map.put("password", "2124");
// Call test methods
userMapper.addUser1(map);
// Additions and deletions , Commit transaction
sqlSession.commit();
// close sqlsession
sqlSession.close();
}
test result :
- No parameter value passed , The password for Null

There are two ways of fuzzy query
There are two methods of fuzzy query :
Java When the code executes , Pass wildcard % %
UserMapper.java
// Fuzzy query List<User> getUserLike(String value);UserMapper.xml
<!-- Fuzzy query --> <select id="getUserLike" parameterType="String" resultType="com.kuang.pojo.User"> select * from mybatis.user where name like #{value} </select>UserMapperTest.java
/* Fuzzy query Name input “ Li ”, You can find all name contains “ Li ” Information about Pass in the parameter : String value Return results List<User> */ @Test public void getUserLike(){ // obtain SqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); // Get UserMapper example UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // Pass the test value into the instance List<User> userList = userMapper.getUserLike("% Zhang %"); // Print out userList for (User user : userList) { System.out.println(user); } // close SqlSession sqlSession.close(); }stay sql Using wildcards in stitching
UserMapper.java
// Fuzzy query List<User> getUserLike(String value);UserMapper.xml
<select id="getUserLike" parameterType="String" resultType="com.kuang.pojo.User"> select * from mybatis.user where name like "%"#{value}"%" </select>UserMapperTest.java
// Pass the test value into the instance List<User> userList = userMapper.getUserLike(" Zhang ");
Sql Inject
sql Inject :
select * from mybatis.user where id = ?
select * from mybaits.user where id = 1 or 1=1
The original user needs to pass a id Parameters , But the user spliced one when delivering to the back end or 1=1, At this time, users can query the information of all users , This results in sql Inject
therefore , During the query , Will pass #{value} To write death , The parameters passed to the back end cannot be spliced
When fuzzy query, try to Mapper.xml Write the parameters passed in , Do not splice , Fuzzy matching matches in the test code
边栏推荐
- 空洞卷积详解(输入输出大小分析)
- Redisson分布式锁使用实例(一)
- X.509、PKCS文件格式介绍
- 常用的 list.isEmpty() 为何突然报null?
- pytest接口自动化测试框架 | pytest获取执行数据、pytest禁用插件
- pytest接口自动化测试框架 | conftest.py
- Do you need to launch MES system? What are you struggling with?
- Use and optimization of MySQL composite index (multi column index)
- Harbor2.2 quick check of user role permissions
- Real time synchronization and conversion of massive data based on Flink CDC
猜你喜欢

JVM内存溢出和内存泄漏的区别

pytest接口自动化测试框架 | pytest配置文件

连锁店收银系统如何帮助鞋店管理好分店?

10. 509. Introduction to PKCs file format

Sword finger offer 25. merge two sorted linked lists

Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)

Pytoch deep learning quick start tutorial -- mound tutorial notes (I)

Introduction to FPGA (I) - the first FPGA project

自定义浏览器默认右击菜单栏

二、容器_
随机推荐
14.2字节流学习
El form displays two columns per row, with the bottom button centered
Pytest interface automation test framework | setup and teardown functions of pytest
Pycharm is really strong
DS-112时间继电器
X.509、PKCS文件格式介绍
[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
动静态库的实现(打包动静态库供他人使用)
Ds-112 time relay
Pytest interface automated testing framework | using multiple fixtures
Hit the blackboard and draw the key points: a detailed explanation of seven common "distributed transactions"
Introduction to FPGA (III) - 38 decoder
Pytest interface automated test framework | fixture call fixture
Y9000p2022 reinstallation win10 problem
flink 写redis 比较慢,大家有啥思路优化吗?
Use of strjoin function in MATLAB
二、容器_
Fineos announced the open registration of grouptech connect activities in 2022
DS-24C/DC220V时间继电器