当前位置:网站首页>【Map】万能的Map使用方法 & 模糊查询的两种方式
【Map】万能的Map使用方法 & 模糊查询的两种方式
2022-07-26 12:08:00 【宋丹敏】
万能的Map
Map可以传递任意个参数,对象必须传递所有参数
对个参数用Map 或者 注解
Map结构 : 键值对
用途:假如我们的实体类,或者数据库中的表,字段或者参数过多,我们就考虑使用Map
Map传递参数,直接在参数中取出Key即可,Key和数据库中的属性名可以不一样
UserMapper(or UserDao).java
//万能的Map
int addUser1(Map<String,Object> map);
UserMapper.xml
- #{userid} 中的Userid可以和数据库中表的id名字不一样,因为这里是键
<!--万能的Map Map中的key可以取出来,Map可以传随意个参数,对象必须传所有参数-->
<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");这里可以只给数据库传递两个参数值
/* 万能的Map测试 Map可以传递随意个参数,对象必须传递所有参数 */
@Test
public void adduser1(){
//获取sqlsession
SqlSession sqlSession = MyBatisUtils.getSqlSession();
//拿到UserMapper实例
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//给实例中放入测试的数值
Map<String,Object> map = new HashMap<String,Object>();
map.put("userid", 5);
map.put("userName", "byby");
// map.put("password", "2124");
//调用测试方法
userMapper.addUser1(map);
//增删改,提交事务
sqlSession.commit();
//关闭sqlsession
sqlSession.close();
}
测试结果:
- 没有传的参数值,密码为Null

模糊查询两种方式
模糊查询有两种方法:
Java代码执行的时候,传递通配符 % %
UserMapper.java
//模糊查询 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
/* 模糊查询 名字输入“李”,就可以查询到所有name中含有“李”的信息 传入参数: String value 返回结果 List<User> */ @Test public void getUserLike(){ //获取SqlSession SqlSession sqlSession = MyBatisUtils.getSqlSession(); //拿到UserMapper实例 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); //给实例中传入测试数值 List<User> userList = userMapper.getUserLike("%张%"); //打印出userList for (User user : userList) { System.out.println(user); } //关闭SqlSession sqlSession.close(); }在sql拼接中使用通配符
UserMapper.java
//模糊查询 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
//给实例中传入测试数值 List<User> userList = userMapper.getUserLike("张");
Sql注入
sql注入:
select * from mybatis.user where id = ?
select * from mybaits.user where id = 1 or 1=1
原本用户之需要传递一个id参数,但是用户在向后端传递的时候拼接了一个 or 1=1,这个时候用户就可以查询出所有用户的信息,这样就造成sql注入
所以,在进行查询的时候,都会通过#{value}来写死,使得传递到后端的参数不能进行拼接
模糊查询的时候也尽量在Mapper.xml中写死传递进来的参数,不要进行拼接,模糊匹配在测试代码中进行匹配
边栏推荐
- 美容院管理系统统一管理制度?
- CVPR 2022 new SOTA for monocular depth estimation new CRFs: neural window fullyconnected CRFs
- C language code quantity statistical tool
- 海外APP推送(下篇):海外厂商通道集成指南
- Several inventory terms often used in communication
- Pytest interface automated testing framework | common plug-ins of pytest
- Pytest interface automated testing framework | confitest.py
- Oracle AWR report script: SQL ordered by elapsed time
- Flink cdc 是不是只支持 sql-client的方式提交SQL脚本啊
- Introduction to FPGA (II) - one out of two selector
猜你喜欢

What is per title encoding?

.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI

Use and optimization of MySQL composite index (multi column index)

Why is redis so fast? Redis threading model and redis multithreading

Redis主从复制原理

Talking about web vitals

数智转型,管理先行|JNPF全力打造“全生命周期管理”平台

了解string类

儿童玩乐场所如何运营?

File类的学习过程中出现的问题及解决方法
随机推荐
Flutter JNI confusion introduction.So file release package flash back
Redis为什么这么快?Redis的线程模型与Redis多线程
Pytest interface automation test framework | setup and teardown functions of pytest
面试官:如何处理高并发?
[Anhui University] information sharing of postgraduate entrance examination and re examination
pytest接口自动化测试框架 | 使用多个fixture
Pytest interface automated testing framework | using multiple fixtures
Here blog: running a large language model in a production environment - overview of the reasoning framework
How did the $50000 annual salary run out
种种迹象表明,Apple将有望支持AV1
4.1 configure MySQL and register login module
Miccai2022 paper | evolutionary multi-objective architecture search framework: application in covid-19 3D CT classification
pytest接口自动化测试框架 | 使用装饰器修饰需要运行的用例
面试京东T5,被按在地上摩擦,鬼知道我经历了什么?
Pytest interface automated testing framework | confitest.py
X.509、PKCS文件格式介绍
Ga-rpn: recommended area network for guiding anchors
SSJ-21B时间继电器
[download attached] a powerful web automated vulnerability scanning tool - Xray
DS-24C/DC220V时间继电器