Mybatis—多表查詢

Mybatis多表查詢

一對一查詢

一對一查詢的模型MapperScannerConfigurer

用戶表和訂單表的關系為,一個用戶有多個訂單,一個訂單只從屬于一個用戶

創建Order和User實體
public class Order {private int id;private Date ordertime;private double total;//代表當前訂單從屬于哪一個客戶private User user;
}public class User {private int id;private String username;private String password;private Date birthday;}
創建接口
public interface OrderMapper {List<Order> findAll();
}
配置OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.controller.OrderMapper">
<resultMap id="orderMap" type="com.model.Order"><result column="id" property="id" /><result column="ordertime" property="ordertime"/><result column="total" property="total"/><association property="user" javaType="com.model.User"><result column="uid" property="id"/><result column="username" property="username"/><result column="password" property="password"/><result column="birthday" property="birthday"/></association>
</resultMap><select id="findAll" resultMap="orderMap">select * from  orders o,user u where o.uid=u.id</select>
</mapper>
測試結果
    @Testpublic void tset4() {InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);List<Order> all = mapper.findAll();System.out.println(all);}

一對多查詢

一對多查詢的模型

用戶表和訂單表的關系為,一個用戶有多個訂單,一個訂單只從屬于一個用戶

一對多查詢的需求:查詢一個用戶,與此同時查詢出該用戶具有的訂單

1.2.3 修改User實體
public class Order {private int id;private Date ordertime;private double total;//代表當前訂單從屬于哪一個客戶private User user;
}public class User {private int id;private String username;private String password;private Date birthday;//代表當前用戶具備哪些訂單private List<Order> orderList;
}
創建UserMapper接口
public interface UserMapper {List<User> findAll();
}
配置UserMapper.xml
    <resultMap id="userMap" type="com.model.User"><result column="username" property="username" /><result column="password" property="password"/><result column="id" property="id"/><result column="birthday" property="birthday"/><collection property="orderList" ofType="com.model.Order"><result property="id" column="oid"/><result property="ordertime" column="ordertime"/><result property="total" column="total"/></collection></resultMap><select id="findAll" resultMap="userMap">select * ,o.id oid from  user u left join  orders o on u.id=o.uid</select>
測試結果
    @Testpublic void tset4() {InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);userMapper mapper = sqlSession.getMapper(userMapper.class);List<User> all = mapper.findAll();System.out.println(all);}

多對多查詢

多對多查詢的模型

用戶表和角色表的關系為,一個用戶有多個角色,一個角色被多個用戶使用

多對多查詢的需求:查詢用戶同時查詢出該用戶的所有角色

創建Role實體,修改User實體
public class User {private int id;private String username;private String password;private Date birthday;//代表當前用戶具備哪些訂單private List<Order> orderList;//代表當前用戶具備哪些角色private List<Role> roleList;
}public class Role {private int id;private String rolename;}
添加UserMapper接口方法
List<User> findAllUserAndRole();
配置UserMapper.xml
    <resultMap id="roleMap" type="com.model.User"><result column="username" property="username" /><result column="password" property="password"/><result column="id" property="id"/><result column="birthday" property="birthday"/><collection property="roleList" ofType="com.model.Role"><result property="id" column="rid"/><result property="rolename" column="rolename"/></collection></resultMap><select id="findAllUserAndRole" resultMap="roleMap">select u.*,r.id rid,r.* from user u left join  sys_user_role ur on u.id=ur.userid inner join  sys_role r on r.id=ur.roleid</select>
