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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis关系映射(1对1,1对多,多对多)

發布時間:2025/3/15 编程问答 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis关系映射(1对1,1对多,多对多) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.映射(多)對一、(一)對一的關聯關系
?? ??? ? 1).使用列的別名
?? ??? ??? ?①.若不關聯數據表,則可以得到關聯對象的id屬性
?? ??? ??? ?②.若還希望得到關聯對象的其它屬性。則必須關聯其它的數據表
?? ??? ?
?? ?1.創建表:
?? ?? 員工表:
?? ??? ?DROP TABLE IF EXISTS `tbl_employee`;

?? ??? ?CREATE TABLE `tbl_employee` (
?? ??? ?? `id` int(11) NOT NULL AUTO_INCREMENT,
?? ??? ?? `user_name` varchar(255) DEFAULT NULL,//DEFAULT默認值
?? ??? ?? `gender` char(1) DEFAULT NULL,
?? ??? ?? `email` varchar(255) DEFAULT NULL,
?? ??? ?? `d_id` int(11) DEFAULT NULL,
?? ??? ?? PRIMARY KEY (`id`),
?? ??? ?? KEY `fk_emp_dept` (`d_id`),
?? ??? ?? CONSTRAINT `fk_emp_dept` FOREIGN KEY (`d_id`) REFERENCES `tbl_dept` (`id`)
?? ??? ?) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
?? ??? ?? ?
?? ??? 部門表:
?? ??? ?CREATE TABLE tbl_dept(
?? ??? ??? id INT(11) PRIMARY KEY AUTO_INCREMENT,
?? ??? ??? dept_name VARCHAR(255)
?? ??? ?)
?? ?
?? ?
?? ?2.創建相應的實體類和Mapper接口!
?? ?
?? ?3.寫關聯的SQL語句
?? ??? ?SELECT e.`id` id , e.`user_name` user_name, e.`gender` gender,e.`d_id` d_id,d.`id` did,d.`dept_name` dept_name
?? ??? ?FROM `tbl_employee` e, tbl_dept d
?? ??? ?WHERE e.`d_id` = d.`id` AND e.id = 1
?? ?4.在sql映射文件中寫映射sql語句【聯合查詢:級聯屬性封裝結果集】????

<!-- 聯合查詢:級聯屬性封裝結果集 --><resultMap type="com.neuedu.entity.Employee" id="getEmployeeAndDeptMap"><id column="id" property="id"/><result column="user_name" property="userName"/><result column="gender" property="gender"/><result column="email" property="email"/><result column="did" property="depart.id"/><result column="dept_name" property="depart.deptName"/></resultMap><!-- public Employee getEmployeeAndDept(Integer id); --><select id="getEmployeeAndDept" resultMap="getEmployeeAndDeptMap">SELECT e.`id` id , e.`user_name` user_name, e.`gender` gender,e.`email` email,e.`d_id` d_id,d.`id` did,d.`dept_name` dept_nameFROM `tbl_employee` e, tbl_dept dWHERE e.`d_id` = d.`id` AND e.id = #{id}</select> 聯合查詢:級聯屬性封裝結果集??? ?? ????

?? ???? 注意:即使使用resultMap來映射,對于“對一”關聯關系可以不使用association
?? ??? ?
?? ?5.編寫測試用例?? ??

public class TestMyBatis {private SqlSession openSession = null;@Testpublic void testGetEmployee(){EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);Employee employee = mapper.getEmployeeAndDept(1);System.out.println(employee);}@Beforepublic void testBefore() throws IOException{String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);openSession= sqlSessionFactory.openSession();}@Afterpublic void testAfter(){openSession.commit();openSession.close();}} 聯合查詢:級聯屬性封裝結果集測試

?? ????
?? ?方法二:【使用association來定義關聯對象的規則,[比較正規的,推薦的方式]】?? ?

<!-- 聯合查詢:使用association封裝結果集 --><resultMap type="com.neuedu.entity.Employee" id="getEmployeeAndDeptMap"><id column="id" property="id"/><result column="user_name" property="userName"/><result column="gender" property="gender"/><result column="email" property="email"/><!--association可以指定聯合的javaBean對象property="depart":指定哪個屬性是聯合的對象javaType:指定這個屬性對象的類型【不能省略】--><association property="depart" javaType="com.neuedu.entity.Department"><id column="did" property="id"/><result column="dept_name" property="deptName"/></association></resultMap><!-- public Employee getEmployeeAndDept(Integer id); --><select id="getEmployeeAndDept" resultMap="getEmployeeAndDeptMap">SELECT e.`id` id , e.`user_name` user_name, e.`gender` gender,e.`email` email,e.`d_id` d_id,d.`id` did,d.`dept_name` dept_nameFROM `tbl_employee` e, tbl_dept dWHERE e.`d_id` = d.`id` AND e.id = #{id}</select> 聯合查詢:使用association封裝結果集?????

