mybatis多表查询
来源:互联网 发布:excel找两列不同的数据 编辑:程序博客网 时间:2024/06/07 02:28
最近在写spring+springmvc+mybatis相关的demo;分享下多表查询的心得:
A表结构:
C表结构
A实体类:
package com.azj.entity;
import java.util.List;
public class A {
private int aid;
private String aname;
private List<C> clist;
public List<C> getClist() {
return clist;
}
public void setClist(List<C> clist) {
this.clist = clist;
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
@Override
public String toString() {
return "A [aid=" + aid + ", aname=" + aname + ", clist=" + clist + "]";
}
}
C实体类
package com.azj.entity;
public class C {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "C [id=" + id + ", name=" + name + "]";
}
public C(int id, String name) {
super();
this.id = id;
this.name = name;
}
public C() {
super();
}
}
sql语句映射:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--多对多查询 -->
<!--第一种方法 -->
<select id="getAll" resultMap="getA" parameterType="Map">
<!-- select * from a aa left join b bb on aa.bid=bb.bid right JOIN c cc on aa.aid=cc.cid where 1=1 -->
select * from a aa left join c cc on aa.aid=cc.aid where 1=1
<choose>
<when test="aid!=null">
and aa.aid=${aid}
</when>
<when test="aname!=null">
and aa.aname=${aname}
</when>
</choose>
</select>
<resultMap type="A" id="getA">
<id column="aid" property="aid"/>
<result column="aname" property="aname"/>
<collection property="clist" javaType="ArrayList" ofType="C">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
</collection>
</resultMap>
<!--第二种方法 -->
<select id="getAll" resultMap="getA" parameterType="Map">
select * from a aa where 1=1
<choose>
<when test="aid!=null">
and aa.aid=${aid}
</when>
<when test="aname!=null">
and aa.aname=${aname}
</when>
</choose>
</select>
<resultMap type="A" id="getA">
<id column="aid" property="aid"/>
<result column="aname" property="aname"/>
<collection property="clist" column="aid"select="getC" javaType="java.util.List" ofType="C"/>
</resultMap>
<select id="getC" resultMap="StudentResultMap" parameterType="java.lang.Integer" >
select *
from C
where aid = #{aid}
</select>
<resultMap id="StudentResultMap" type="C">
<id column="cid" property="id"/>
<result column="cname" property="name"/>
</resultMap> -->
</mapper>
- mybatis多表查询
- mybatis多表查询
- MyBatis多表查询
- Mybatis多表查询
- mybatis多表查询
- 【Mybatis】多表查询
- mybatis多表查询
- mybatis多表复杂查询
- mybatis的多表查询
- mybatis多表查询操作
- mybatis多表关联查询
- mybatis多表关联查询
- MyBatis多表关联查询
- Mybatis的多表查询
- Mybatis多表关联查询
- spring-mybatis多表查询
- mybatis 多表联合查询
- mybatis分页多表查询
- transition
- 追逐物联网浪潮,LoRa定位技术多场景应用
- Educational Codeforces Round 34 (Rated for Div. 2) D. Almost Difference(高精度)
- HBuilder 入门(5) 编写一个登录页面
- 003Longest Substring Without Repeating Characters
- mybatis多表查询
- spring线程池ThreadPoolTaskExecutor应用
- #Import #include @class区别
- Android 应用程序没有权限时跳到应用的权限管理页面,让用户手动打开需要的权限
- 分类器准确率评估
- 文件流的基本使用方法
- Lintcode 35.翻转链表
- 数据结构与算法_插入排序
- ASP.NET程序员常用的85个工具