測試結果
    @Testpublic void tset4() {InputStream resourceAsStream = null;try {resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");} catch (IOException ioException) {ioException.printStackTrace();}SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession = sqlSessionFactory.openSession(true);userMapper mapper = sqlSession.getMapper(userMapper.class);List<User> all = mapper.findAllUserAndRole();System.out.println(all);}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391799.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391799.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391799.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

VS2008 開發設計MOSS工作流 URN 注意了

最近學習MOSS 很苦惱&#xff0c;進度也很慢&#xff0c;最近在學習VS2008開發工作流&#xff0c;其中有結合INFOPATH 2007來做, 出現個BUG或者說是設置的問題,整整花了我一天工作時間&#xff0c;是這樣的: 在部署的時候關于URN&#xff0c;大部分的教程都是這樣的說的&#…

ArangoDB Foxx service 使用

備注&#xff1a;項目使用的是github https://github.com/arangodb-foxx/demo-hello-foxx1. git clonegit clone https://github.com/arangodb-foxx/demo-hello-foxx.git 2. 安裝foxx servicefoxx-manager install demo-hello-foxx /demoapp 3. 效果自動生成的swagger 文檔項目…

編譯原理 數據流方程_數據科學中最可悲的方程式

編譯原理 數據流方程重點 (Top highlight)Prepare a box of tissues! I’m about to drop a truth bomb about statistics and data science that’ll bring tears to your eyes.準備一盒紙巾&#xff01; 我將投放一本關于統計和數據科學的真相炸彈&#xff0c;這會讓您眼淚汪…

@ConTrollerAdvice的使用

ConTrollerAdvice&#xff0c;從名字上面看是控制器增強的意思。 在javaDoc寫到/*** Indicates the annotated class assists a "Controller".** <p>Serves as a specialization of {link Component Component}, allowing for* implementation classes to be a…

Mybatis—注解開發

Mybatis的注解開發 MyBatis的常用注解 這幾年來注解開發越來越流行&#xff0c;Mybatis也可以使用注解開發方式&#xff0c;這樣我們就可以減少編寫Mapper映射文件了。 Insert&#xff1a;實現新增 Update&#xff1a;實現更新 Delete&#xff1a;實現刪除 Select&#x…

道路工程結構計算軟件_我從軟件工程到產品管理的道路

道路工程結構計算軟件by Sari Harrison莎莉哈里森(Sari Harrison) 我從軟件工程到產品管理的道路 (My path from software engineering to product management) 以及一些有關如何自己做的建議 (And some advice on how to do it yourself) I am often asked how to make the m…

Vue 指令

下面列舉VUE的HTML頁面模板指令&#xff0c;并進行分別練習。 1. templates 2. v-if, v-for <div idapp><ol><li v-for"todo in todos>{{ todo.text}}</li></ol> </div><script>app new Vue({ el: #app, data: { return…

iOS-FMDB

2019獨角獸企業重金招聘Python工程師標準>>> #import <Foundation/Foundation.h> #import <FMDatabase.h> #import "MyModel.h"interface FMDBManager : NSObject {FMDatabase *_dataBase; }(instancetype)shareInstance;- (BOOL)insert:(MyM…

解決朋友圈壓縮_朋友中最有趣的朋友[已解決]

解決朋友圈壓縮We live in uncertain times.我們生活在不確定的時代。 We don’t know when we’re going back to school or the office. We don’t know when we’ll be able to sit inside at a restaurant. We don’t even know when we’ll be able to mosh at a Korn co…

西安項目分析

西安物流 西安高考補習 西安藝考 轉載于:https://www.cnblogs.com/wpxuexi/p/7294269.html

MapServer應用開發平臺示例

MapServer為當前開源WebGIS的應用代表&#xff0c;在西方社會應用面極為廣泛&#xff0c;現介紹幾個基于它的開源應用平臺。 1.GeoMOOSE GeoMoose is a Web Client Javascript Framework for displaying distributed cartographic data. Among its many strengths, it can hand…

leetcode 995. K 連續位的最小翻轉次數(貪心算法)

在僅包含 0 和 1 的數組 A 中&#xff0c;一次 K 位翻轉包括選擇一個長度為 K 的&#xff08;連續&#xff09;子數組&#xff0c;同時將子數組中的每個 0 更改為 1&#xff0c;而每個 1 更改為 0。 返回所需的 K 位翻轉的最小次數&#xff0c;以便數組沒有值為 0 的元素。如果…

kotlin數據庫_如何在Kotlin應用程序中使用Xodus數據庫

kotlin數據庫I want to show you how to use one of my favorite database choices for Kotlin applications. Namely, Xodus. Why do I like using Xodus for Kotlin applications? Well, here are a couple of its selling points:我想向您展示如何在Kotlin應用程序中使用我…

使用route add添加路由,使兩個網卡同時訪問內外網

route add命令格式&#xff1a;route [-f] [-p] [Command] [Destination] [mask Netmask] [Gateway] [metric Metric] [if Interface] 通過配置電腦的靜態路由來實現同時訪問內外網的。電腦的網絡IP配置不用變&#xff0c;兩個網卡都按照正常配置&#xff08;都配置IP地址、子網…

基于JavaConfig配置的Spring MVC的構建

上一篇講了基于XML配置的構建&#xff0c;這一篇講一講基于JavaConfig的構建。為什么要寫這篇文章&#xff0c;因為基于xml配置的構建&#xff0c;本人認為很麻煩&#xff0c;要寫一堆的配置&#xff0c;不夠簡潔&#xff0c;而基于JavacConfig配置的構建符合程序員的編碼習慣&…

pymc3 貝葉斯線性回歸_使用PyMC3進行貝葉斯媒體混合建模,帶來樂趣和收益

pymc3 貝葉斯線性回歸Michael Johns, Zhenyu Wang, Bruno Dupont, and Luca Fiaschi邁克爾約翰斯&#xff0c;王振宇&#xff0c;布魯諾杜邦和盧卡菲亞斯基 “If you can’t measure it, you can’t manage it, or fix it”“如果無法衡量&#xff0c;就無法管理或修復它” –…

webkit中對incomplete type指針的處理技巧

近日在研究webkit的時候發現了一個函數 template<typename T> inline void deleteOwnedPtr(T* ptr) {typedef char known[sizeof(T) ? 1 : -1];if(sizeof(known))delete ptr; } 一開始對這個函數非常費解&#xff0c;為什么作者不直接 delete ptr; 通過上stackoverflow提…

leetcode 1004. 最大連續1的個數 III(滑動窗口)

給定一個由若干 0 和 1 組成的數組 A&#xff0c;我們最多可以將 K 個值從 0 變成 1 。 返回僅包含 1 的最長&#xff08;連續&#xff09;子數組的長度。 示例 1&#xff1a; 輸入&#xff1a;A [1,1,1,0,0,0,1,1,1,1,0], K 2 輸出&#xff1a;6 解釋&#xff1a; [1,1,1…

我如何找到工作并找到理想的工作

By Julius Zerwick朱利葉斯澤威克(Julius Zerwick) This article is about how I went through my job hunt for a full time position as a software engineer in New York City and ended up with my dream job. I had spent two years building my skills and had aspirati…

synchronized 與 Lock 的那點事

synchronized 與 Lock 的那點事 最近在做一個監控系統&#xff0c;該系統主要包括對數據實時分析和存儲兩個部分&#xff0c;由于并發量比較高&#xff0c;所以不可避免的使用到了一些并發的知識。為了實現這些要求&#xff0c;后臺使用一個隊列作為緩存&#xff0c;對于請求只…