当前位置:网站首页>Map by value
Map by value
2022-08-01 18:20:00 【Cool Xiaoya】
文章目录
一、Map的使用说明
假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!
使用Map时,则Entity classes are not required了!(It is recommended to use when there are too many fields or parameters!)
二、Map 实例
Jump to previous article:
https://blog.csdn.net/weixin_45737330/article/details/126064436
Just modify the following code!
使用Map添加信息
1、Mapper接口
//使用mapto add user information
int getUserAdd02(Map<String,Object> map);
2、XML
<insert id="getUserAdd02" parameterType="map">
insert into mybatis01.user(id, name, pwd)
values (#{Ku_Id},#{Ku_Name},#{Ku_Pwd});
</insert>
3、Test
@Test
public void UserAdd(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//创建HashMap对象
HashMap<String, Object> map = new HashMap<>();
map.put("Ku_Id",6);
map.put("Ku_Name","酷");
map.put("Ku_Pwd","097");
mapper.getUserAdd02(map);
System.out.println(map);
//增删改一定要提交事务
sqlSession.commit();
sqlSession.close();
}
使用Map删除信息
1、Mapper接口
//使用map来删除用户信息
int getUserDel02(Map<String,Object> map);
2、XML
<delete id="getUserDel02" parameterType="map">
delete
from mybatis01.user
where id=#{Ku_Id};
</delete>
3、Test
@Test
public void UserDelete(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("Ku_Id",12);
int i = mapper.getUserDel02(map);
if(i>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败");
}
System.out.println(map);
//增删改一定要提交事务
sqlSession.commit();
sqlSession.close();
}
使用Map修改信息
1、Mapper接口
//使用map来修改用户信息
int getUserUpdate02(Map<String,Object> map);
2、XML
<update id="getUserUpdate02" parameterType="map">
update mybatis01.user
set id=#{Ku_Id},name=#{Ku_Name},pwd=#{Ku_Pwd}
where id=#{Ku_Id};
</update>
3、Test
@Test
public void UserUpd(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//创建HashMap对象
HashMap<String, Object> map = new HashMap<>();
map.put("Ku_Id",6);
map.put("Ku_Name","亚");
map.put("Ku_Pwd","3321");
mapper.getUserUpdate02(map);
System.out.println(map);
//增删改一定要提交事务
sqlSession.commit();
sqlSession.close();
}
使用Map查询信息
1、Mapper接口
//使用map来查询用户信息
List<Map> getUserSelect02(Map<String,Object> map);
2、XML
<select id="getUserSelect02" resultType="map">
select *
from mybatis01.user where id=#{kxy_Id};
</select>
3、Test
@Test
public void getUserSelect02(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//创建HashMap对象
HashMap<String, Object> map = new HashMap<>();
map.put("kxy_Id",6);
List<Map> mapList = mapper.getUserSelect02(map);
for (Map map1 : mapList) {
System.out.println(map1);
}
//增删改一定要提交事务 sqlSession.commit();
sqlSession.close();
}
总结
- Map传递参数,直接在sql中取出key即可!【parameterType=“map”】
- 对象传递参数,直接在sql中取对象的属性即可!【parameterType=“Object”】(int属性可省略不写!)
- 只有一个基本类型参数的情况下,可以直接在sql中取到! 多个参数用Map,或者注解!
边栏推荐
- 【Day_10 0428】井字棋
- MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
- SQL的ROUND函数用法及其实例
- 深入浅出Flask PIN
- 亚马逊云科技Build On2022技能提升计划第二季——揭秘出海爆款新物种背后的黑科技
- Leetcode75. Color Classification
- University of California | Inverse Reinforcement Learning from Different Third-Person Videos via Graph Abstraction
- 突破性能天花板!亚信数据库支撑 10 多亿用户,峰值每秒百万交易
- The function realization of the national standard GB28181 protocol EasyGBS platform compatible with the old version of the inflow port
- AntDB数据库亮相24届高速展,助力智慧高速创新应用
猜你喜欢

ExcelPatternTool: Excel表格-数据库互导工具

Leetcode73. Matrix Zeroing

QLineEdit学习与使用

QT basic functions, signals, slots

2022年MySQL最新面试题

XAML WPF item groupBox control

QPalette palette, frame color fill

explain 各字段介绍

QT commonly used global macro definitions

How to solve the dynamic binding of el-form-item prop attribute does not take effect
随机推荐
SQL的索引详细介绍
Tower Defense Shoreline User Agreement
SQL函数 TO_DATE(二)
Leetcode75. 颜色分类
SQL窗口函数
QT基础功能,信号、槽
日志工厂(详细)
Leetcode73. Matrix Zeroing
突破性能天花板!亚信数据库支撑 10 多亿用户,峰值每秒百万交易
打开微信客服
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
8月微软技术课程,欢迎参与
B005 – 基于STC8的单片机智能路灯控制系统
Go GORM事务实例分析
一加OnePlus 10RT出现在Geekbench上 产品发布似乎也已临近
Break the performance ceiling!AsiaInfo database supports more than 1 billion users, with a peak of one million transactions per second
AIOps智能运维的领跑者擎创科技正式入驻InfoQ 写作社区!
B002 - Embedded Elderly Positioning Tracking Monitor
How to solve the dynamic binding of el-form-item prop attribute does not take effect
Topology Parts Disassembly 3D Visualization Solution