1.MyBatis多表查詢
之前的博客在MyBatis第三章節中介紹了多表查詢多對一、一對多的情況,這里再對一對一、多堆多的情況做一下說明,也算是將MyBatis這部分的知識做一個補充和完整收尾的作業,
1.1.一對一查詢
這里基于一個簡單的用戶購買商品訂單表的案例來進行說明一對一查詢的模型:
用戶表user和訂單表的關系為:一個用戶有多個訂單,一個訂單只從屬于一個用戶,
一對一查詢的需求:查詢一個訂單,與此同時查詢出該訂單所屬的用戶:

對應資料庫中表的設計:
orders訂單表:

user用戶表:

對應創建物體類:
Order類:
public class Order { private int id; private String orderTime; private int total; private User user; //一個訂單從屬于一個用戶,針對于訂單與用戶這里是一對一的關系 }
User類:
public class User { private int id; private String username; private String password; private Date birthday; }
對應OrderMapper介面及xml中查詢sql封裝,需要注意的是使用Date_Format函式進行格式封裝輸出到對應物體order類,
可以將Date型別的ordertime進行標準格式化輸出:
public interface OrderMapper { //查詢出所有訂單,并一并查詢出訂單對應的用戶 List<Order> findAll(); }
<resultMap id="orderMap" type="order"> <id column="oid" property="id"></id> <result column="ordertime" property="orderTime"></result> <result column="total" property="total"></result> <result column="uid" property="user.id"></result> <result column="username" property="user.username"></result> <result column="password" property="user.password"></result> <result column="birthday" property="user.birthday"></result> </resultMap> <select id="findAll" resultMap="orderMap"> SELECT o.id oid, DATE_FORMAT(o.ordertime, '%Y-%m-%d %H:%i:%S') ordertime, o.total, u.id uid, u.username, u.password, u.birthday FROM orders o JOIN USER u ON o.uid = u.id </select>
其中<resultMap>還可以配置成如下,使用<association>進行User類的單獨封裝映射,注意這里使用了別名簡化配置,這里就不再累述:
<resultMap id="orderMap" type="order"> <id column="oid" property="id"></id> <result column="ordertime" property="orderTime"></result> <result column="total" property="total"></result> <association property="user" javaType="user"> <id column="uid" property="id"></id> <result column="username" property="username"></result> <result column="password" property="password"></result> <result column="birthday" property="birthday"></result> </association> </resultMap>
1.2.多對多查詢
多對多查詢一般設立中間表來關聯兩張主表,常見的模型是用戶User和對應的角色Role,
用戶表User和角色表Role的關系為,一個用戶有多個角色,一個角色被多個用戶使用,
多對多基于查詢需求來實作:查詢用戶表的同時查詢出該用戶對應的所有角色

資料庫表的簡單設計如下:
sys_user表(用戶表):

sys_role表(角色表):

sys_user_role表(中間表)

對應物體類創建,主要是在用戶domain中設計出多方角色的List<Role>屬性:
User類:
public class User { private int id; private String username; private String password; private Date birthday; /** * 查詢用戶同時查詢出它所有的角色 */ private List<Role> roleList; }
Role類:
public class Role { private int id; private String roleName; private String roleDesc; }
對應Mapper介面和xml中的sql封裝如下:
public interface UserMapper { /** * 查詢出所有用戶及其對應的角色 * @return */ List<User> findAllUserRole(); }
<?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.fengye.mapper.UserMapper"> <resultMap id="userRoleMap" type="user"> <id column="id" property="id"></id> <result column="username" property="username"></result> <result column="password" property="password"></result> <result column="birthday" property="birthday"></result> <!--用戶對應多個角色List使用collection: property是物體類對應的屬性, ofType是List中每個元素的型別 注意:使用ofType時,下面封裝的property中不能使用role.屬性; 注意區分一對一中type屬性的封裝剛好相反--> <collection property="roleList" ofType="role"> <id column="roleId" property="id"></id> <result column="roleName" property="roleName"></result> <result column="roleDesc" property="roleDesc"></result> </collection> </resultMap> <select id="findAllUserRole" resultMap="userRoleMap"> select * from sys_user u join sys_user_role ur on u.id = ur.userId join sys_role r on r.id = ur.roleId </select> </mapper>
其實基本上對應sql連接寫好了,封裝注意一下表與表連接的邏輯關系、從屬性,連接標簽條件的使用,基本上就是熟能生巧的問題了,
本節代碼示例已上傳至github地址:
https://github.com/devyf/MyBatisReview/tree/master/fengye_mybatis_multi
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/261611.html
標籤:Java
下一篇:java反射機制的學習心得
