当前位置:网站首页>mybats中xml参数类型的设置
mybats中xml参数类型的设置
2022-07-20 12:52:00 【小蚂蚁hjk】
1.单个参数或者是多个参数相同类型的时候可以指定相应参数类型
2.当多个参数并且不同类型的时候就不需要指定参数了
例如
1.单个参数
public List<XXBean> getXXBeanList(String xxCode); <select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean"> select t.* from tableName t where t.id= #{id} </select> 其中方法名和ID一致,#{}中的参数名与方法中的参数名一直, 我这里采用的是XXXBean是采用的短名字, select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充。
2.、多参数:
public List<XXXBean> getXXXBeanList(String xxId, String xxCode); <select id="getXXXBeanList" resultType="XXBean"> select t.* from tableName where id = #{0} and name = #{1} </select> 由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
3.Map封装多参数:
public List<XXXBean> getXXXBeanList(HashMap map); <select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean"> select 字段... from XXX where id=#{xxId} code = #{xxCode} </select> 其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。
4、List封装in:
public List<XXXBean> getXXXBeanList(List<String> list); <select id="getXXXBeanList" resultType="XXBean"> select 字段... from XXX where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select> foreach 最后的效果是select 字段... from XXX where id in ('1','2','3','4')
5.多参数传递之注解方式示:
例子: public AddrInfo getAddrInfo(@Param("corpId")int corpId, @Param("addrId")int addrId); xml配置这样写: <select id="getAddrInfo" resultMap="com.xxx.xxx.AddrInfo"> SELECT * FROM addr__info where addr_id=#{addrId} and corp_id=#{corpId} </select> 以前在<select>语句中要带parameterType的,现在可以不要这样写。
6.selectList()只能传递一个参数,但实际所需参数既要包含String类型,又要包含List类型时的处理方法:
将参数放入Map,再取出Map中的List遍历。如下:
List<String> list_3 = new ArrayList<String>(); Map<String, Object> map2 = new HashMap<String, Object>(); list.add("1");
list.add("2");
map2.put("list", list); //网址id map2.put("siteTag", "0");//网址类型
public List<SysWeb> getSysInfo(Map<String, Object> map2) { return getSqlSession().selectList("sysweb.getSysInfo", map2); }
<select id="getSysInfo" parameterType="java.util.Map" resultType="SysWeb"> select t.sysSiteId, t.siteName, t1.mzNum as siteTagNum, t1.mzName as siteTag, t.url, t.iconPath from TD_WEB_SYSSITE t left join TD_MZ_MZDY t1 on t1.mzNum = t.siteTag and t1.mzType = 10 WHERE t.siteTag = #{siteTag } and t.sysSiteId not in <foreach collection="list" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </select>
6.1.由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
<update id="modifyPwd"> UPDATE ams_user SET login_pwd = #{0}, update_time =#{1,jdbcType=TIMESTAMP} WHERE user_id = #{2,jdbcType=INTEGER} </update>
- 通过注解的方式
调用方法: - Integer modifyPwd(@Param("userId")Integer userId,@Param("pwd") String pwd,@Param("upTime") Date updateTime);
- xml中的写法:
- <update id="modifyPwd"> UPDATE ams_user SET login_pwd = #{pwd}, update_time = #{upTime,jdbcType=TIMESTAMP} WHERE user_id = #{userId,jdbcType=INTEGER} </update>
- 通过Map的方式传递多个参数
map中key的名字就是在#{}中使用的那个 - Integer modifyPwd(HashMap map); <update id="modifyPwd" parameterType="hashmap"> UPDATE ams_user SET login_pwd = #{pwd}, update_time = #{upTime,jdbcType=TIMESTAMP} WHERE user_id =#{userId,jdbcType=INTEGER} </update>
边栏推荐
- Rose pass is being issued!
- Laravel 实现数据库和迁移文件的双向同步
- Avec la sortie de HongMeng 3.0, la fusion Multi - écrans a progressé régulièrement, mais Google a subi une nouvelle chute
- V853开发板硬件资料——RISC-V核E907用户手册
- 1800万引进23名菲律宾博士引热议,学校老师回应:权宜之计
- AD活动目录和域网络
- The method of using postman to test the interface and send the request with cookie
- When I came to interview Ali test engineer, the interviewer asked me about my plan for the next 3-5 years, and I drew a big cake for the interviewer.
- 执行程序的三种模式,内存与缓存
- Interview question: Queen eight question (Queen n question) [suggestions collection]
猜你喜欢
总结 | 基于深度学习的医学图像半监督分割
kubernetes创建一个dashboard只读权限的用户(具有exec权限)
Highly recommended | overview of convolutional neural networks: from basic technology to research prospects
Three dimensional data (channel in the second dimension) - four-dimensional data (channel in the first dimension before input to the pooling layer) - three-dimensional data (channel in the second dime
上海文旅局局长:安全是文旅业的生命线,正抢抓元宇宙新赛道
Five coquettish projects were born!
程序员看的JPEG图像压缩介绍(多图慎入)
马斯克回应是否会将大脑上传到云端:Already did it!
Redis realizes the ranking of surrounding scenic spots from near to far
With the release of Hongmeng 3.0, multi screen integration has been steadily promoted, but Google has suffered another setback
随机推荐
《流浪(Stray)》工人外套与安全帽获得方法 工人安全帽怎么获得
同步原语:锁
UML顺序图/序列图/时序图
九章云极DataCanvas YLearn因果学习开源项目:从预测到决策
There are 450million 5g network users and more than 900million 5g package users. Why are users still unwilling to accept 5g?
上海文旅局局长:安全是文旅业的生命线,正抢抓元宇宙新赛道
Four redis cluster schemes you must know and their advantages and disadvantages
Visual Studio Code 安装包下载慢&&安装&&环境配置&&新建一条龙详解
Point cloud format reading and saving
Codevs - 2750 is concerned about the southern disaster area
[linear DP] Digital triangle
鴻蒙3.0發布,多屏融合穩步推進,穀歌卻再受重挫
从铸剑到御剑:滴滴工程效能平台建设之路
重组单克隆抗体丨ProSci CD154 抗体实例分析
三维数据(channel在第2维)-四维数据(输入到pooling层之前,channel在第一维)-三维数据(channel在第2维)
【26. 字符串哈希】
The committee member information page of the registration conclusion at the roadshow
Installation and deployment of redis on centos7 [easy to understand]
Sax parsing XML and pull parsing XML
【Power Shell】Invoke-Expression ,Invoke-Expression -Command $activateCommand;错误或power shell激活虚拟环境报错失败