?? ????
?? ?方法三[上述結果相當于使用嵌套結果集的形式]【我們這里還可以使用Association進行分步查詢】:?? ?? ???

<!--使用association進行分步查詢1.先按照員工id查詢員工信息2.根據查詢員工信息中d_id值取部門表查出部門信息3.部門設置到員工中:--><select id="getDepartById" resultType="com.neuedu.entity.Department">SELECT id ,dept_name deptName FROM tbl_dept WHERE id = #{id}</select><resultMap type="com.neuedu.entity.Employee" id="myEmpByStep"><id column="id" property="id"/><result column="user_name" property="userName"/><result column="gender" property="gender"/><result column="email" property="email"/><!--association定義關聯對象的封裝規則select:表明當前屬性是調用指定的方法查出的結果column:指定將哪一列的值傳給這個方法流程:使用select指定的方法(傳入column指定的這列參數的值)查出對象,并封裝給property指定的屬性。--><association property="depart" select="getDepartById" column="d_id"></association></resultMap><!-- public Employee getEmpAndDept(Integer id); --><select id="getEmpAndDept" resultMap="myEmpByStep">select * from tbl_employee where id =#{id}</select> 使用association進行分步查詢??

???
?? ?補充:懶加載機制【按需加載,也叫懶加載】:
?? ?
?? ?<!--
?? ??? ?在分步查詢這里,我們還要講到延遲加載:
?? ??? ?Employee? === > Dept:
?? ??? ?我們每次查詢Employee對象的時候,都將關聯的對象查詢出來了。
?? ??? ?而我們想能不能我在需要部門信息的時候,再去查詢,不需要的時候就不用查詢了。
?? ??? ?答案:可以的
?? ??? ?我們只需要在分步查詢的基礎之上加上兩個配置:
?? ??? ? 1.在mybatis的全局配置文件中加入兩個屬性:
?? ??? ? ?? ?<settings>
?? ??? ??? ??? ?<setting name="mapUnderscoreToCamelCase" value="true"/>
?? ??? ??? ??? ?<!-- 開啟懶加載機制 ,默認值為true-->
?? ??? ??? ??? ?<setting name="lazyLoadingEnabled" value="true"/>
?? ??? ??? ??? ?<!-- 開啟的話,每個屬性都會直接全部加載出來;禁用的話,只會按需加載出來 -->
?? ??? ??? ??? ?<setting name="aggressiveLazyLoading" value="false"/>
?? ??? ??? ?</settings>
?? ??? ?
?? ?-->
?? ??? ?測試:
?? ??? ?
?? ??? ??? ?@Test
?? ??? ??? ?public void testGetEmployee(){
?? ??? ??? ??? ?EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
?? ??? ??? ??? ?Employee employee = mapper.getEmpAndDept(1);
?? ??? ??? ??? ?System.out.println(employee.getUserName());
?? ??? ??? ?}
?? ?
?? ??? 此時:可以看到這里只發送了一條SQL語句。
?? ?? ?
?? ??? 注意:上面我們上面如果關聯的是一個對象,我們還可以使用association標簽,如果我們關聯的是一個集合,
?? ??? 那么該使用誰呢?
?? ?? ?
?? ?======================================映射對多的關聯關系==================================================
?? ? 場景二:查詢部門的時候,將部門對應的所有員工信息也查詢出來,注釋在DepartmentMapper.xml中
?? ?
?? ??? ?第一種:1.修改Department實體類【添加Employee集合,并為該集合提供getter/setter方法】?? ??? ??? ?

        public class Department {private Integer id;private String deptName;private List<Employee> list;public List<Employee> getList() {return list;}public void setList(List<Employee> list) {this.list = list;}......}建立DepartmentMapper接口文件,并添加如下方法:public Department getDeptByIdPlus(Integer id);

?? ??? ??? ??? ????
?? ??? ??? ?2.sql映射文件中的內容為:【collection:嵌套結果集的方式:使用collection標簽定義關聯的集合類型元素的封裝規則】???? ??? ????

