首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,082 阅读
2
类的加载
741 阅读
3
Spring Cloud OAuth2.0
726 阅读
4
SpringBoot自动装配原理
691 阅读
5
集合不安全问题
584 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
388
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
页面
统计
关于
搜索到
1
篇与
的结果
2020-12-13
一对多
一对多处理一个老师拥有多个学生(对老师而言,就是一对多的关系)1、新建实体类Teacherpackage com.sw.pojo; import lombok.Data; import java.io.Serializable; import java.util.List; /** * @Author suaxi * @Date 2020/12/13 10:55 */ @Data public class Teacher implements Serializable { private int id; private String name; //一个老师对应多个学生 private List<Student> students; } Studentpackage com.sw.pojo; import lombok.Data; import java.io.Serializable; /** * @Author suaxi * @Date 2020/12/13 10:55 */ @Data public class Student implements Serializable { private int id; private String name; private int tid; } 2、TeacherMapper.xml配置(两种方式)<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sw.dao.TeacherMapper"> <!--按结果集嵌套处理--> <select id="findTeacherById" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.id tid ,t.name tname from student s,teacher t where s.tid = t.id and t.id = #{tid}; </select> <resultMap id="TeacherStudent" type="teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!-- 集合使用 collection javaType="" 指定属性的类型 集合中的泛型信息,使用 ofType 获取 --> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <!--按照查询嵌套处理--> <select id="findTeacherById2" resultMap="TeacherStudent2"> select * from teacher where id = #{tid}; </select> <resultMap id="TeacherStudent2" type="Teacher"> <collection property="students" javaType="ArrayList" ofType="Student" select="findStudentByTeacherId" column="id"/> </resultMap> <select id="findStudentByTeacherId" resultType="Student"> select * from student where tid = #{tid}; </select> </mapper>注:1、关联 - association 【多对一】2、集合 - collection 【一对多】3、javaType & ofTypejavaType用来指定实体类中属性的类型ofType用来指定映射到List或者集合中的pojo类型,泛型中的约束类型
2020年12月13日
136 阅读
0 评论
0 点赞