当前位置:网站首页>[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
边栏推荐
- Pytest interface automated testing framework | confitest.py
- pytest接口自动化测试框架 | 重新运行失败用例
- DS-112时间继电器
- Pytest interface automated test framework | fixture call fixture
- 什么是OOM,为什么会OOM及一些解决方法
- pytest接口自动化测试框架 | pytest配置文件
- 物联网设备加密的意义
- 需不需要上线MES系统,你在纠结什么?
- [ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
- 美容院管理系统统一管理制度?
猜你喜欢

How much do you know about the two infrastructures of the badminton stadium?

什么是Per-Title编码?

扫雷小游戏——轻松玩上瘾(C语言版)

Redis为什么这么快?Redis的线程模型与Redis多线程

动静态库的实现(打包动静态库供他人使用)

数智转型,管理先行|JNPF全力打造“全生命周期管理”平台
![[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!](/img/a0/4910970a089cab198875944c7ae88c.png)
[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!

FPGA入门学习(二) - 二选一的选择器

什么是物联网?常见IoT协议最全讲解

一些常用的文章写作使用方法和技巧
随机推荐
V00 - 年纪大了,想做啥就做啥吧
MySQL之数据查询(聚合函数)
There are various signs that apple is expected to support AV1
pytest接口自动化测试框架 | pytest常用插件
什么是物联网?常见IoT协议最全讲解
了解string类
Oracle AWR report script: SQL ordered by elapsed time
MATLAB中strjoin函数使用
Pytest interface automated testing framework | common plug-ins of pytest
Pytest interface automation test framework | execute use cases through markup expressions
pytest接口自动化测试框架 | 使用装饰器修饰需要运行的用例
DS-112时间继电器
大量if else判断如何优化?@Valib详解
V01 - XX,记录美好生活从日志开始
Fineos announced the open registration of grouptech connect activities in 2022
《多线程下ThreadLocal使用场景实例》
Yuancosmos daily | yuancosmos social app "Party Island" product off the shelves; Guangzhou Nansha yuanuniverse industrial agglomeration zone was unveiled; The inter ministerial joint conference system
[MySQL constraint]
Redis实现Single单点登入--系统框架搭建(一)
Network protocol: tcp/ip protocol