Mybatis 嵌套查询(高级结果映射)
来源:互联网 发布:国家网络管理平台 编辑:程序博客网 时间:2024/05/21 01:43
需求说明
有三张表,省级表,市级表,区级表。
需要做一个关联嵌套查询
public class Province {//省份 private String code; private String name; private List<City> cities;}
public class City {//城市 private String code; private String name; private List<Area> district;}
public class Area {//区域 private String code; private String name;}
这里我省略了实体类的构造方法。通过实体类是可以看出表结构,和表之间的关联关系的。
//这是在MybatisXml文件中做的关联映射<resultMap type="com.ea.bic.model.vo.Area" id="AreaMap">//区 <id column="id" property="code"/> <result column="areaname" property="name"/> </resultMap> <resultMap type="com.ea.bic.model.vo.City" id="cityMap">//市 <id column="cid" property="code"/> <result column="citname" property="name"/> <collection property="district" ofType="com.ea.bic.model.vo.Area" resultMap="AreaMap" /> </resultMap> <resultMap type="com.ea.bic.model.vo.Province" id="ProvinceMap">//省 <id column="pid" property="code" /> <result column="proname" property="name" /> <collection property="cities" ofType="com.ea.bic.model.vo.City" resultMap="cityMap" /> <!--两种方式的不同之处在这里,自己分析就可以知道--> </resultMap>
接下来就是SQL
<select id="findAreaList" resultMap="ProvinceMap"> t_province.id pid, t_province.areaName proname, t_city.id cid, t_city.areaName citname, t_district.id id, t_district.areaName areaname FROM t_area t_district INNER JOIN t_area t_city ON t_district.parent_id = t_city.id INNER JOIN t_area t_province ON t_city.parent_id = t_province.id</select>
列名的别名 一定要与配置的映射一致,不然你懂得。
后来为了这个查询的效率更高一点,我把三张表整成了一张视图。
然后SQL就成了这样
<select id="findAreaList" resultMap="ProvinceMap"> select t.id id, t.areaName areaname, t.cid cid, t.citName citname, t.pid pid, t.proname from t_area_full t</select>
数据的接收就用List来接收就OK了
当时我就卡在这里,一直找不到问题。
总结一下
想想也没啥好总结的,主要是为了记录下,防止下次还出现这样的问题
学编程要是不会总结,那跟咸鱼又有什么区别~
0 0
- Mybatis 嵌套查询(高级结果映射)
- MyBatis之高级关联和集合映射(二、嵌套查询和嵌套结果小案例)
- mybatis联合查询 之 高级结果映射
- MyBatis之高级关联和集合映射(二、嵌套结果综合案例)
- mybatis高级结果映射collection
- 使用MyBatis(八)一对一关联(嵌套查询,嵌套结果)
- MyBatis高级映射(一对一查询)
- MyBatis高级映射(一对多查询)
- Mybatis接口映射,嵌套查询
- Mybaties的关联映射(嵌套结果映射,嵌套查询映射)
- Mybatis高级映射一对一查询
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)
- Swing(一)
- 欢迎使用CSDN-markdown编辑器
- JMS入门(六)--DeliveryMode
- 在web项目里获取spring bean的工具类
- Mybatis 嵌套查询(高级结果映射)
- Android开发60条技术经验总结
- jedis调用redis之Set
- 数据结构 第二章 线性表(2)malloc和realloc内存分配问题
- 深入分析委托与事件_C#
- Android 关于OOM的解决方案
- Leetcode ☞ 61. Rotate List
- webView加载数据的方法
- 关于6410存储器映射