MyBatis 多表操作的實(shí)現(xiàn)
1.1 一對(duì)一查詢
1.1.1 概述
關(guān)系數(shù)據(jù)庫(kù)中第一個(gè)表中的單個(gè)行只可以與第二個(gè)表中的一個(gè)行相關(guān),且第二個(gè)表中的一個(gè)行也只可以與第一個(gè)表中的一個(gè)行相關(guān)。
1.1.2 創(chuàng)建實(shí)體類
public class Student { private Integer id; private String name; private Boolean age; private String sex; private StudentStatus studentStatus; // set and get }
public class StudentStatus { private Integer id; private String num; private String major; // set and get }
1.1.3 創(chuàng)建 DAO 接口
public class StudentStatus { private Integer id; private String num; private String major; // set and get }
1.1.4 結(jié)果映射
resultMap 元素是 MyBatis 中最重要最強(qiáng)大的元素。它可以從 90% 的 JDBC ResultSets 數(shù)據(jù)提取代碼中解放出來(lái),并在一些情形下允許進(jìn)行一些 JDBC 不支持的操作。實(shí)際上,在為一些比如連接的復(fù)雜語(yǔ)句編寫映射代碼的時(shí)候,一份 resultMap 能夠代替實(shí)現(xiàn)同等功能的長(zhǎng)達(dá)數(shù)千行的代碼。resultMap 的設(shè)計(jì)思想是,對(duì)于簡(jiǎn)單的語(yǔ)句根本不需要配置顯式的結(jié)果映射,而對(duì)于復(fù)雜一點(diǎn)的語(yǔ)句只需要描述它們的關(guān)系就行了。之前已經(jīng)使用過(guò)簡(jiǎn)單映射語(yǔ)句了,但并沒(méi)有顯式指定 resultMap。只是簡(jiǎn)單的使用 resultType 將所有的列映射到對(duì)象的屬性上,需要注意的是列名與屬性名一致才能映射,解決列名不匹配還是需要使用 resultMap。
<resultMap id="userResultMap" type="User"> <result property="id" column="user_id" /> <result property="username" column="user_name"/> <result property="password" column="hashed_password"/> </resultMap> <select id="selectUsers" resultMap="userResultMap"> select user_id, user_name, hashed_password from some_table where id = #{id} </select>
1.1.5 配置 mapper
<?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.software.mybatis.dao.StudentDao"> <resultMap id="resMap" type="student"> <result property="studentStatus.id" column="st_id"/> <result property="studentStatus.major" column="major"/> <result property="studentStatus.num" column="num"/> </resultMap> <select id="findAll" resultMap="resMap"> select * from student s, student_status st where s.st_id = st.st_id </select> </mapper>
上面這種配置會(huì)將自動(dòng)將列名一致的映射到 type 指定的實(shí)體類中,該實(shí)體類中屬性類型為對(duì)象的則需要單獨(dú)拿出來(lái)映射。還可以使用 association 進(jìn)行復(fù)雜的映射,我們發(fā)現(xiàn)未配置的屬性無(wú)法進(jìn)行映射。產(chǎn)生這個(gè)問(wèn)題的原因是 resultMap 的自動(dòng)映射未打開,使用 autoMapping 設(shè)置這個(gè)屬性為 true/false,MyBatis 將會(huì)為本結(jié)果映射開啟/關(guān)閉自動(dòng)映射。
<mapper namespace="com.software.mybatis.dao.StudentDao"> <resultMap id="resMap" type="com.software.mybatis.entity.Student"> <result property="id" column="id"/> <result property="name" column="name"/> <result property="sex" column="sex"/> <result property="age" column="age"/> <association property="studentStatus" javaType="com.software.mybatis.entity.StudentStatus"> <result property="id" column="st_id"/> <result property="major" column="major"/> <result property="num" column="num"/> </association> </resultMap> <select id="findAll" resultMap="resMap"> select * from student s, student_status st where s.st_id = st.st_id </select> </mapper>
<?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.software.mybatis.dao.StudentDao"> <resultMap id="resMap" type="student" autoMapping="true"> <association property="studentStatus" resultMap="stMap" /> </resultMap> <resultMap id="stMap" type="StudentStatus" autoMapping="true"/> <select id="findAll" resultMap="resMap"> select * from student s, student_status st where s.st_id = st.st_id </select> </mapper>
1.1.6 核心配置
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <typeAliases> <package name="com.software.mybatis.entity"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/db"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="student-mapper.xml"/> </mappers> </configuration>
1.1.7 測(cè)試
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description 測(cè)試類 */ public class MybatisDemo { @Test public void TestA() throws IOException { // 加載核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml"); // 獲得 sqlSession 工廠對(duì)象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); // 獲得 sqlSession 對(duì)象 SqlSession sqlSession = sqlSessionFactory.openSession(); List<Student> list = sqlSession.selectList("com.software.mybatis.dao.StudentDao.findAll"); System.out.println(list); } }
1.2 一對(duì)多查詢
1.2.1 概述
一對(duì)多關(guān)系是關(guān)系數(shù)據(jù)庫(kù)中兩個(gè)表之間的一種關(guān)系,該關(guān)系中第一個(gè)表中的單個(gè)行可以與第二個(gè)表中的一個(gè)或多個(gè)行相關(guān),但第二個(gè)表中的一個(gè)行只可以與第一個(gè)表中的一個(gè)行相關(guān)。
1.2.2 創(chuàng)建實(shí)體類
public class Student { private Integer sId; private String sName; private Long sAge; private String sSex; private Integer cId; // set and get }
public class Class { private Integer cId; private String cName; private String cAddr; private List<Student> students; // set and get }
1.1.3 創(chuàng)建 DAO 接口
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description DAO 接口 */ public interface ClassDao { public List<Class> findAll(); }
1.1.4 配置 mapper
<?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.software.mybatis.dao.ClassDao"> <resultMap id="resMap" type="Class"> <result property="cId" column="c_id"/> <result property="cName" column="c_name"/> <result property="cAddr" column="c_addr"/> <collection property="students" ofType="Student"> <result property="sId" column="s_id" /> <result property="sName" column="s_name"/> <result property="sAge" column="s_age"/> <result property="sSex" column="s_sex"/> <result property="cId" column="c_id"/> </collection> </resultMap> <select id="findAll" resultMap="resMap"> select * from student s, class c where s.c_id = c.c_id </select> </mapper>
1.1.5 測(cè)試
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description 測(cè)試類 */ public class MybatisDemo { @Test public void TestA() throws IOException { // 加載核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml"); // 獲得 sqlSession 工廠對(duì)象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); // 獲得 sqlSession 對(duì)象 SqlSession sqlSession = sqlSessionFactory.openSession(); List<Class> list = sqlSession.selectList("com.software.mybatis.dao.ClassDao.findAll"); for (Class aClass : list) { System.out.println(aClass); } } }
1.3 多對(duì)多查詢
1.3.1 概述
多對(duì)多關(guān)系是關(guān)系數(shù)據(jù)庫(kù)中兩個(gè)表之間的一種關(guān)系, 該關(guān)系中第一個(gè)表中的一個(gè)行可以與第二個(gè)表中的一個(gè)或多個(gè)行相關(guān)。第二個(gè)表中的一個(gè)行也可以與第一個(gè)表中的一個(gè)或多個(gè)行相關(guān)。該關(guān)系一般會(huì)借助第三方表實(shí)現(xiàn)。
1.3.2 創(chuàng)建實(shí)體類
public class Course { private Integer cId; private String cName; private List<Student> students; // set and get }
public class Student { private Integer sId; private String sName; private Long sAge; private String sSex; private List<Course> courses; // set and get }
1.3.3 創(chuàng)建 DAO 接口
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description course DAO 接口 */ public interface CourseDao { public List<Course> findAll(); }
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description student DAO 接口 */ public interface StudentDao { public List<Student> findAll(); }
1.3.4 配置 mapper
☞ student-mapper.xml
<?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.software.mybatis.dao.StudentDao"> <resultMap id="resMap" type="Student"> <result property="sId" column="s_id" /> <result property="sName" column="s_name"/> <result property="sAge" column="s_age"/> <result property="sSex" column="s_sex"/> <collection property="courses" ofType="Course"> <result property="cId" column="c_id"/> <result property="cName" column="c_name"/> </collection> </resultMap> <select id="findAll" resultMap="resMap"> select * from course c, student s, s_c sc where c.c_id = sc.c_id and s.s_id = sc.s_id </select> </mapper>
☞ course-mapper.xml
<?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.software.mybatis.dao.CourseDao"> <resultMap id="resMap" type="Course"> <result property="cId" column="c_id"/> <result property="cName" column="c_name"/> <collection property="students" ofType="Student"> <result property="sId" column="s_id" /> <result property="sName" column="s_name"/> <result property="sAge" column="s_age"/> <result property="sSex" column="s_sex"/> </collection> </resultMap> <select id="findAll" resultMap="resMap"> select * from course c, student s, s_c sc where c.c_id = sc.c_id and s.s_id = sc.s_id </select> </mapper>
1.3.5 測(cè)試
/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description 測(cè)試類 */ public class MybatisDemo { @Test public void TestA() throws IOException { // 加載核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml"); // 獲得 sqlSession 工廠對(duì)象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); // 獲得 sqlSession 對(duì)象 SqlSession sqlSession = sqlSessionFactory.openSession(); List<Course> courseList = sqlSession.selectList("com.software.mybatis.dao.CourseDao.findAll"); List<Student> studentList = sqlSession.selectList("com.software.mybatis.dao.StudentDao.findAll"); System.out.println("### 課程 ###"); for (Course course : courseList) { System.out.println(course); } System.out.println("### 學(xué)生 ###"); for (Student student : studentList) { System.out.println(student); } } }
到此這篇關(guān)于MyBatis 多表操作的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis 多表操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java排序之冒泡排序的實(shí)現(xiàn)與優(yōu)化
冒泡排序是一種簡(jiǎn)單的交換排序。之所以叫做冒泡排序,因?yàn)槲覀兛梢园衙總€(gè)元素當(dāng)成一個(gè)小氣泡,根據(jù)氣泡大小,一步一步移動(dòng)到隊(duì)伍的一端,最后形成一定對(duì)的順序。本文將利用Java實(shí)現(xiàn)冒泡排序,并進(jìn)行一定的優(yōu)化,希望對(duì)大家有所幫助2022-11-11SpringBoot整合達(dá)夢(mèng)數(shù)據(jù)庫(kù)的教程詳解
這篇文章主要給大家介紹了SpringBoot整合達(dá)夢(mèng)數(shù)據(jù)庫(kù)的詳細(xì)教程,文章中有詳細(xì)的圖片介紹和代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2023-08-08java中對(duì)象的強(qiáng)、軟、弱、虛四種引用詳解
這篇文章主要介紹了java中對(duì)象的強(qiáng)、軟、弱、虛四種引用詳解,對(duì)象的引用分為4種,分別是強(qiáng)引用>軟引用>弱引用>虛引用,程序員可以通過(guò)不同的引用控制對(duì)象的生命周期,方便垃圾回收,使程序更加靈活的控制對(duì)象生命周期,需要的朋友可以參考下2023-09-09SpringBoot啟動(dòng)執(zhí)行sql腳本的3種方法實(shí)例
在應(yīng)用程序啟動(dòng)后,可以自動(dòng)執(zhí)行建庫(kù)、建表等SQL腳本,下面這篇文章主要給大家介紹了關(guān)于SpringBoot啟動(dòng)執(zhí)行sql腳本的3種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01android顯示意圖激活另一個(gè)Activity的方法
下面小編就為大家?guī)?lái)一篇android顯示意圖激活另一個(gè)Activity的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹的核心方法
二叉搜索樹是一種常用的數(shù)據(jù)結(jié)構(gòu),它是一棵二叉樹,且每個(gè)節(jié)點(diǎn)的值都大于其左子樹中任何節(jié)點(diǎn)的值,而小于其右子樹中任何節(jié)點(diǎn)的值,這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹的核心方法,需要的朋友可以參考下2023-12-12