Mybatis中的多对一、一对一、一对多
生活随笔
收集整理的這篇文章主要介紹了
Mybatis中的多对一、一对一、一对多
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先在數據庫創建3個表,老師表、學生表和課程表
其中
1 某一課程和老師是一對一的關系
2 所有課程和老師是多對一的關系
3 某一課程和學生是一對多的關系
表和表中的數據sql語句
/* 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', '語文'); INSERT INTO `course` VALUES ('2', '數學'); INSERT INTO `course` VALUES ('3', '外語'); 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');構建實體類(引入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;}構建Mapper接口?CourseMapper.java
public interface CourseMapper {//查詢所有課程并對應老師//多對一List<Course> findCourseAndTeachersByCourseId();//查詢某一個課程并對應老師//一對一Course findCourseAndTeacherByCourseId(Integer cId);//通過課程來查詢學生//一對多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>配置文件中記得開啟駝峰命令
測試類
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();}}運行結果
[Course(cId=null, cName=語文, teacher=Teacher(tId=null, tName=韓梅梅, tAge=25, cId=null), students=[]), Course(cId=null, cName=數學, teacher=Teacher(tId=null, tName=陳婷, tAge=26, cId=null), students=[]), Course(cId=null, cName=外語, 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=語文, teacher=Teacher(tId=null, tName=韓梅梅, tAge=25, cId=null), students=null) Course(cId=null, cName=語文, teacher=null, students=[Student(sId=null, sName=陳小強, sAge=18, cId=null), Student(sId=null, sName=ww, sAge=20, cId=null)])?
總結
以上是生活随笔為你收集整理的Mybatis中的多对一、一对一、一对多的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是BACKHAUL
- 下一篇: Java中的反射和枚举