在Mybatis的collection标签中获取以,分隔的id字符串
2019独角兽企业重金招聘Python工程师标准>>>
有的时候我们把一个表的id以逗号(,)分隔的字符串形式放在另一个表里表示一种包含关系,当我们要查询出我们所需要的全部内容时,会在resultMap标签中使用collection标签来获取这样的一个集合。
我们以门店以及门店提供的服务来进行一个介绍
这是一个门店表,service_ids是一家门店包含的所有的服务id
Java实体类为
/** * 服务商门店 */ @NoArgsConstructor @Data public class Store {private Long id; private String name; private Address address; private String cityName; private List<Service> serviceList; private Double avgStar; //服务的数量 private Integer numService; }
服务的数据表
Java实体类如下
/** * 商家服务 */ @NoArgsConstructor @AllArgsConstructor @Data public class Service {private Long id; private String name; private Price price; private String topUrls; private String details; private List<Evaluate> evaluateList; public Service deepClone() {Input input = null; try {Kryo kryo = new Kryo(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); Output output = new Output(stream); kryo.writeObject(output, this); output.close(); // System.out.println(Arrays.toString(stream.toByteArray())); input = new Input(new ByteArrayInputStream(stream.toByteArray())); return kryo.readObject(input,Service.class); }finally {input.close(); }} }
另外我们还需要一个用来接引索引的表sequence,只有一个主键字段seq,里面放入尽可能多的从1开始的数字
Mybatis dao如下
@Mapper public interface StoreDao {List<Store> findStoreByCity(String city); @Update("update store set service_ids=concat(service_ids,concat(',',#{serviceId})) where id=#{storeId}")int addServiceToStore(ParamId paramId); }
这里我们主要看的是findStoreByCity方法
映射文件如下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cloud.serviceprovider.dao.StoreDao"> <resultMap id="store_map" type="com.cloud.model.serviceprovider.Store"> <id property="id" column="id" /> <result property="name" column="store_name" /> <result property="cityName" column="city_name" /> <association property="address" javaType="com.cloud.model.serviceprovider.Address"> <id property="name" column="address_name" /> <result property="longitude" column="address_longitude" /> <result property="latitude" column="address_latitude" /> </association> <collection property="serviceList" javaType="java.util.List" column="service_ids" ofType="com.cloud.model.serviceprovider.Service" select="findServiceByIds"> </collection> </resultMap> <resultMap id="service_Map" type="com.cloud.model.serviceprovider.Service"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="top_urls" property="topUrls" /> <result column="details" property="details" /> <association property="price" javaType="com.cloud.model.serviceprovider.Price"> <id column="normal_price" property="normalPrice" /> <result column="seckill_price" property="secKillPrice" /> </association> </resultMap> <select id="findServiceByIds" parameterType="java.lang.String" resultMap="service_Map" resultType="java.util.List"> select id,name,normal_price,seckill_price,top_urls,details from service<where> id in (SELECT DISTINCTSUBSTRING_INDEX(SUBSTRING_INDEX(#{service_ids}, ',', seq),',' ,- 1)FROM sequencewhere seq BETWEEN 1AND (SELECT1 + LENGTH(#{service_ids}) - LENGTH(replace(#{service_ids}, ',', ''))))</where> </select> <select id="findStoreByCity" parameterType="java.lang.String" resultMap="store_map"> select id,store_name,city_name,address_name,address_longitude,address_latitude,service_idsfrom store<where> city_name=#{city}</where> </select> <select id="findStoreById" parameterType="java.lang.Long" resultMap="store_map" resultType="com.cloud.model.serviceprovider.Store"> select id,store_name,city_name,address_name,address_longitude,address_latitude,service_idsfrom store<where> id=#{id}</where> </select> </mapper>
我们重点来看的是
<collection property="serviceList" javaType="java.util.List" column="service_ids" ofType="com.cloud.model.serviceprovider.Service" select="findServiceByIds"> </collection>
<select id="findServiceByIds" parameterType="java.lang.String" resultMap="service_Map" resultType="java.util.List"> select id,name,normal_price,seckill_price,top_urls,details from service<where> id in (SELECT DISTINCTSUBSTRING_INDEX(SUBSTRING_INDEX(#{service_ids}, ',', seq),',' ,- 1)FROM sequencewhere seq BETWEEN 1AND (SELECT1 + LENGTH(#{service_ids}) - LENGTH(replace(#{service_ids}, ',', ''))))</where> </select>
这里需要说明的是如果写成id in (#{service_ids})是取不出我们所希望的集合的,因为#{service_ids}只是一个字符串,翻译过来的语句例为id in ('1,2,3')之类的语句,所以需要将它解析成id in (1,2,3),substring_index的作用可以自行查询。
最终在controller中查出来的结果如下
{
"code": 200,
"data": [
{
"address": {
"distance": 11444.8137,
"latitude": 256.2342133234,
"longitude": 135.3454234,
"name": "三润汽修厂"
},
"avgStar": 5,
"cityName": "广州",
"id": 1,
"name": "三润汽修厂",
"serviceList": [
{
"details": "sdfadfsdfdadsdf",
"id": 1,
"name": "人工洗车",
"price": {
"normalPrice": 50,
"secKillPrice": 45
},
"topUrls": "http://123.456.789"
},
{
"details": "ddsadfasdehgfjh",
"id": 2,
"name": "换轮胎",
"price": {
"normalPrice": 300,
"secKillPrice": 250
},
"topUrls": "http://123.456.789"
},
{
"details": "<html><body><img src='http://123.234.123.12'></body></html>",
"id": 2455928998547424253,
"name": "大保养",
"price": {
"normalPrice": 50,
"secKillPrice": 45
},
"topUrls": "http://123.234.123.12,http://234.123.343.21"
}
]
},
{
"address": {
"distance": 18577.1862,
"latitude": 348.23423234,
"longitude": 168.2344234,
"name": "驰加京海店"
},
"avgStar": null,
"cityName": "广州",
"id": 2,
"name": "驰加京海店",
"serviceList": [
{
"details": "sdfadfsdfdadsdf",
"id": 1,
"name": "人工洗车",
"price": {
"normalPrice": 50,
"secKillPrice": 45
},
"topUrls": "http://123.456.789"
},
{
"details": "ddsadfasdehgfjh",
"id": 2,
"name": "换轮胎",
"price": {
"normalPrice": 300,
"secKillPrice": 250
},
"topUrls": "http://123.456.789"
},
{
"details": "<html><body><img src='http://123.234.123.12'></body></html>",
"id": 2456268364314575869,
"name": "小保养",
"price": {
"normalPrice": 100,
"secKillPrice": 88
},
"topUrls": "http://123.234.123.12,http://234.123.343.21"
}
]
}
],
"msg": "操作成功"
}
转载于:https://my.oschina.net/u/3768341/blog/3060073
在Mybatis的collection标签中获取以,分隔的id字符串相关推荐
- Mybatis - xml文件标签中写注释
Mybatis - xml文件标签中写注释 1.错误场景. ( /* */ 注释 ) 在IDEA中的直接使用快捷键注释 Ctrl + Shift + / 注释. SELECT t.name, t.ag ...
- html怎么在页面中获取操作用户的id,CSS ID 选择器详解
类选择器还是 ID 选择器? 在类选择器这一章中我们曾讲解过,可以为任意多个元素指定类.前一章中类名 important 被应用到 p 和 h1 元素,而且它还可以应用到更多元素. 区别 1:只能在文 ...
- Mybatis 一个update标签中执行多条update sql语句
本质上Mysql是不支持这种骚操作的 但是不代表并不能实现,只需要在jdbc的url链接库地址上添加&allowMultiQueries=true即可 driver=com.mysql.jdb ...
- document中输出html字符串流,HTML DOMDocument从段落后面的标签中获取字符串
我想解析html文档.我需要'h2'之后所有'p'的内容. 要解析的html :(示例) Lorem ipsum Lorem ipsum dolor sit amet, consectetuer ad ...
- Mybatis 向数据库表中插入记录 不携带id值
MapperXml.xml文件 <insert id="addNews" parameterType="com.tedu.pojo.News" useG ...
- 前端小技巧:javascript 获取标签中的属性 对指定标签中的属性进行操作
javascript 标签中指定属性值的更改与获取 适用于基本标签 <input><i><a>等基本标签 引用jQuery,两种方式 使用下面的input标签为例子 ...
- Mybatis ResultMap Collection 复合主键
https://www.cnblogs.com/azhqiang/p/6492195.html Mybatis ResultMap Collection 复合主键 <resultMap type ...
- js修改div标签中的内容
<div id='divId'>初始文字</div><script>$(document).ready(function(e){$('#divId').html(' ...
- php mysql 到表最后_如何在PHP中获取MySQL表的最后插入ID?
如何在PHP中获取MySQL表的最后插入ID? 我有一张表,经常插入新数据. 我需要获取表格的最后一个ID. 我怎样才能做到这一点? 它类似于SELECT MAX(id) FROM table吗? g ...
最新文章
- Swift教程之类和结构体
- Oracle实例迁移_真实场景实操
- pip安装deb_趟坑:使用pip安装TensorFlow
- Spring原始注解和新注解(使用注解代替xml配置文件)
- [转]让.NET应用秒变微服务
- Python3——FTP(文件传输协议)
- 基于模板的文字识别结果结构化处理技术
- mysql动态变量查询_MySQL将变量传递给动态查询
- PyQt5笔记(06) -- 菜单
- Android 下拉刷新用法
- 想学python买什么书好-看了许多python书后!我发现学好python这三本书必看
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
- 数据库服务:数据库表空间扩容
- Mac键盘部分数字键和字母键失灵,无法使用怎么办?
- Android常见公有方法(隐藏虚拟按键/隐藏软键盘/获取屏幕宽高等)
- 如何在word里面打对勾
- 苹果13系统锁屏延迟_iPhone锁屏慢有延迟怎么办 苹果手机锁屏不灵敏解决方法
- 阿里java开发规范学习笔记 (V1.5华山版)
- DS1042C数字示波器的波形截图流程
- 华为RS3 封层模型及以太网帧结构
热门文章
- html鼠标位置采集,css – HTML – 相对于鼠标指针的工具提示位置
- 2021-秋招你准备好了吗?软件测试面试题
- html新标准,HTML 5新标准将会在2022年正式发布
- Oracle如何建立多库,基于Oracle多库查询方法(分享)
- python实践项目(一)
- 如何抓取http请求/拦截器用法
- 10.2.0.3.0 oracle导出,oracle 数据库从10.2.0.4升级到11.2.0.3
- 无法在此计算机上安装vmware,电脑中虚拟机无法安装VMware Tools提示D盘找不到setup.exe如何解决...
- sublime中html自动对齐,sublime text怎么对齐HTML代码
- mex2 Inputs and Outputs