Mybatis關(guān)聯(lián)查詢之一對(duì)多和多對(duì)一XML配置詳解
平時(shí)在開發(fā)過程中dao、bean和XML文件都是自動(dòng)生成的,很少寫XML的配置關(guān)系,今天記錄一下mybatis的關(guān)聯(lián)查詢中的多對(duì)一和一對(duì)多的情況。
首先是有兩張表(學(xué)生表Student和老師Teacher表,注:這里只是為了演示一對(duì)多和多對(duì)一的情況,請(qǐng)不要杠),為了更易懂,這里只設(shè)置了最簡(jiǎn)單的幾個(gè)必要字段。表結(jié)構(gòu)如下圖
Student表:
Teacher表:
創(chuàng)建實(shí)體bean
Teacher.java:
import java.util.List;public class Teacher { private Integer id; private String name; private String className; private List<Student> students; // get、set方法省略}
Sfudent.java
public class Student { private Integer id; private String name; private Integer teacherId; private String className; private Teacher teacher; // get、set方法省略}
下面重點(diǎn)來了:配置Mapper.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.tz.mybatis.dao.studentDao'> <!-- ///一對(duì)多的第一種寫法/// --> <resultMap type='Teacher' id='teacherMap'> <id column='id' property='id'/> <result column='name' property='name'/> <collection property='students' ofType='Student' column='id'> <!-- 這里的column對(duì)應(yīng)的是下面查詢的別名,而不是表字段名 --> <id column='sid' property='id'/> <!-- property對(duì)應(yīng)JavaBean中的屬性名 --> <result column='sname' property='name'/> <result column='className' property='className'/> </collection> </resultMap> <!-- 查詢所有的老師級(jí)各自的所有學(xué)生 --> <select parameterType='Teacher' resultMap='teacherMap'> SELECT t.id, t.NAME, t.class_Name, s.id AS sid, s. NAME AS sname, s.class_name as className FROM teacher t LEFT JOIN student s ON t.id = s.teacher_id </select></mapper>
測(cè)試類:
package com.tz.test;import java.io.IOException;import java.io.InputStream;import java.util.List;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Before;import org.junit.Test;import com.tz.mybatis.bean.Student;import com.tz.mybatis.bean.Teacher;public class TeacherTest { private SqlSessionFactory sqlSessionFactory; @Before public void init() throws IOException { String resource = 'mybatis-config.xml'; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void getTeachers() { SqlSession session = sqlSessionFactory.openSession(); List<Teacher> list = session.selectList('com.tz.mybatis.dao.studentDao.getTeachers'); System.out.println(list); } }
下面給出第二種寫法:
<!-- //一對(duì)多的第二種寫法/ --> <resultMap type='Teacher' id='teacherMaps'> <id column='id' property='id'/> <result column='name' property='name'/> <result column='class_name' property='className'/> <collection property='students' ofType='Student' select='getStudents' column='id'> </collection> </resultMap> <!-- 查詢所有的老師級(jí)各自的所有學(xué)生 --> <select parameterType='Teacher' resultMap='teacherMaps'> SELECT t.id, t.NAME, t.class_name FROM teacher t </select> <select parameterType='int' resultType='Student'> select s.id, s. NAME, s.class_name as className from student s where teacher_id = #{id} </select>
測(cè)試類:
@Test public void getTeachers2() { SqlSession session = sqlSessionFactory.openSession(); List<Teacher> list = session.selectList('com.tz.mybatis.dao.studentDao.getAllTeacher'); System.out.println(list); }
查詢學(xué)生信息(多對(duì)一):
首先還是配置文件:
<resultMap type='Student' id='studentMap'> <id column='id' property='id'/> <result column='name' property='name'/> <result column='class_name' property='className'/> <result column='teacher_id' property='teacherId'/> <association property='teacher' select='getTeacher' column='teacher_id' javaType='Teacher'> <!-- 這里要注意的是column對(duì)應(yīng)的是student中的外鍵,而且需是表字段名 --> </association> </resultMap> <select resultMap='studentMap'> SELECT s.id, s.name, s.class_name, s.teacher_id FROM student s </select> <select resultType='Teacher' parameterType='int'> SELECT t.id, t.name, t.class_name as className FROM teacher t where id = #{teacher_id} </select>
測(cè)試類:
@Test public void getStudents() { SqlSession session = sqlSessionFactory.openSession(); List<Student> list = session.selectList('com.tz.mybatis.dao.studentDao.getStudent'); System.out.println(list); }
最后:當(dāng)然如果不想配置這么麻煩的信息,可以直接寫一個(gè)關(guān)聯(lián)查詢的SQL語句,返回結(jié)果直接由Map接受即可。不過這樣就不太符合面向?qū)ο蟮睦砟盍恕?
到此這篇關(guān)于Mybatis關(guān)聯(lián)查詢之一對(duì)多和多對(duì)一XML配置詳解的文章就介紹到這了,更多相關(guān)Mybatis關(guān)聯(lián)查詢內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 使用mysql記錄從url返回的http GET請(qǐng)求數(shù)據(jù)操作2. Microsoft Office Access設(shè)置字體顏色的方法3. 怎樣在Oracle中執(zhí)行一次基本的快照復(fù)制4. SQL Server 2008通過端口1433連接到主機(jī)127.0.0.1的TCP/IP連接失敗5. Oracle的PDB數(shù)據(jù)庫創(chuàng)建DIRECTORY時(shí)遇到ORA-65254問題及解決方法6. Oracle基本語法集錦7. Oracle817 版本 不同字符集之間的數(shù)據(jù)庫導(dǎo)入8. Oracle 體系結(jié)構(gòu)介紹9. 精細(xì)分析Oracle分布式系統(tǒng)數(shù)據(jù)復(fù)制技術(shù)10. MySQL數(shù)據(jù)庫對(duì)敏感數(shù)據(jù)加密及解密的實(shí)現(xiàn)方式
