【Mybatis】一对多实例
①创建数据库和表,数据库为mytest,表为teacher和student
DROP TABLE IF EXISTS teacher;
DROP TABLE IF EXISTS student; CREATE TABLE teacher(
teacher_id INT PRIMARY KEY auto_increment NOT NULL,
teacher_name VARCHAR(20)
); CREATE TABLE student(
student_id INT PRIMARY KEY auto_increment NOT NULL,
student_name VARCHAR(20),
teacher_id INT
); ALTER TABLE student ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(teacher_id); INSERT INTO teacher(teacher_name) VALUES ("张三丰");
INSERT INTO teacher(teacher_name) VALUES ("王重阳");
INSERT INTO student(student_name, teacher_id) VALUES ("宋远桥", 1);
INSERT INTO student(student_name, teacher_id) VALUES ("俞莲舟", 1);
INSERT INTO student(student_name, teacher_id) VALUES ("俞岱岩", 1);
INSERT INTO student(student_name, teacher_id) VALUES ("张松溪", 1);
INSERT INTO student(student_name, teacher_id) VALUES ("张翠山", 1);
INSERT INTO student(student_name, teacher_id) VALUES ("殷梨亭", 1);
INSERT INTO student(student_name, teacher_id) VALUES ("莫声谷", 1); INSERT INTO student(student_name, teacher_id) VALUES ("丹阳子马钰", 2);
INSERT INTO student(student_name, teacher_id) VALUES ("长真子谭处端", 2);
INSERT INTO student(student_name, teacher_id) VALUES ("长生子刘处玄", 2);
INSERT INTO student(student_name, teacher_id) VALUES ("长春子丘处机", 2);
INSERT INTO student(student_name, teacher_id) VALUES ("玉阳子王处一", 2);
INSERT INTO student(student_name, teacher_id) VALUES ("广宁子郝大通", 2);
INSERT INTO student(student_name, teacher_id) VALUES ("清静散人孙不二", 2);
②创建Java工程,导入相应的jar包
③创建配置文件conf.xml和数据库配置文件db.properties
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <mappers>
<mapper resource="org/mybatis/mapping/teacherMapper.xml"/>
<mapper resource="org/mybatis/mapping/studentMapper.xml"/>
</mappers>
</configuration>
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mytest
name = root
password = root
④创建实体类Teacher和Student
package org.mybatis.domain; import java.util.List; public class Teacher { private int teacher_id;
private String teacher_name;
private List<Student> students; public int getTeacher_id() {
return teacher_id;
} public void setTeacher_id(int teacher_id) {
this.teacher_id = teacher_id;
} public String getTeacher_name() {
return teacher_name;
} public void setTeacher_name(String teacher_name) {
this.teacher_name = teacher_name;
} public List<Student> getStudents() {
return students;
} public void setStudents(List<Student> students) {
this.students = students;
} @Override
public String toString() {
return "[Teacher = teacher_id:" + teacher_id + ", teacher_name:" + teacher_name
+ ", the number of students:" + students.size() + "]";
} }
package org.mybatis.domain; public class Student { private int student_id;
private String student_name;
private Teacher teacher; public int getStudent_id() {
return student_id;
} public void setStudent_id(int student_id) {
this.student_id = student_id;
} public String getStudent_name() {
return student_name;
} public void setStudent_name(String student_name) {
this.student_name = student_name;
} public Teacher getTeacher() {
return teacher;
} public void setTeacher(Teacher teacher) {
this.teacher = teacher;
} @Override
public String toString() {
return "[Student = student_id:" + student_id + ", student_name:" + student_name +
", teacher:" + teacher.getTeacher_name() + "]";
} }
⑤创建sql映射文件studentMapper.xml和teacherMapper.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="org.mybatis.mapping.studentMapper">
<select id="getStudent" resultMap="getStudentMap">
select * from teacher t, student s where t.teacher_id=s.teacher_id and s.teacher_id=#{id}
</select> <resultMap type="org.mybatis.domain.Student" id="getStudentMap">
<id column="student_id" property="student_id" />
<result column="student_name" property="student_name" />
<association property="teacher" javaType="org.mybatis.domain.Teacher">
<id column="teacher_id" property="teacher_id"/>
<result column="teacher_name" property="teacher_name"/>
</association>
</resultMap>
</mapper>
studentMapper.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="org.mybatis.mapping.teacherMapper">
<select id="getTeacher" resultMap="getTeacherMap">
select * from teacher t, student s where t.teacher_id=s.teacher_id and t.teacher_id=#{id}
</select> <resultMap type="org.mybatis.domain.Teacher" id="getTeacherMap">
<id column="teacher_id" property="teacher_id" />
<result column="teacher_name" property="teacher_name" />
<collection property="students" ofType="org.mybatis.domain.Student">
<id column="student_id" property="student_id"/>
<result column="student_name" property="student_name"/>
</collection>
</resultMap>
</mapper>
teacherMapper.xml
⑥向配置文件中注册 studentMapper.xml和teacherMapper.xml 文件【已添加,查看第③步】
⑦测试类
package org.mybatis.app; import java.io.InputStream;
import java.util.List; 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 org.mybatis.domain.Student;
import org.mybatis.domain.Teacher; public class TestOneToMany { SqlSession session; @Before
public void beforeLoad() {
InputStream inputStream =
TestOneToMany.class.getClassLoader().getResourceAsStream("conf.xml");
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
} @Test
public void testGetStudents() {
String statement = "org.mybatis.mapping.studentMapper.getStudent";
List<Student> studentList = session.selectList(statement, 1);
for(Student s : studentList) {
System.out.println(s.getStudent_name() + "的师傅是:" + s.getTeacher().getTeacher_name());
} session.close();
} @Test
public void testGetTeacher() {
String statement = "org.mybatis.mapping.teacherMapper.getTeacher";
Teacher t = session.selectOne(statement, 1);
System.out.println(t.getTeacher_name() + "有" + t.getStudents().size() + "个徒弟");
session.close();
} }
⑧结构图
【Mybatis】一对多实例的更多相关文章
- mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条
mybatis 一对多的注入 指的是连表查询时候 将不同的查询结果以列表存储对象形式 注入进去 多对一指的是 查询多条结果但都是一样的 只需注入一条
- Java基础-SSM之mybatis一对多和多对一关系映射
Java基础-SSM之mybatis一对多和多对一关系映射 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表) 1>.创建customers表: ...
- mybatis一对多关联查询+pagehelper->分页错误
mybatis一对多关联查询+pagehelper->分页错误. 现象: 网上其他人遇到的类似问题:https://segmentfault.com/q/1010000009692585 解决: ...
- 1. mybatis 的入门实例
mybatis 的入门实例 1.创建一个普通的Java项目 1.加入jar包(所有mybatis 和mysql) 2.加入配置文件 src 目录下 (1) db.properties mysql.us ...
- MyBatis一对多和多对多xml配置
MyBatis一对多和多对多xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ma ...
- mybatis一对多关联关系映射
mybatis一对多关联关系映射 一对多关联关系只需要在多的一方引入少的一方的主键作为外键即可.在实体类中就是反过来,在少的一方添加多的一方,声明一个List 属性名 作为少的一方的属性. 用户和订单 ...
- 3.SpringBoot整合Mybatis(一对多)
前言: Mybatis一对多的处理关系: 一个人有好多本书,每本书的主人只有一个人.当我们查询某个人拥有的所有书籍时,就涉及到了一对多的映射关系. 一.添加数据表: CREATE TABLE `boo ...
- mybatis 一对多查询 集合创建空对象的问题
在做 mybatis 一对多查询的时候, resultMap 里面用到了集合标签 collection ,后来发现 当该条数据没有子集的时候, collection 会自动创建一个属性都是null的对 ...
- mybatis 一对多和多对一 简单案例笔记
以案例说明(以下案例代码都敲过验证过) 多对一(多个学生对一个老师 即学生集合中都存一个老师对象) Mybatis多对一实现方式1: //定义Student 和 Teacher 实体 @Data p ...
- Mybatis 一对多延迟加载,并且子查询中与主表字段不对应 (19)
Mybatis 一对多延迟加载,并且子查询中与主表字段不对应应用说明. 实现一对多关联(懒加载),一个教研组对应多个教师,既:教师的教研编号与教研组的教研编号关联,并且教师关联教研组外键与教研组编号 ...
随机推荐
- RobotFramework+Selenium2软件安装教程
1.安装python 必须是 2.7 版本 设置环境变量:F:\software\Python27;F:\software\Python27\Scripts; 重启电脑: 2.安装wxPy ...
- 展示博客---Alpha版本展示
Alpha版本展示 1. 团队成员的简介和个人博客地址,团队的源码仓库地址. 成员 简介 个人博客地址 祁泽文 被动态统计图搞扒下的我 http://www.cnblogs.com/jiaowoxia ...
- 背水一战 Windows 10 (63) - 控件(WebView): 基础知识, 加载 html, http, https, ms-appx-web:///, embedded resource, ms-appdata:///, ms-local-stream://
[源码下载] 背水一战 Windows 10 (63) - 控件(WebView): 基础知识, 加载 html, http, https, ms-appx-web:///, embedded res ...
- Python selenium + Firefox启动浏览器
Python selenium 的运用 from selenium import webdriver # from selenium.webdriver.firefox.firefox_profile ...
- Spring AOP术语:连接点和切点的区别。
定义: 1.连接点(Join point):连接点是在应用执行过程中能够插入切面(Aspect)的一个点.这些点可以是调用方法时.甚至修改一个字段时. 2.切点(Pointcut):切点是指通知(Ad ...
- [SRC初探]手持新手卡挖SRC逻辑漏洞心得分享
文章来源i春秋 本文适合新手参阅,大牛笑笑就好了,嘿嘿末尾有彩蛋!!!!!!!!!!!!!!!!!本人参加了本次"i春秋部落守卫者联盟"活动,由于经验不足,首次挖SRC,排名不是那 ...
- Android 追加写入文件的三种方法
一.使用FileOutputStream 使用FileOutputStream,在构造FileOutputStream时,把第二个参数设为true public static void method1 ...
- Android NDK学习(四):C/C++层调用JAVA
一.从C/C++层调用JAVA层代码(无参数调用) //在c代码里面调用java代码里面的方法 // java 反射 // 1 . 找到java代码的 class文件 // jclass (*Find ...
- webrtc vad小bug
当channel为5的时候offset为80,再进行下面的操作smallest_values[j + 1]将会越出数组的限界到”第97个“:应该将下图的16改为15 low_value_vector数 ...
- linux中sed命令的使用
sed命令是linux或者shell编程中常用的筛选.替换命令,如果能熟练使用sed则对经常使用的人来说在工作上是非常有帮助的 下面把sed主要的用法列出来(有错误的地方大家可以指正): p命令只打印 ...