您好,登錄后才能下訂單哦!
使用Mybatis 如何實現多對一或一對多的關聯查詢?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
首先 數據庫量表之間字段關系(沒有主外鍵)
studentmajor表的id字段對應student表里major字段
兩個實體類
package com.model; import java.util.Date; public class Student { private Integer sno; private String sname; private String ssex; private Integer sclass; private StudentMajor studentmajor; public Student() { super(); } public Student(Integer sno, String sname, String ssex, Integer sclass, StudentMajor studentmajor) { super(); this.sno = sno; this.sname = sname; this.ssex = ssex; this.sclass = sclass; this.studentmajor = studentmajor; } public StudentMajor getStudentmajor() { return studentmajor; } public void setStudentmajor(StudentMajor studentmajor) { this.studentmajor = studentmajor; } public Integer getSno() { return sno; } public void setSno(Integer sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } @Override public String toString() { return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sclass=" + sclass + ", studentmajor=" + studentmajor + "]"; } public Integer getSclass() { return sclass; } public void setSclass(Integer sclass) { this.sclass = sclass; } }
package com.model; import java.util.List; public class StudentMajor { private Integer id; private String mcode; private String mname; private List<Student> students; public StudentMajor() { super(); } public StudentMajor(Integer id, String mcode, String mname, List<Student> students) { super(); this.id = id; this.mcode = mcode; this.mname = mname; this.students = students; } @Override public String toString() { return "StudentMajor [id=" + id + ", mcode=" + mcode + ", mname=" + mname + ", students=" + students + "]"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMcode() { return mcode; } public void setMcode(String mcode) { this.mcode = mcode; } public String getMname() { return mname; } public void setMname(String mname) { this.mname = mname; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
定義兩個接口
package com.dao; import java.util.List; import java.util.Map; import com.model.Student; public interface StudentMapper { /** * 全表查詢 */ public List<Student> selectall(); /** * 根據專業查人員,給一對多用 */ public List<Student> selectz(Integer major); }
package com.dao; import java.util.List; import com.model.StudentMajor; public interface StudentMajorMapper { /** * 全表查詢 * @return */ public List<StudentMajor> selectAll(); /** * 根據主鍵查數據,給多對一用 * @param id * @return */ public StudentMajor select(Integer id); }
定義兩個實體類的映射方法
<?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.dao.StudentMapper"> <!-- 多對一查詢 --> <resultMap type="Student" id="slist"> <!-- 跟一對一一樣用association標簽,實體類定義的成員,要跟數據庫字段名對應上 --> <association property="studentmajor" column="major" select="com.dao.StudentMajorMapper.select"/> <!-- 用接口里定義的方法,根據student表中的major字段查出對應數據 --> </resultMap> <!-- 查全部 --> <select id="selectall" resultMap="slist" > select * from student </select> <!-- 根據專業查人員 --> <select id="selectz" parameterType="Integer" resultType="student"> select * from student s where s.major=#{major} </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.dao.StudentMajorMapper"> <!-- 一對多查詢關聯 --> <resultMap type="StudentMajor" id="slist"> <!-- 實體類屬性對應數據庫的主鍵字段,不然主鍵會查不到 --> <id property="id" column="id"/> <!-- 用collection標簽 ,也是實體類屬性要對應數據庫字段--> <collection property="students" column="id" select="com.dao.StudentMapper.selectz"> </collection> </resultMap> <!-- 全表查詢 --> <select id="selectAll" resultMap="slist"> select * from studentmajor </select> <!-- 根據主鍵查 --> <select id="select" parameterType="Integer" resultType="StudentMajor"> select * from studentmajor where id=#{id} </select> </mapper>
JUnit測試
package com.util; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.dao.StudentMajorMapper; import com.dao.StudentMapper; import com.model.Student; import com.model.StudentMajor; public class JJJtest { private SqlSession ss; private StudentMapper sm; private StudentMajorMapper smm; @Before public void setUp() throws Exception { ss=SqlSessionUtil.getSqlSession(); sm=ss.getMapper(StudentMapper.class); smm=ss.getMapper(StudentMajorMapper.class); } @After public void tearDown() throws Exception { ss.commit(); ss.close(); } //一對多查詢 public void test() { List<StudentMajor> list=smm.selectAll(); for(StudentMajor a:list){ System.out.println(a); } } //根據專業查人員,給一對多用 public void selectz(){ List<Student> l=sm.selectz(3); for(Student a:l){ System.out.println(a); } } //多對一查詢 @Test public void selectall() { List<Student> st=sm.selectall(); for(Student tt:st){ System.out.println(tt); } } //根據主鍵查詢,給多對一用 public void select(){ StudentMajor a=smm.select(1); System.out.println(a); } }
一對多查詢結果
多對一查詢結果
關于使用Mybatis 如何實現多對一或一對多的關聯查詢問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。