Mybatis(万能map)
mybatis(万能map)
我们使用对象作为参数有一个缺点:
- 我们要在mapper.xml文件和测试中要把所有的字段都写出来,那么,假如一个对象有100个字段,那我们要把这些字段都写出来吗?
所以这时我们就要用到map作为参数
实例:
对象 VS map
接口
int addUser(User user);
int addUser2(Map<String,Object> map);
mapper.xml
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into mybatis.user (id,name,pwd) values (#{id},#{name },#{pwd});
</insert>
<insert id="addUser2" parameterType="map">
insert into mybatis.user(id,name,pwd)values (#{helloid},#{helloname},#{hellopwd});
</insert>
测试
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.addUser(new User(4,"赵六","4664785"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void addUser2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
HashMap<String, Object> map = new HashMap<>();
map.put("helloid",8);
map.put("helloname","田七");
map.put("hellopwd","5465555");
userMapper.addUser2(map);
sqlSession.close();
}
如果对象中字段非常多的话,我们写起来就很麻烦,所以一定要使用map
总结:
- 参数为一个时,我们使用基本类型作为参数
- 参数为多个时,我们使用map作为参数
我今天运行这个测试,怎么都成功不了,一直报错,代码还是没有一点问题,经过一番折腾,发现是因为我在的xml文件的注释中含有中文,万万没有想到这里都会出错
解决方法:将UTF-8改为UTF8
Mybatis(万能map)的更多相关文章
- mybatis循环、mybatis传map
mybatis中使用循环.mybatis传入map案例 <!-- 根据id修改商户提成配置--> <update id="editStopAll" paramet ...
- mybatis 遍历map;
mybatis 遍历map; 参考http://blog.csdn.net/hj7jay/article/details/78652050 ps: ${m[key]}这是显示 打印的key读value ...
- JSONObject和JSONArray 以及Mybatis传入Map类型参数
import org.json.JSONArray;import org.json.JSONObject; 将字符串转化为JSONArray JSONArray jsonArray = new ...
- Mybatis sql map 小于号配置
Mybatis SQL map配置中出现小于号转义时,通过<![CDATA[查询条件]]>解决. EXCEMPLE: <select id="getComments&quo ...
- mybatis返回map类型数据空值字段不显示的解决方法
在日常开发中,查询数据返回类型为map,数据库中有些自动值为null,则返回的结果中没有值为空的字段,则如何显示值为空的字段呢? Spring boot + MyBatis返回map中null值默认不 ...
- mybatis使用Map<String,Object>映射mysql结果集,关于字段的问题
--mysql常用字段类型如图 --mybatis使用Map<String,Object>映射,会将tinyint映射成Integer类型.decimal映射成BigDecimal类型 所 ...
- mybatis入门,CRUD,万能Map,模糊查询
第一个Mybatis程序 核心配置文件mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?& ...
- 用Mybatis返回Map,List<Map>
返回Map,Mybatis配置如下 : <select id="getCountyHashMap" resultType="java.util.HashMap&qu ...
- MyBatis返回map数据
(1)接口中编写方法 //单行 public Map<String, Object> getEmpReturnMap(Integer id); //多行 @MapKey("id& ...
随机推荐
- pyecharts简介
一.概况 Echarts 是一个由百度开源的数据可视化,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可. 而 Python 是一门富有表达力的语言,很适合用于数据处理.当数据分析遇上数据可 ...
- Firebug: Net Panel 使用详解
Introduction to Firebug: Net Panel Since there is not much user documentation related to Firebug fea ...
- Shell 语法和tips -- 持续更新
1. 字符串掐头去尾 #, % 例如:x=aabbaarealwwvvwwecho "${x%w*w}"aabbaarealwwvv echo "${x%%w*w}&qu ...
- AtCoder Beginner Contest 169 题解
AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...
- win10 linux ubuntu子系统 使用adb
条件 本文已经默认你已经在win10系统下成功配置了ubuntu子系统,所以唯一的条件就是windows上的adb 版本和ubuntu子系统的adb版本一致. 方法 怎么来保证adb 版本一致呢?在本 ...
- EhCache简单入门
一 介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvider.Ehcache是一种广泛使用的开源Java分布式缓存.主要面向通 ...
- CF793A Oleg and shares 题解
Content 有 \(n\) 支股票,第 \(i\) 支股票原价为 \(a_i\) 卢布.每秒钟可能会有任意一支股票的价格下降 \(k\) 卢布,以至于降到负数.求所有股票的价格均变得相同所要经过的 ...
- Nginxre quest_time 和upstream_response_time
nginx优化之request_time 和upstream_response_time差别 https://www.cnblogs.com/dongruiha/p/7007801.html http ...
- JS判断是否为“YYYYMMDD”式的日期
function isDate8(sDate) { if (!/^[0-9]{8}$/.test(sDate)) { return false; } var year, month, day; yea ...
- c++11之函数参数包展开
1.关于 本文略带总结性,参考:泛化之美--C++11可变模版参数的妙用 参数包展开方式有两种: 递归展开 和 逗号表达式展开. 本文代码并非全部来自参考文章,自己做了注释和修改.请以原文为准 2. ...