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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

Mybatis中的多对一、一对一、一对多

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

首先在數(shù)據(jù)庫(kù)創(chuàng)建3個(gè)表,老師表、學(xué)生表和課程表

其中

1 某一課程和老師是一對(duì)一的關(guān)系

2 所有課程和老師是多對(duì)一的關(guān)系

3 某一課程和學(xué)生是一對(duì)多的關(guān)系

表和表中的數(shù)據(jù)sql語(yǔ)句

/* Navicat MySQL Data TransferSource Server : localhost Source Server Version : 50729 Source Host : localhost:3306 Source Database : testmybatisTarget Server Type : MYSQL Target Server Version : 50729 File Encoding : 65001Date: 2020-08-20 22:25:17 */SET FOREIGN_KEY_CHECKS=0;-- ---------------------------- -- Table structure for `course` -- ---------------------------- DROP TABLE IF EXISTS `course`; CREATE TABLE `course` (`c_id` int(5) NOT NULL AUTO_INCREMENT,`c_name` varchar(20) NOT NULL,PRIMARY KEY (`c_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;-- ---------------------------- -- Records of course -- ---------------------------- INSERT INTO `course` VALUES ('1', '語(yǔ)文'); INSERT INTO `course` VALUES ('2', '數(shù)學(xué)'); INSERT INTO `course` VALUES ('3', '外語(yǔ)'); INSERT INTO `course` VALUES ('4', '理綜');-- ---------------------------- -- Table structure for `student` -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` (`s_id` int(5) NOT NULL AUTO_INCREMENT,`s_name` varchar(20) NOT NULL,`s_age` int(5) NOT NULL,`c_id` int(5) NOT NULL,PRIMARY KEY (`s_id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;-- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', 'cxq', '18', '1'); INSERT INTO `student` VALUES ('2', 'gy', '18', '2'); INSERT INTO `student` VALUES ('3', 'wkl', '18', '4'); INSERT INTO `student` VALUES ('4', 'gdh', '18', '3'); INSERT INTO `student` VALUES ('5', 'zs', '19', '2'); INSERT INTO `student` VALUES ('6', 'ww', '20', '1'); INSERT INTO `student` VALUES ('7', 'zs111', '21', '3'); INSERT INTO `student` VALUES ('8', 'ls', '22', '4');-- ---------------------------- -- Table structure for `teacher` -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` (`t_id` int(5) NOT NULL AUTO_INCREMENT,`t_name` varchar(20) NOT NULL,`t_age` int(5) NOT NULL,`c_id` int(5) NOT NULL,PRIMARY KEY (`t_id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;-- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES ('1', '韓梅梅', '25', '1'); INSERT INTO `teacher` VALUES ('2', '秦江', '28', '4'); INSERT INTO `teacher` VALUES ('3', '陳好', '24', '3'); INSERT INTO `teacher` VALUES ('4', '陳婷', '26', '2');

構(gòu)建實(shí)體類(引入lombok)

Course.java

@Data @ToString public class Course {private Integer cId;private String cName;private Teacher teacher;private List<Student> students; }

Student.java

@Data @ToString public class Student {private Integer sId;private String sName;private Integer sAge;private Integer cId; }

Teacher.java

@Data @ToString public class Teacher {private Integer tId;private String tName;private Integer tAge;private Integer cId;}

構(gòu)建Mapper接口?CourseMapper.java

public interface CourseMapper {//查詢所有課程并對(duì)應(yīng)老師//多對(duì)一List<Course> findCourseAndTeachersByCourseId();//查詢某一個(gè)課程并對(duì)應(yīng)老師//一對(duì)一Course findCourseAndTeacherByCourseId(Integer cId);//通過(guò)課程來(lái)查詢學(xué)生//一對(duì)多Course findCourseAndStudentByCourseId(Integer cId); }

?

CourseMapper.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="mapper.CourseMapper"><select id="findCourseAndTeachersByCourseId" resultMap="CourseAndTeachers">select c.c_name,t.t_name,t.t_age from course c ,teacher t where c.c_id = t.c_id</select><resultMap id="CourseAndTeachers" type="entity.Course"><result column="c_name" property="cName"/><association property="teacher" javaType="entity.Teacher"><result column="t_name" property="tName"/><result column="t_age" property="tAge"/></association></resultMap><select id="findCourseAndTeacherByCourseId" parameterType="Integer" resultMap="CourseAndTeacher">select c.c_name,t.t_name,t.t_age from course c ,teacher t where c.c_id = t.c_id and c.c_id=#{cId}</select><resultMap id="CourseAndTeacher" type="entity.Course"><result column="c_name" property="cName"/><association property="teacher" javaType="entity.Teacher"><result column="t_name" property="tName"/><result column="t_age" property="tAge"/></association></resultMap><select id="findCourseAndStudentByCourseId" parameterType="Integer" resultMap="CourseAndStudents">select c.c_name,s.s_name,s.s_age from course c,student s where c.c_id=s.c_id and s.c_id=#{cId}</select><resultMap id="CourseAndStudents" type="entity.Course"><result column="c_name" property="cName"/><collection property="students" ofType="entity.Student"><result column="s_name" property="sName"/><result column="s_age" property="sAge"/></collection></resultMap></mapper>

配置文件中記得開啟駝峰命令

測(cè)試類

import entity.Course; import mapper.CourseMapper; 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 java.io.IOException; import java.io.Reader; import java.util.List;public class Test {public static void main(String[] args) throws IOException {String resoruce = "mybatis.xml";Reader reader = Resources.getResourceAsReader(resoruce);SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);SqlSession session = sessionFactory.openSession();CourseMapper courseMapper = session.getMapper(CourseMapper.class);List<Course> lists = courseMapper.findCourseAndTeachersByCourseId();Course course1 = courseMapper.findCourseAndTeacherByCourseId(1);Course course2 = courseMapper.findCourseAndStudentByCourseId(1);System.out.println(lists);System.out.println(course1);System.out.println(course2);session.commit();session.close();}}

運(yùn)行結(jié)果

[Course(cId=null, cName=語(yǔ)文, teacher=Teacher(tId=null, tName=韓梅梅, tAge=25, cId=null), students=[]), Course(cId=null, cName=數(shù)學(xué), teacher=Teacher(tId=null, tName=陳婷, tAge=26, cId=null), students=[]), Course(cId=null, cName=外語(yǔ), teacher=Teacher(tId=null, tName=陳好, tAge=24, cId=null), students=[]), Course(cId=null, cName=理綜, teacher=Teacher(tId=null, tName=秦江, tAge=28, cId=null), students=[])] Course(cId=null, cName=語(yǔ)文, teacher=Teacher(tId=null, tName=韓梅梅, tAge=25, cId=null), students=null) Course(cId=null, cName=語(yǔ)文, teacher=null, students=[Student(sId=null, sName=陳小強(qiáng), sAge=18, cId=null), Student(sId=null, sName=ww, sAge=20, cId=null)])

?

總結(jié)

以上是生活随笔為你收集整理的Mybatis中的多对一、一对一、一对多的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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