日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis多表模型

發(fā)布時(shí)間:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis多表模型 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

多表模型:

  • 多表模型分類 一對(duì)一:在任意一方建立外鍵,關(guān)聯(lián)對(duì)方的主鍵。
  • 一對(duì)多:在多的一方建立外鍵,關(guān)聯(lián)一的一方的主鍵。
  • 多對(duì)多:借助中間表,中間表至少兩個(gè)字段,分別關(guān)聯(lián)兩張表的主鍵。
多表模型一對(duì)一操作:
  • sql語句準(zhǔn)備
  • CREATE TABLE person(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),age INT ); INSERT INTO person VALUES (NULL,'張三',23); INSERT INTO person VALUES (NULL,'李四',24); INSERT INTO person VALUES (NULL,'王五',25);CREATE TABLE card(id INT PRIMARY KEY AUTO_INCREMENT,number VARCHAR(30),pid INT,CONSTRAINT cp_fk FOREIGN KEY (pid) REFERENCES person(id) ); INSERT INTO card VALUES (NULL,'12345',1); INSERT INTO card VALUES (NULL,'23456',2); INSERT INTO card VALUES (NULL,'34567',3);
  • 配置文件
  • <?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.duobiao.table.One"> <!-- <resultMap>:配置字段和實(shí)體對(duì)象屬性的映射關(guān)系標(biāo)簽。 id 屬性:唯一標(biāo)識(shí) type 屬性:實(shí)體對(duì)象類型<id>:配置主鍵映射關(guān)系標(biāo)簽。 <result>:配置非主鍵映射關(guān)系標(biāo)簽。column 屬性:表中字段名稱property 屬性: 實(shí)體對(duì)象變量名稱 <association>:配置被包含對(duì)象的映射關(guān)系標(biāo)簽。property 屬性:被包含對(duì)象的變量名javaType 屬性:被包含對(duì)象的數(shù)據(jù)類型 --><!--配置字段和實(shí)體對(duì)象屬性的映射關(guān)系--><resultMap id="oneToOne" type="card"><id column="cid" property="id" /><result column="number" property="number" /><association property="p" javaType="person"><id column="pid" property="id" /><result column="name" property="name" /><result column="age" property="age" /></association></resultMap><select id="selectAll" resultMap="oneToOne">SELECT c.id cid,number,pid,NAME,age FROM card c,person p WHERE c.pid=p.id</select> </mapper>

    核心配置文件

    <!--起別名--><typeAliases><package name="com.duobiao.bean"/></typeAliases> <!-- mappers引入映射配置文件 --><mappers><!-- mapper 引入指定的映射配置文件 resource屬性指定映射配置文件的名稱 --><mapper resource="OneToOneMapper.xml"/></mappers>
  • bean包
  • public class Person {private Integer id;private String name;private Integer age; }public class Card {// 主鍵idprivate Integer id;// 身份證號(hào)private String number;// 所屬人對(duì)象private Person p; }
  • 接口:
  • public interface OneToOneMapper {// 查詢?nèi)?/span>List<Card> selectAll(); }
  • 測試類
  • @Testpublic void selectAll() throws Exception{//1.加載核心配置文件InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");//2.獲取SqlSession工廠對(duì)象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通過工廠對(duì)象獲取SqlSession對(duì)象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.獲取OneToOneMapper接口的實(shí)現(xiàn)類對(duì)象OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);//5.調(diào)用實(shí)現(xiàn)類的方法,接收結(jié)果List<Card> list = mapper.selectAll();//6.處理結(jié)果for (Card c : list) {System.out.println(c);}//7.釋放資源sqlSession.close();is.close();}
    一對(duì)多:

    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;// 班級(jí)名private String name;// 班級(jí)中所有的學(xué)生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:配置被包含的集合對(duì)象映射關(guān)系property:被包含對(duì)象的變量名ofType:被包含對(duì)象的實(shí)際數(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工廠對(duì)象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通過工廠對(duì)象獲取SqlSession對(duì)象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.獲取OneToManyMapper接口的實(shí)現(xiàn)類對(duì)象OneToManyMapper mapper = sqlSession.getMapper(OneToManyMapper.class);//5.調(diào)用實(shí)現(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();}
    多對(duì)多:

    sql語句:

    CREATE TABLE course(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20) ); INSERT INTO course VALUES (NULL,'語文'); INSERT INTO course VALUES (NULL,'數(shù)學(xué)');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工廠對(duì)象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//3.通過工廠對(duì)象獲取SqlSession對(duì)象SqlSession sqlSession = sqlSessionFactory.openSession(true);//4.獲取ManyToManyMapper接口的實(shí)現(xiàn)類對(duì)象ManyToManyMapper mapper = sqlSession.getMapper(ManyToManyMapper.class);//5.調(diào)用實(shí)現(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();}
    標(biāo)簽解釋:
    <resultMap>:配置字段和對(duì)象屬性的映射關(guān)系標(biāo)簽。id 屬性:唯一標(biāo)識(shí)type 屬性:實(shí)體對(duì)象類型<id>:配置主鍵映射關(guān)系標(biāo)簽。<result>:配置非主鍵映射關(guān)系標(biāo)簽。column 屬性:表中字段名稱property 屬性: 實(shí)體對(duì)象變量名稱<association>:配置被包含對(duì)象的映射關(guān)系標(biāo)簽。property 屬性:被包含對(duì)象的變量名javaType 屬性:被包含對(duì)象的數(shù)據(jù)類型<collection>:配置被包含集合對(duì)象的映射關(guān)系標(biāo)簽。property 屬性:被包含集合對(duì)象的變量名ofType 屬性:集合中保存的對(duì)象數(shù)據(jù)類型

    總結(jié)

    以上是生活随笔為你收集整理的Mybatis多表模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。