Mybatis多表模型
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Mybatis多表模型
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                多表模型:
- 多表模型分類 一對一:在任意一方建立外鍵,關(guān)聯(lián)對方的主鍵。
- 一對多:在多的一方建立外鍵,關(guān)聯(lián)一的一方的主鍵。
- 多對多:借助中間表,中間表至少兩個字段,分別關(guān)聯(lián)兩張表的主鍵。
多表模型一對一操作:
核心配置文件
<!--起別名--><typeAliases><package name="com.duobiao.bean"/></typeAliases> <!-- mappers引入映射配置文件 --><mappers><!-- mapper 引入指定的映射配置文件 resource屬性指定映射配置文件的名稱 --><mapper resource="OneToOneMapper.xml"/></mappers>一對多:
sql數(shù)據(jù):
CREATE TABLE classes(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20) ); INSERT INTO classes VALUES (NULL,'一班'); INSERT INTO classes VALUES (NULL,'二班');CREATE TABLE student(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(30),age INT,cid INT,CONSTRAINT cs_fk FOREIGN KEY (cid) REFERENCES classes(id) ); INSERT INTO student VALUES (NULL,'張三',23,1); INSERT INTO student VALUES (NULL,'李四',24,1); INSERT INTO student VALUES (NULL,'王五',25,2); INSERT INTO student VALUES (NULL,'趙六',26,2);bean:
public class Student {private Integer id;private String name;private Integer age; }public class Classes {// 主鍵idprivate Integer id;// 班級名private String name;// 班級中所有的學生private List<Student> students; }配置文件:
<?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.table.OneToManyMapper"><resultMap id="oneToMany" type="classes"><id column="cid" property="id"/><result column="cname" property="name"/><!--collection:配置被包含的集合對象映射關(guān)系property:被包含對象的變量名ofType:被包含對象的實際數(shù)據(jù)類型--><collection property="students" ofType="student"><id column="sid" property="id"/><result column="sname" property="name"/><result column="sage" property="age"/></collection></resultMap><select id="selectAll" resultMap="oneToMany">SELECT c.id cid,c.name cname,s.id sid,s.name sname,s.age sage FROM classes c,student s WHERE c.id=s.cid</select> </mapper>測試:
@Testpublic void selectAll() throws Exception{//1.加載核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.獲取SqlSession工廠對象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通過工廠對象獲取SqlSession對象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.獲取OneToManyMapper接口的實現(xiàn)類對象OneToManyMapper mapper = sqlSession.getMapper(OneToManyMapper.class);//5.調(diào)用實現(xiàn)類的方法,接收結(jié)果List<Classes> classes = mapper.selectAll();//6.處理結(jié)果for (Classes cls : classes) {System.out.println(cls.getId() + "," + cls.getName());List<Student> students = cls.getStudents();for (Student student : students) {System.out.println("\t" + student);}}//7.釋放資源sqlSession.close();is.close();}多對多:
sql語句:
CREATE TABLE course(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20) ); INSERT INTO course VALUES (NULL,'語文'); INSERT INTO course VALUES (NULL,'數(shù)學');CREATE TABLE stu_cr(id INT PRIMARY KEY AUTO_INCREMENT,sid INT,cid INT,CONSTRAINT sc_fk1 FOREIGN KEY (sid) REFERENCES student(id),CONSTRAINT sc_fk2 FOREIGN KEY (cid) REFERENCES course(id) ); INSERT INTO stu_cr VALUES (NULL,1,1); INSERT INTO stu_cr VALUES (NULL,1,2); INSERT INTO stu_cr VALUES (NULL,2,1); INSERT INTO stu_cr VALUES (NULL,2,2);配置文件:
<?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.tableManyToManyMapper"><resultMap id="manyToMany" type="student"><id column="sid" property="id"/><result column="sname" property="name"/><result column="sage" property="age"/><collection property="courses" ofType="course"><id column="cid" property="id"/><result column="cname" property="name"/></collection></resultMap><select id="selectAll" resultMap="manyToMany">SELECT sc.sid,s.name sname,s.age sage,sc.cid,c.name cname FROM student s,course c,stu_cr sc WHERE sc.sid=s.id AND sc.cid=c.id</select> </mapper>測試:
@Testpublic void selectAll() throws Exception{//1.加載核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.獲取SqlSession工廠對象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通過工廠對象獲取SqlSession對象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.獲取ManyToManyMapper接口的實現(xiàn)類對象ManyToManyMapper mapper = sqlSession.getMapper(ManyToManyMapper.class);//5.調(diào)用實現(xiàn)類的方法,接收結(jié)果List<Student> students = mapper.selectAll();//6.處理結(jié)果for (Student student : students) {System.out.println(student.getId() + "," + student.getName() + "," + student.getAge());List<Course> courses = student.getCourses();for (Course cours : courses) {System.out.println("\t" + cours);}}//7.釋放資源sqlSession.close();is.close();}標簽解釋:
<resultMap>:配置字段和對象屬性的映射關(guān)系標簽。id 屬性:唯一標識type 屬性:實體對象類型<id>:配置主鍵映射關(guān)系標簽。<result>:配置非主鍵映射關(guān)系標簽。column 屬性:表中字段名稱property 屬性: 實體對象變量名稱<association>:配置被包含對象的映射關(guān)系標簽。property 屬性:被包含對象的變量名javaType 屬性:被包含對象的數(shù)據(jù)類型<collection>:配置被包含集合對象的映射關(guān)系標簽。property 屬性:被包含集合對象的變量名ofType 屬性:集合中保存的對象數(shù)據(jù)類型總結(jié)
以上是生活随笔為你收集整理的Mybatis多表模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 计算机基础和操作系统基础知识测试,计算机
- 下一篇: 照片边框 app android,Scr
