当前位置:网站首页>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_11 0506】 最近公共祖先
- el-form-item prop属性动态绑定不生效如何解决
- Prometheus的Recording rules实践
- 【Day_11 0506】求最大连续bit数
- OpenCV installation, QT, VS configuration project settings
- 电商库存系统的防超卖和高并发扣减方案
- sql添加索引
- B005 - STC8 based single chip microcomputer intelligent street light control system
- 【Day_09 0427】走方格的方案数
- MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
猜你喜欢
随机推荐
公用函数----mfc
QLineEdit learning and use
B001 - Intelligent ecological fish tank based on STM32
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions
WinRAR | Generate multiple installers into one installer
ExcelPatternTool: Excel表格-数据库互导工具
Leetcode74. 搜索二维矩阵
Stop using MySQL online DDL
想随时、随地、随心使用数据库的朋友们,全体注意!
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
XAML WPF项目groupBox控件
2022年 PHP面试问题记录
What is the implementation principle of Go iota keyword and enumeration type
QPalette调色板、框架色彩填充
以消费场景为驱动的CMDB要怎么建?
Friends who want to use the database anytime, anywhere and as you like, all attention!
日志工厂(详细)
SQL的ROUND函数用法及其实例
opencv syntax Mat type summary








