咱們在學習Hibernate的時候,若是表涉及到兩張的話,那麼咱們是在映射文件中使用<set>
..<many-to-one>
等標籤將其的映射屬性關聯起來的...那麼在咱們Mybatis中又怎麼作呢???java
先來回顧一下咱們SQL99的語法:mysql
一)內鏈接(等值鏈接):查詢客戶姓名,訂單編號,訂單價格 --------------------------------------------------- select c.name,o.isbn,o.price from customers c inner join orders o where c.id = o.customers_id; --------------------------------------------------- select c.name,o.isbn,o.price from customers c join orders o where c.id = o.customers_id; --------------------------------------------------- select c.name,o.isbn,o.price from customers c,orders o where c.id = o.customers_id; --------------------------------------------------- select c.name,o.isbn,o.price from customers c join orders o on c.id = o.customers_id; --------------------------------------------------- 注意:內鏈接(等值鏈接)只能查詢出多張表中,鏈接字段相同的記錄 二)外鏈接:按客戶分組,查詢每一個客戶的姓名和訂單數 --------------------------------------------------- 左外鏈接: select c.name,count(o.isbn) from customers c left outer join orders o on c.id = o.customers_id group by c.name; --------------------------------------------------- 右外鏈接: select c.name,count(o.isbn) from orders o right outer join customers c on c.id = o.customers_id group by c.name; --------------------------------------------------- 注意:外鏈接既能查詢出多張表中,鏈接字段相同的記錄;又能根據一方,將另外一方不符合相同記錄強行查詢出來 三)自鏈接:求出AA的老闆是EE --------------------------------------------------- 內自鏈接: select users.ename,boss.ename from emps users inner join emps boss on users.mgr = boss.empno; --------------------------------------------------- 外自鏈接: select users.ename,boss.ename from emps users left outer join emps boss on users.mgr = boss.empno; --------------------------------------------------- 注意:自鏈接是將一張表,經過別名的方式,看做多張表後,再進行鏈接。 這時的鏈接便可以採用內鏈接,又能夠採用外鏈接
因爲咱們Mybatis中並無像Hibernate這樣全自動化的,所以咱們是沒有<set>
..<many-to-one>
等標籤的,咱們仍是使用手寫SQL語句來使咱們的關聯屬性鏈接起來...android
需求:sql
--mysql create table cards( cid int(5) primary key, cnum varchar(10) ); create table students( sid int(5) primary key, sname varchar(10), scid int(5), constraint scid_fk foreign key(scid) references cards(cid) ); insert into cards(cid,cnum) values(1,'111'); insert into students(sid,sname,scid) values(1,'哈哈',1); select * from cards; select * from students;
/** * 身份證(單方) * @author AdminTC */ public class Card { private Integer id; private String num; public Card(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } }
/** * 學生(單方) * @author AdminTC */ public class Student { private Integer id; private String name; private Card card;//關聯屬性 public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } }
因爲咱們有兩個實體,所以咱們會有兩個映射文件數據庫
Student映射文件apache
<?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="studentNamespace"> <resultMap type="zhongfucheng2.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> </resultMap> </mapper>
Card映射文件微信
<?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="cardNamespace"> <resultMap type="zhongfucheng2.Card" id="cardMap"> <id property="id" column="cid"/> <result property="num" column="cnum"/> </resultMap> </mapper>
如今我想根據學生的編號查詢學生的信息和身份證信息!session
因爲該查詢着重是查詢學生的信息,因而咱們在學生的映射文件中寫SQL語句mybatis
按照需求,咱們寫出來的SQL語句是這樣子的。app
select * from zhongfucheng.students s,zhongfucheng.cards c where c.cid = s.scid and sid=1;
我來看一下查詢結果:
咱們的實體與映射表中,Student實體是沒有關聯其餘的字段的,僅僅是寫出了該實體的自帶的屬性。
<resultMap type="zhongfucheng2.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> </resultMap>
明顯地,咱們Student是不能封裝返回的結果,所以咱們須要將關聯屬性進行關聯起來!
<resultMap type="zhongfucheng2.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> <!-- property寫的是在Student實體中寫關聯字段的屬性變量名稱 resultMap寫的是映射文件中的命名空間.id --> <association property="card" resultMap="cardNamespace.cardMap"/> </resultMap>
咱們關聯了之後,Student實體就可以封裝返回的結果了
<resultMap type="zhongfucheng2.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> <!-- property寫的是在Student實體中寫關聯字段的屬性變量名稱 resultMap寫的是映射文件中的命名空間.id --> <association property="card" resultMap="cardNamespace.cardMap"/> </resultMap> <select id="findById" parameterType="int" resultMap="studentMap"> select * from zhongfucheng.students s,zhongfucheng.cards c where c.cid = s.scid and sid=#{id}; </select>
查詢編號爲1的學生信息【包括身份證編號】
public Student findById(int id) throws Exception { //獲得鏈接對象 SqlSession sqlSession = MybatisUtil.getSqlSession(); try{ return sqlSession.selectOne("studentNamespace.findById", id); /* sqlSession.commit();*/ }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } public static void main(String[] args) throws Exception { StudentDao studentDao = new StudentDao(); Student student = studentDao.findById(1); System.out.println(student.getId() + "----" + student.getName() + "----" + student.getCard().getNum()); }
需求:
create table grades( gid int(5) primary key, gname varchar(10) ); create table students( sid int(5) primary key, sname varchar(10), sgid int(5), constraint sgid_fk foreign key(sgid) references grades(gid) ); insert into grades(gid,gname) values(1,'java'); insert into students(sid,sname,sgid) values(1,'哈哈',1); insert into students(sid,sname,sgid) values(2,'呵呵',1); select * from grades; select * from students;
package zhongfucheng2; import java.util.ArrayList; import java.util.List; /** * 學科(單方) * @author AdminTC */ public class Grade { private Integer id; private String name; private List<Student> studentList = new ArrayList<Student>();//關聯屬性 public Grade(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } }
package zhongfucheng2; /** * 學生(多方) * @author AdminTC */ public class Student { private Integer id; private String name; private Grade grade;//關聯屬性 public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } }
<mapper namespace="studentNamespace"> <resultMap type="zhongfucheng2.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> </resultMap> <!--查詢選修的java學科有多少位學生--> <!--因爲咱們只要查詢學生的名字,而咱們的實體studentMap能夠封裝學生的名字,那麼咱們返回studentMap便可,並不須要再關聯到學科表--> <select id="findByGrade" parameterType="string" resultMap="studentMap"> select s.sname,s.sid from zhongfucheng.students s,zhongfucheng.grades g WHERE s.sgid=g.gid and g.gname=#{name}; </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="gradeNamespace"> <resultMap type="zhongfucheng2.Grade" id="gradeMap"> <id property="id" column="gid"/> <result property="name" column="gname"/> </resultMap> </mapper>
public List<Student> findByGrade(String grade) throws Exception { //獲得鏈接對象 SqlSession sqlSession = MybatisUtil.getSqlSession(); try{ return sqlSession.selectList("studentNamespace.findByGrade", grade); /* sqlSession.commit();*/ }catch(Exception e){ e.printStackTrace(); sqlSession.rollback(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } public static void main(String[] args) throws Exception { StudentDao studentDao = new StudentDao(); List<Student> student = studentDao.findByGrade("java"); for (Student student1 : student) { System.out.println(student1.getName()); } }
需求:
create table students( sid int(5) primary key, sname varchar(10) ); create table courses( cid int(5) primary key, cname varchar(10) ); create table middles( msid int(5), mcid int(5), primary key(msid,mcid) ); insert into students(sid,sname) values(1,'哈哈'); insert into students(sid,sname) values(2,'呵呵'); insert into courses(cid,cname) values(1,'java'); insert into courses(cid,cname) values(2,'android'); insert into middles(msid,mcid) values(1,1); insert into middles(msid,mcid) values(1,2); insert into middles(msid,mcid) values(2,1); insert into middles(msid,mcid) values(2,2); select * from students; select * from courses; select * from middles;
package cn.itcast.javaee.mybatis.many2many; import java.util.ArrayList; import java.util.List; /** * 課程(多方) * @author AdminTC */ public class Course { private Integer id; private String name; private List<Student> studentList = new ArrayList<Student>();//關聯屬性 public Course(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } }
package cn.itcast.javaee.mybatis.many2many; import java.util.ArrayList; import java.util.List; /** * 學生(多方) * @author AdminTC */ public class Student { private Integer id; private String name; private List<Course> courseList = new ArrayList<Course>();//關聯屬性 public Student(){} public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Course> getCourseList() { return courseList; } public void setCourseList(List<Course> courseList) { this.courseList = courseList; } }
<?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="courseNamespace"> <resultMap type="cn.itcast.javaee.mybatis.many2many.Course" id="courseMap"> <id property="id" column="cid"/> <result property="name" column="cname"/> </resultMap> <!-- 查詢哈哈選學了哪些課程 --> <select id="findAllByName" parameterType="string" resultMap="courseMap"> select c.cid,c.cname from students s inner join middles m on s.sid = m.msid inner join courses c on m.mcid = c.cid and s.sname = #{name} </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="studentNamespace"> <resultMap type="cn.itcast.javaee.mybatis.many2many.Student" id="studentMap"> <id property="id" column="sid"/> <result property="name" column="sname"/> </resultMap> <select id="findAllByCourseName" parameterType="string" resultMap="studentMap"> select s.sname from students s inner join middles m on s.sid = m.msid inner join courses c on m.mcid = c.cid and c.cname = #{name} </select> </mapper>
package cn.itcast.javaee.mybatis.many2many; import java.util.List; import org.apache.ibatis.session.SqlSession; import cn.itcast.javaee.mybatis.util.MybatisUtil; /** * 持久層 * @author AdminTC */ public class StudentCourseDao { /** * 查詢哈哈選學了哪些課程 * @param name 表示學生的姓名 */ public List<Course> findAllByName(String name) throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); return sqlSession.selectList("courseNamespace.findAllByName",name); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } /** * 查詢java課程有哪些學生選修 * @param name 表示學生的課程 */ public List<Student> findAllByCourseName(String name) throws Exception{ SqlSession sqlSession = null; try{ sqlSession = MybatisUtil.getSqlSession(); return sqlSession.selectList("studentNamespace.findAllByCourseName",name); }catch(Exception e){ e.printStackTrace(); throw e; }finally{ MybatisUtil.closeSqlSession(); } } public static void main(String[] args) throws Exception{ StudentCourseDao dao = new StudentCourseDao(); List<Course> courseList = dao.findAllByName("哈哈"); System.out.print("哈哈選學了" + courseList.size()+"個課程,分別是:"); for(Course c : courseList){ System.out.print(c.getName()+" "); } System.out.println("\n-----------------------------------------------------"); List<Student> studentList = dao.findAllByCourseName("android"); System.out.println("選修了android課程的學生有"+studentList.size()+"個,分別是:"); for(Student s : studentList){ System.out.print(s.getName()+" "); } } }
對於Mybatis的多表鏈接就很是簡單了,因爲SQL語句全是由咱們本身寫,若是咱們返回的數據類型在當前的實體中是不夠封裝的話,那麼咱們只要再關聯對應的映射屬性就好了!
若是文章有錯的地方歡迎指正,你們互相交流。 習慣在微信看技術文章,想要獲取更多的Java資源的同窗,能夠關注微信公衆號:Java3y