mybatis對映關係——多對多對映
阿新 • • 發佈:2019-02-12
多對多對映就是兩個實體之間是多對多的關係,例如老師和學生之間,可能是一個老師對應多個學生,同理一個學生可能對多個老師,那麼怎麼使用Mybatis程式怎麼寫出多對多關係???
1.先在entity包中新建兩個實體類,一個是學生類,另一個就是教師類,以學生類為主體,完成多表查詢操作:
student類
package com.baidu.lmj.entity;
public class TStudent {
private int id;
private String username;
private int cid;
private TTeachter tea;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public TTeachter getTea() {
return tea;
}
public void setTea(TTeachter tea) {
this.tea = tea;
}
}
teachter類
package com.baidu.lmj.entity;
import java.util.List;
public class TTeachter {
private int id;
private String tname;
private List<TStudent> list;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public List<TStudent> getList() {
return list;
}
public void setList(List<TStudent> list) {
this.list = list;
}
}
2.在dao包中分別建一個TTeachter.java和TStudent.java,並且定義一些常用的方法,方法如下所示:
TTeachter.java
package com.baidu.lmj.dao;
import java.util.List;
import com.baidu.lmj.entity.TTeachter;
public interface TTeachterdao {
public List<TTeachter> SelectAllTTeachter();
public List<TTeachter> SelectAllTTeachter1();
}
TStudent.java
package com.baidu.lmj.dao;
import java.util.List;
public interface TStudent {
public List<TStudent> SelectAllStudent();
public List<TStudent> SelectAllStudent1();
}
3.在entity包中建立TStudent.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.baidu.lmj.dao.TStudentDao">
<resultMap type="com.baidu.lmj.entity.TStudent" id="map01">
<id column="sid" property="id"/>
<result column="username" property="username"/>
<collection property="tea" ofType="com.baidu.lmj.entity.TTeachter">
<id column="tid" property="id"/>
<result column="tname" property="tname"/>
</collection>
</resultMap>
<select id="SelectAllStudent" resultMap="map01">
select a.id AS sid,username,c.id AS tid,c.tname from t_student a INNER JOIN t_s_t b ON a.id=b.sid INNER JOIN t_teacher c ON b.tid=c.id
</select>
<resultMap type="com.baidu.lmj.entity.TStudent" id="map02">
<id column="id" property="id"/>
<result column="username" property="username"/>
<collection property="tea" column="id" ofType="com.baidu.lmj.entity.TTeachter" select="id001">
<id column="id" property="id"/>
<result column="tname" property="tname"/>
</collection>
</resultMap>
<select id="SelectAllStudent1" resultMap="map02">
select * FROM t_student
</select>
<select id="id001" parameterType="int" resultType="com.baidu.lmj.entity.TTeachter">
select * FROM t_s_t a INNER JOIN t_teacher b ON a.tid=b.id WHERE a.sid=1
</select>
</mapper>
4.在biz包中新建TStudentBiz.java類,進行業務層構建
package com.baidu.lmj.biz;
import java.io.IOException;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.baidu.lmj.dao.TStudentDao;
import com.baidu.lmj.entity.TStudent;
import com.baidu.lmj.util.SessionFactory;
public class TStudentBiz {
TStudentDao dao;
public List<TStudent> getAllTStudent(){
List<TStudent> list=null;
try {
SqlSession session=SessionFactory.getSession();
dao=session.getMapper(TStudentDao.class);
list=dao.SelectAllStudent();
SessionFactory.ClosedSession(session);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public List<TStudent> getAllTStudent1(){
List<TStudent> list=null;
try {
SqlSession session=SessionFactory.getSession();
dao=session.getMapper(TStudentDao.class);
list=dao.SelectAllStudent1();
SessionFactory.ClosedSession(session);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
5.在Main.java類裡面進行測試並執行,測試程式碼如下所示:
package com.baidu.lmj.Main;
import java.util.List;
import com.baidu.lmj.biz.TStudentBiz;
import com.baidu.lmj.entity.TStudent;
import com.baidu.lmj.entity.TTeachter;
public class Main1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// TStudentBiz biz=new TStudentBiz();
//
// List<TStudent> list=biz.getAllTStudent();
//
// for(TStudent stu:list)
// {
// System.out.println("查詢的學生是"+stu.getUsername());
// System.out.println("他的教師是************************");
//
// for(TTeachter te:stu.getTea())
// {
// System.out.println(te.getTname());
// }
// System.out.println("****************結束********************");
// }
TStudentBiz biz=new TStudentBiz();
List<TStudent> list=biz.getAllTStudent1();
for(TStudent stu:list)
{
System.out.println("查詢的學生是"+stu.getUsername());
System.out.println("他的教師是************************");
for(TTeachter te:stu.getTea())
{
System.out.println(te.getTname());
}
System.out.println("****************結束********************");
}
}
}