国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

MyBatis實(shí)體關(guān)系映射

2020-03-31 15:41 作者:匯智知了堂  | 我要投稿

MyBatis既然是一個(gè)ORM框架,則它也有像Hibernate那樣的一對(duì)多,多對(duì)多,多對(duì)一的實(shí)體關(guān)系映射功能。下面我們就來介紹一下如何使用MyBatis的實(shí)體關(guān)系映射

1.MyBatis實(shí)體關(guān)系映射,對(duì)于我個(gè)人來講常用的有下面兩種


多對(duì)一:在子表的映射文件中添加association

一對(duì)多:在父表的映射文件中添加collection


2.MyBatis中多對(duì)一的案例


先創(chuàng)建兩張表


CREATE TABLE `student` (

? `sid` int(11) default NULL,

? `sname` varchar(10) default NULL,

? `t_id` int(11) default NULL

) ;

CREATE TABLE `teacher` (

? `t_id` int(11) NOT NULL,

? `t_name` varchar(20) default NULL,

? PRIMARY KEY? (`t_id`)

) ;


創(chuàng)建實(shí)體類


package com.zlt.pojo;

public class Teacher {

? ? private intt_id;

? ? private String t_name;

? ? public intgetT_id() {

? ? ? ? return t_id;

? ? }

? ? public void setT_id(int t_id) {

? ? ? ? this.t_id = t_id;

? ? }

? ? public String getT_name() {

? ? ? ? return t_name;

? ? }

? ? public void setT_name(String t_name) {

? ? ? ? this.t_name = t_name;

? ? }

}

package com.zlt.pojo;

public class Student {

? ? private int sid;

? ? private String sname;

? ? private Teacher teacher;

? ? public int getSid() {

? ? ? ? return sid;

? ? }

? ? public void setSid(intsid) {

? ? ? ? this.sid = sid;

? ? }

? ? public String getSname() {

? ? ? ? return sname;

? ? }

? ? public void setSname(String sname) {

? ? ? ? this.sname = sname;

? ? }

? ? public Teacher getTeacher() {

? ? ? ? return teacher;

? ? }

? ? public void setTeacher(Teacher teacher) {

? ? ? ? this.teacher = teacher;

? ? }

}

創(chuàng)建StudentMapper.xml文件,在此XML文件中配置多對(duì)一的關(guān)系


<?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.zlt.mapper.StudentMapper">

? ? <resultMap type="com.zlt.pojo.Student" id="Student">

? ? ? ? <id property="sid" column="sid"/>

? ? ? ? <result property="sname" column="sname"/>

? ? ? ? <association property="teacher" javaType="com.zlt.pojo.Teacher">

? ? ? ? ? ? <id property="t_id" column="t_id"/>

? ? ? ? ? ? <result property="t_name" column="t_name"/>

? ? ? ? </association>

? ? </resultMap>

? ? <select id="getStudent" resultMap="Student">

? ? ? ? select * from student a, teacher b where a.t_id = b.t_id and sid = 123

? ? </select>

</mapper>


完成多對(duì)一關(guān)系的代碼測(cè)試


package com.zlt.test;

import java.io.IOException;

import java.io.Reader;

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.Test;

import com.zlt.pojo.Student;