<!-- public Department getDeptByIdPlus(Integer id); --><resultMap type="com.neuedu.entity.Department" id="getDeptByIdPlusMap"><id column="did" property="id"/><result column="dept_name" property="deptName"/><!--collection:定義關聯集合類型的屬性的封裝規則ofType:指定集合里面元素的類型--><collection property="list" ofType="com.neuedu.entity.Employee"><!-- 定義這個集合中元素的封裝規則 --><id column="eid" property="id"/><result column="user_name" property="userName"/><result column="email" property="email"/><result column="gender" property="gender"/></collection></resultMap><select id="getDeptByIdPlus" resultMap="getDeptByIdPlusMap">SELECT d.`id` did, d.`dept_name` dept_name,e.`id` eid,e.`user_name` user_name,e.`email` email,e.`gender` genderFROM `tbl_dept` dLEFT JOIN tbl_employee eON e.`d_id` = d.`id`WHERE d.`id` = #{id}</select> 映射文件內容

? ??? ??? ??? ????
?? ??? ??? ?3.測試方法為:
?? ??? ??? ??? ?@Test
?? ??? ??? ??? ?public void testGetEmployee(){
?? ??? ??? ??? ??? ?DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
?? ??? ??? ??? ??? ?Department department = mapper.getDeptByIdPlus(2);
?? ??? ??? ??? ??? ?System.out.println(department);
?? ??? ??? ??? ?}

?? ?第二種:使用分步查詢結果集的方式:
?? ??? ?1.如果使用分步查詢的話,我們的sql語句就應該為:??? ??

       SELECT * FROM `tbl_dept` WHERE id = 2;SELECT * FROM `tbl_employee` WHERE d_id = 2;? ??? ????

?? ???? 2.在DepartmentMapper接口文件中添加方法,如下所示:???? ???

    public Department getDeptWithStep(Integer id);? ????

?? ???? 3.再從EmployeeMapper接口中添加一個方法,如下所示:? ??? ??

     public List<Employee> getEmployeeByDeptId(Integer deptId);

?? ??? ???? 并在響應的sql映射文件中添加相應的sql語句? ??? ??? ????

<select id="getEmployeeByDeptId" resultType="com.neuedu.entity.Employee">select * from tbl_employee where d_id = #{departId} </select>

?? ???? 4.在DepartmentMapper映射文件中:?? ??? ????

        <resultMap type="com.neuedu.entity.Department" id="getDeptWithStepMap"><id column="id" property="id"/><result column="dept_name" property="deptName"/><collection property="list" select="com.neuedu.mapper.EmployeeMapper.getEmployeeByDeptId" column="id"></collection></resultMap><select id="getDeptWithStep" resultMap="getDeptWithStepMap">SELECT id ,dept_name FROM tbl_dept WHERE id = #{id}</select>???? ??? ??

?? ???? 5.測試類:
?? ??? ??? ?@Test
?? ??? ??? ?public void testGetEmployee(){
?? ??? ??? ??? ?DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
?? ??? ??? ??? ?Department department = mapper.getDeptWithStep(2);
?? ??? ??? ??? ?System.out.println(department);
?? ??? ??? ?}
?? ??? ?
?? ??? ?
?? ?1.映射(一)對多、(多)對多的關聯關系=======》【映射"對多"的關聯關系】
?? ??? ?1.必須使用collection節點進行映射
?? ??? ?2.基本示例:
?? ??? ?? 注意:1). ofType指定集合中的元素類型
?? ??? ???????? 2).
?? ??? ??? ??? ?<!--
?? ??? ??? ??? ??? collection標簽
?? ??? ??? ??? ??? ??? ?映射多的一端的關聯關系,使用ofType指定集合中的元素類型
?? ??? ??? ??? ??? ?columnprefix:指定列的前綴
?? ??? ??? ??? ??? ?使用情境:若關聯的數據表和之前的數據表有相同的列名,此時就需要給關聯的列其"別名".
?? ??? ??? ??? ??? ?若有多個列需要起別名,可以為所有關聯的數據表的列都加上相同的前綴,然后再映射時指定前綴。
?? ??? ??? ??? ?-->

轉載于:https://www.cnblogs.com/kangxingyue-210/p/7475216.html

總結

以上是生活随笔為你收集整理的mybatis关系映射(1对1,1对多,多对多)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。