前言:本篇小刘讲述mybatis关联语句的使用
1. 创建表结构如下:
2. 实体类部分代码:
-
2.1 Wife类:
public class Wife {
private Integer id;
private String name;
private Integer userId;
。。。。。。。。。。。。。。。
}
-
2.2 User类:
public class User {
private Integer id;
private String username;
private String hobby;
private Date birthday;
/**
* 一对一,一个user只有一个妻子
*/
private Wife wife;
/**
* 一对多:一个user有多个订单
*/
private List<Order>orders;
...............................
}
-
2.3 Order类:
public class Order {
private Integer id;
private String orderName;
private Integer userId;
private Date orderTime;
private User user;
.......................
}
3. 接口类
-
3.1 UserDao
/**
* 找到所有的用户即包含一对一,也包含一对多
* @return
*/
List<User>findAll();
/***
* 找出一对一的妻子
* @return
*/
List<User>findAllOne();
-
3.2 UserMapper
<?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.zjitc.dao.UserDao">
<resultMap id="users" type="com.zjitc.pojo.entity.User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="hobby" column="hobby"/>
<result property="birthday" column="birthday"/>
<!--
一对一:一个用户只有一个妻子,关联查询
多对一:也采用关联查询
-->
<association property="wife" javaType="com.zjitc.pojo.entity.Wife">
<id property="id" column="wife_id"/>
<result property="name" column="name"/>
<result property="userId" column="user_id"/>
</association>
<!--
一对多采用collection left join:一个用户含有多个订单
多对一:采用关联association
-->
<collection property="orders" column="user_id" ofType="com.zjitc.pojo.entity.Order">
<id column="order_id" property="id"/><!--一对多,主键相同必须用别名-->
<result property="orderName" column="order_name"/>
<result property="orderTime" column="order_time"/>
<result property="userId" column="user_id"/>
</collection>
</resultMap>
<resultMap id="xx" type="com.retailo2o.smc.Do.xx" extends="BaseResultMap">
<result column="cust_name" jdbcType="VARCHAR" property="customerName"></result>
<collection property="detailList"
select="com.retailo2o.smc.mapper.WsPreDetailMapper.xx"
column="{billNumber=bill_number,platformNum=platform_num}"></collection>
</resultMap>
<!--
一对多,left join
-->
<select id="findAll" resultMap="users">
select u.*,o.id order_id,o.order_name,o.order_time,o.user_id
,
w.* from user as u
left join `order` as o
on o.user_id=u.id left join wife w on u.id = w.user_id
</select>
<select id="findAllOne" resultMap="users">
select s.*,w.* from user as s left join wife as w on s.id=w.user_id;
</select>
</mapper>
3.3 多对一:
public interface OrderDao {
/**
* 找到所有的订单
* @return
*/
List<Order>findAll();
}
3.4 OrderDaoMapper
<?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.zjitc.dao.OrderDao">
<resultMap id="order" type="com.zjitc.pojo.entity.Order">
<id property="id" column="id"/>
<result property="orderName" column="order_name"/>
<result property="orderTime" column="order_time"/>
<result property="userId" column="user_id"/>
<!--
多对一和一对一没啥区别,看查询的数据差别
:association关联查询,多个订单有同一个一个user
-->
<association property="user" javaType="com.zjitc.pojo.entity.User">
<id column="id" property="id"/>
<result property="username" column="username"/>
<result property="hobby" column="hobby"/>
<result property="birthday" column="birthday"/>
</association>
</resultMap>
<select id="findAll" resultMap="order">
select o.*,u.id,u.username,u.hobby,u.birthday from `order` as o left join user as u on o.user_id=u.id;
</select>
</mapper>
<resultMap type="com.qiruo.yuejuan.entity.ExamDTO" id="uniteSingleExamResultMap"> <association property="twoExam" column="{examId=id}" javaType="com.qiruo.yuejuan.entity.ExamDTO" select="queryTwoExamInfo" /> <collection property="questionNumber" column="{examId=id}" ofType="int" select="queryQuestionNumber"></collection> <collection property="succeeNumber" column="{examId=id}" ofType="int" select="querysubSucceeNumber"></collection> <!--联考单科时查询所有参与学校考试信息--> <collection property="uniteExamList" column="{examId=id,singleSubjectExamIds=singleSubjectExamIds,examNum=examNum}" ofType="int" select="queryUniteExamList"></collection> <!--联考单科时查询参与学校信息列表--> <collection property="schoolList" column="{examId=id,examNum=examNum}" ofType="com.qiruo.yuejuan.entity.SchoolVO" select="querySchoolListOfUniteSingleSubject"></collection> </resultMap>
多对多:一般转换为一对多,多对一的处理方式:
特别注意:
mysql连接的方式:
三种基本表连接方式:
- 外连接
- 内连接
- 交叉连接(笛卡尔积)
外连接:包含左外连接(left join或者left out join),右外连接(right join或者right out join),全外连接(full join或者full out join)
左外连接包括left join左表所有行。假设左表中某行在右表没有匹配。则结果中相应行右表的部分所有为空(NULL).
注:此时我们不能说结果的行数等于左表数据的行数。
右外连接包括right join右表所有行,假设左表中某行在右表没有匹配,则结果中相应左表的部分所有为空(NULL)。
注:相同此时我们不能说结果的行数等于右表的行数。
全然外连接包括full join左右两表中所有的行,假设右表中某行在左表中没有匹配,则结果中相应行右表的部分所有为空(NULL),假设左表中某行在右表中没有匹配,则结果中相应行左表的部分所有为空(NULL)。
内连接:join或者inner join
inner join 是比較运算符,仅仅返回符合条件的行。
交叉连接:cross join
概念:没有 WHERE 子句的交叉联接将产生连接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。
结果和inner join所看到的运行结果一样。
如有不解,请加java爱好群大家交流:852665736;群里都是热心好客的小伙伴,大家一同进步。