public class Test03 {

? ? private static SqlSessionFactorysqlSessionFactory;

? ? private static Reader reader;

? ? static {

? ? ? ? try {

? ? ? ? ? ? reader = Resources.getResourceAsReader("config.xml");

? ? ? ? ? ? sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

? ? ? ? } catch (IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? @Test

? ? public void m01() {

? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();

? ? ? ? String sql = "com.zlt.mapper.StudentMapper.getStudent";

? ? ? ? Student student = sqlSession.selectOne(sql);

? ? ? ? System.out.println(student.getSname() + " ===? " + student.getTeacher().getT_name());

? ? ? ? sqlSession.close();

? ? }

}


3.MyBatis一對(duì)多的案例


修改Student和Teacher這兩個(gè)實(shí)體類


package com.zlt.pojo;

public class Student {

? ? private intsid;

? ? private String sname;

? ? private Teacher teacher;

? ? public intgetSid() {

? ? ? ? return sid;

? ? }

? ? public void setSid(intsid) {

? ? ? ? this.sid = sid;

? ? }

? ? public String getSname() {

? ? ? ? return sname;

? ? }

? ? public void setSname(String sname) {

? ? ? ? this.sname = sname;

? ? }

? ? public Teacher getTeacher() {

? ? ? ? return teacher;

? ? }

? ? public void setTeacher(Teacher teacher) {

? ? ? ? this.teacher = teacher;

? ? }

}

package com.zlt.pojo;

import java.util.List;

public class Teacher {

? ? private intt_id;

? ? private String t_name;

? ? private List<Student> student;

? ? public List<Student>getStudent() {

? ? ? ? return student;

? ? }

? ? public void setStudent(List<Student> student) {

? ? ? ? this.student = student;

? ? }

? ? public intgetT_id() {

? ? ? ? return t_id;

? ? }

? ? public void setT_id(intt_id) {

? ? ? ? this.t_id = t_id;

? ? }

? ? public String getT_name() {

? ? ? ? return t_name;

? ? }

? ? public void setT_name(String t_name) {

? ? ? ? this.t_name = t_name;

? ? }

}


創(chuàng)建TeacherMapper的映射文件,在此文件的<resultMap>標(biāo)簽中加入<collection>


<?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.zlt.mapper.TeacherMapper">

? ? <resultMap type="com.zlt.pojo.Teacher" id="Teacher">

? ? ? ? <id property="t_id" column="t_id"/>

? ? ? ? <result property="t_name" column="t_name"/>

? ? ? ? <collection property="student" ofType="com.zlt.pojo.Student">

? ? ? ? ? ? <id property="sid" column="sid"/>

? ? ? ? ? ? <result property="sname" column="sname"/>

? ? ? ? </collection>

? ? </resultMap>

? ? <select id="getTeacher" resultMap="Teacher">

? ? ? ? select * from teacher a,student b where a.t_id = b.t_id and a.t_id = 1

? ? </select>

</mapper>


測(cè)試


package com.zlt.test;

import java.io.IOException;

import java.io.Reader;

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.Test;

import com.zlt.pojo.Student;

import com.zlt.pojo.Teacher;

public class Test03 {

? ? private static SqlSessionFactorysqlSessionFactory;

? ? private static Reader reader;

? ? static {

? ? ? ? try {

? ? ? ? ? ? reader = Resources.getResourceAsReader("config.xml");

? ? ? ? ? ? sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

? ? ? ? } catch (IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? @Test

? ? public void m01() {

? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();

? ? ? ? String sql = "com.zlt.mapper.StudentMapper.getStudent";

? ? ? ? Student student = sqlSession.selectOne(sql);

? ? ? ? System.out.println(student.getSname() + " ===? " + student.getTeacher().getT_name());

? ? ? ? sqlSession.close();

? ? }

? ? @Test

? ? public void m02() {

? ? ? ? SqlSessionsqlSession = sqlSessionFactory.openSession();

? ? ? ? String sql = "com.zlt.mapper.TeacherMapper.getTeacher";

? ? ? ? Teacher teacher = sqlSession.selectOne(sql);

? ? ? ? List<Student> student = teacher.getStudent();

? ? ? ? for (Student s : student) {

? ? ? ? ? ? System.out.println(teacher.getT_name() + "====" + s.getSname());

? ? ? ? }

? ? ? ? sqlSession.close();

? ? }

}


原創(chuàng):知了堂Java培訓(xùn)講師——子墨


MyBatis實(shí)體關(guān)系映射的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
古田县| 达州市| 南雄市| 海阳市| 墨脱县| 科技| 黔南| 察隅县| 石棉县| 苗栗县| 沙河市| 宁城县| 准格尔旗| 泰来县| 宿州市| 新化县| 乃东县| 洛浦县| 天水市| 大姚县| 佛冈县| 江北区| 武汉市| 嘉善县| 隆尧县| 博客| 临夏县| 柯坪县| 疏附县| 乐昌市| 阜新| 东明县| 定安县| 手机| 乌苏市| 昭觉县| 固始县| 灌云县| 贵港市| 汕尾市| 中方县|