赞
踩
目录
并把mybatisDemo1中的pom.xml的依赖配置代码复制到 mybatisDemo2中
将mybatisDemo1中的resources的如下三个文件复制到mybatisDemo2中的resources下
并且将核心配置文件SqlMapConfig.xml中不需要的东西去掉,去完之后如下:
- <?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="mysql">
- <environment id="mysql">
- <transactionManager type="JDBC"></transactionManager>
- <dataSource type="POOLED">
- <property name="driver" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- </dataSource>
- </environment>
- </environments>
-
- <!-- 注册映射文件-->
- <mappers>
- <mapper resource="com/first/mapper/StudentMapper.xml"></mapper>
- </mappers>
- </configuration>
在java包下创建com/first/pojo
在pojo下创建Classes类和Student类
Student.java:
- package com.first.pojo;
-
- public class Student {
- private int sid;
- private String name;
- private int age;
- private String sex;
- private Classes classes;
-
- public int getSid() {
- return sid;
- }
-
- public void setSid(int sid) {
- this.sid = sid;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getSex() {
- return sex;
- }
-
- public void setSex(String sex) {
- this.sex = sex;
- }
-
- public Classes getClasses() {
- return classes;
- }
-
- public void setClasses(Classes classes) {
- this.classes = classes;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "sid=" + sid +
- ", name='" + name + '\'' +
- ", age=" + age +
- ", sex='" + sex + '\'' +
- ", classes=" + classes +
- '}';
- }
- }
Classes.java:
- package com.first.pojo;
-
- import java.util.List;
-
- public class Classes {
- private int cid;
- private String className;
- private List<Student> studentList;
-
- public int getCid() {
- return cid;
- }
-
- public void setCid(int cid) {
- this.cid = cid;
- }
-
- public String getClassName() {
- return className;
- }
-
- public void setClassName(String className) {
- this.className = className;
- }
-
- public List<Student> getStudentList() {
- return studentList;
- }
-
- public void setStudentList(List<Student> studentList) {
- this.studentList = studentList;
- }
-
- @Override
- public String toString() {
- return "Classes{" +
- "cid=" + cid +
- ", className='" + className + '\'' +
- ", studentList=" + studentList +
- '}';
- }
- }
数据库设计如下:
在com/first/mapper下创建接口文件StudentMapper.java
- package com.first.mapper;
-
- import com.first.pojo.Student;
-
- import java.util.List;
-
- public interface StudentMapper {
- List<Student> findAll();
- }
那么现在我们再来看如下利用resultMap自定义的映射关系:
- <?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">
-
- <!--映射文件-->
-
- <!--namespace设置映射的文件-->
- <mapper namespace="com.first.mapper.StudentMapper">
- <!-- id:自定义映射名 type:自定义映射的对象类型-->
- <resultMap id="studentMapper" type="com.first.pojo.Student">
- <!-- id定义主键列 property:POJO属性名 column:数据库列名 -->
- <id property="sid" column="sid"></id>
- <!-- result定义普通列 property:POJO属性名 column:数据库列名 -->
- <result property="name" column="name"></result>
- <result property="age" column="age"></result>
- <result property="sex" column="sex"></result>
- <!--对象的话就用association,列表就用collection -->
- <!--一对一对象列,property:属性名,column:关联类名(这里classId或cid都行),javaType:对象类型-->
- <association property="classes" column="classId" javaType="com.first.pojo.Classes">
- <!--关联对象主键列-->
- <id property="cid" column="cid"></id>
- <!--关联对象普通列-->
- <result property="className" column="className"></result>
- </association>
- </resultMap>
- <select id="findAll" resultMap="studentMapper">
- select * from student left join classes on student.classId = classes.cid;
- </select>
- </mapper>
TestManyTablesQuery.java:
- import com.first.mapper.StudentMapper;
- import com.first.pojo.Student;
- 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.After;
- import org.junit.Before;
- import org.junit.Test;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
-
-
- public class TestManyTablesQuery {
- //全局变量
- InputStream is=null;
- SqlSession session =null;
-
- //前置方法:测试类里的测试方法执行之前都会先执行前置方法
- @Before
- public void before() throws IOException {
- //(1)读取核心配置文件
- is= Resources.getResourceAsStream("SqlMapConfig.xml");
- //(2)创建SqlSessionFactoryBuilder对象
- SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
- //(3)SqlSessionFactoryBuilder对象获取SqlSessionFactory对象
- SqlSessionFactory factory = builder.build(is);
- //(4)SqlSessionFactory对象获取SqlSession对象
- session = factory.openSession();
- }
-
- //后置方法:测试类里的测试方法执行之后都会执行后置方法
- @After
- public void after() throws IOException {
- //(7)释放资源
- session.close();
- is.close();
- }
-
- @Test
- public void testFindAll(){
- StudentMapper studentMapper = session.getMapper(StudentMapper.class);
- List<Student> all = studentMapper.findAll();
- all.forEach(System.out::println);
- }
-
- }
输出:
- Student{sid=1, name='张三', age=10, sex='男', classes=Classes{cid=1, className='三年一班', studentList=null}}
- Student{sid=2, name='李四', age=10, sex='女', classes=Classes{cid=1, className='三年一班', studentList=null}}
- Student{sid=3, name='尚尚', age=10, sex='男', classes=Classes{cid=2, className='三年二班', studentList=null}}
- Student{sid=4, name='百战', age=11, sex='男', classes=Classes{cid=2, className='三年二班', studentList=null}}
- Student{sid=5, name='王五', age=10, sex='男', classes=Classes{cid=2, className='三年二班', studentList=null}}
查询班级时,将关联的学生集合查询出来,就是一对多关联查询。
创建持久层接口
- package com.first.mapper;
-
- import com.first.pojo.Classes;
-
- import java.util.List;
-
- public interface ClassesMapper {
- List<Classes> findAll();
- }
创建映射文件ClassesMapper.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">
-
- <!--映射文件-->
-
- <!--namespace设置映射的文件-->
- <mapper namespace="com.first.mapper.ClassesMapper">
- <!-- id:自定义映射名 type:自定义映射的对象类型-->
- <resultMap id="classesMapper" type="com.first.pojo.Classes">
- <!-- id定义主键列 property:POJO属性名 column:数据库列名 -->
- <id property="cid" column="cid"></id>
- <!-- result定义普通列 property:POJO属性名 column:数据库列名 -->
- <result property="className" column="className"></result>
- <!--一对多集合列,property:属性名,column:关联类名(这里classId和cid),ofType:集合的泛型类型-->
- <!--List也属于集合的一种-->
- <collection property="studentList" column="classId" ofType="com.first.pojo.Student">
- <id property="sid" column="sid"></id>
- <result property="name" column="name"></result>
- <result property="age" column="age"></result>
- <result property="sex" column="sex"></result>
- </collection>
- </resultMap>
- <select id="findAll" resultMap="classesMapper">
- select * from classes left join student on student.classId = classes.cid;
- </select>
- </mapper>
注册映射文件:
- @Test
- public void testFindAllClasses(){
- ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
- List<Classes> all = classesMapper.findAll();
- all.forEach(System.out::println);
- }
输出:
- Classes{cid=1, className='三年一班', studentList=[Student{sid=1, name='张三', age=10, sex='男', classes=null}, Student{sid=2, name='李四', age=10, sex='女', classes=null}]}
- Classes{cid=2, className='三年二班', studentList=[Student{sid=3, name='尚尚', age=10, sex='男', classes=null}, Student{sid=4, name='百战', age=11, sex='男', classes=null}, Student{sid=5, name='王五', age=10, sex='男', classes=null}]}
- package com.first.pojo;
-
- import java.util.List;
-
- public class Teacher {
- private int tid;
- private String tname;
- private List<Classes> classes;
-
- public int getTid() {
- return tid;
- }
-
- public void setTid(int tid) {
- this.tid = tid;
- }
-
- public String getTname() {
- return tname;
- }
-
- public void setTname(String tname) {
- this.tname = tname;
- }
-
- public List<Classes> getClasses() {
- return classes;
- }
-
- public void setClasses(List<Classes> classes) {
- this.classes = classes;
- }
-
- @Override
- public String toString() {
- return "Teacher{" +
- "tid=" + tid +
- ", tname='" + tname + '\'' +
- ", classes=" + classes +
- '}';
- }
- }
Classes类:
- package com.first.pojo;
-
- import java.util.List;
-
- public class Classes {
- private int cid;
- private String className;
- private List<Student> studentList;
- private List<Teacher> teacherList;
-
- public int getCid() {
- return cid;
- }
-
- public void setCid(int cid) {
- this.cid = cid;
- }
-
- public String getClassName() {
- return className;
- }
-
- public void setClassName(String className) {
- this.className = className;
- }
-
- public List<Student> getStudentList() {
- return studentList;
- }
-
- public void setStudentList(List<Student> studentList) {
- this.studentList = studentList;
- }
-
- public List<Teacher> getTeacherList() {
- return teacherList;
- }
-
- public void setTeacherList(List<Teacher> teacherList) {
- this.teacherList = teacherList;
- }
-
- @Override
- public String toString() {
- return "Classes{" +
- "cid=" + cid +
- ", className='" + className + '\'' +
- ", studentList=" + studentList +
- ", teacherList=" + teacherList +
- '}';
- }
- }
因为老师类和班级类没有公共字段,所以需要借助于一个中间表来将两个表连起来。
- package com.first.mapper;
-
- import com.first.pojo.Teacher;
-
- import java.util.List;
-
- public interface TeacherMapper {
- List<Teacher> findAll();
- }
- <?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">
-
- <!--映射文件-->
-
- <!--namespace设置映射的文件-->
- <mapper namespace="com.first.mapper.TeacherMapper">
- <!-- id:自定义映射名 type:自定义映射的对象类型-->
- <resultMap id="teacherMapper" type="com.first.pojo.Teacher">
- <!-- id定义主键列 property:POJO属性名 column:数据库列名 -->
- <id property="tid" column="tid"></id>
- <!-- result定义普通列 property:POJO属性名 column:数据库列名 -->
- <result property="tname" column="tname"></result>
- <!--对象的话就用association,列表就用collection -->
- <!--集合列,property:属性名,column:关联类名(这里classId或cid都行),javaType:对象类型-->
- <collection property="classes" column="tid" ofType="com.first.pojo.Classes">
- <!--关联对象主键列-->
- <id property="cid" column="cid"></id>
- <!--关联对象普通列-->
- <result property="className" column="className"></result>
- </collection>
- </resultMap>
- <select id="findAll" resultMap="teacherMapper">
- select * from teacher
- left join classes_teacher
- on teacher.tid = classes_teacher.tid
- left join classes
- on classes_teacher.cid = classes.cid
- </select>
- </mapper>
在核心配置文件中注册映射文件:
测试多对多关联查询
- @Test
- public void testFindAllTeacher(){
- TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
- List<Teacher> all = teacherMapper.findAll();
- all.forEach(System.out::println);
- }
输出:
- Teacher{tid=1, tname='王老师', classes=[Classes{cid=1, className='三年一班', studentList=null, teacherList=null}]}
- Teacher{tid=2, tname='李老师', classes=[Classes{cid=1, className='三年一班', studentList=null, teacherList=null}, Classes{cid=2, className='三年二班', studentList=null, teacherList=null}]}
- Teacher{tid=3, tname='张老师', classes=[Classes{cid=2, className='三年二班', studentList=null, teacherList=null}]}
接下来测试查询班级时,将关联的老师集合查询出来。
对ClassesMapper.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">
-
- <!--映射文件-->
-
- <!--namespace设置映射的文件-->
- <mapper namespace="com.first.mapper.ClassesMapper">
- <!-- id:自定义映射名 type:自定义映射的对象类型-->
- <resultMap id="classesMapper" type="com.first.pojo.Classes">
- <!-- id定义主键列 property:POJO属性名 column:数据库列名 -->
- <id property="cid" column="cid"></id>
- <!-- result定义普通列 property:POJO属性名 column:数据库列名 -->
- <result property="className" column="className"></result>
- <!--一对多集合列,property:属性名,column:关联类名(这里classId和cid),ofType:集合的泛型类型-->
- <!--List也属于集合的一种-->
- <!--学生集合-->
- <collection property="studentList" column="classId" ofType="com.first.pojo.Student">
- <id property="sid" column="sid"></id>
- <result property="name" column="name"></result>
- <result property="age" column="age"></result>
- <result property="sex" column="sex"></result>
- </collection>
- <!--老师集合-->
- <collection property="teacherList" column="cid" ofType="com.first.pojo.Teacher">
- <!--关联对象主键列-->
- <id property="tid" column="tid"></id>
- <!--关联对象普通列-->
- <result property="tname" column="tname"></result>
- </collection>
- </resultMap>
- <select id="findAll" resultMap="classesMapper">
- select * from classes
- left join student
- on student.classId = classes.cid
- left join classes_teacher
- on classes.cid=classes_teacher.cid
- left join teacher
- on classes_teacher.tid=teacher.tid;
- </select>
- </mapper>
测试函数就用之前写的testFindAllClasses():
- @Test
- public void testFindAllClasses(){
- ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
- List<Classes> all = classesMapper.findAll();
- all.forEach(System.out::println);
- }
输出:
- Classes{cid=1, className='三年一班', studentList=[Student{sid=1, name='张三', age=10, sex='男', classes=null}, Student{sid=2, name='李四', age=10, sex='女', classes=null}], teacherList=[Teacher{tid=1, tname='王老师', classes=null}, Teacher{tid=2, tname='李老师', classes=null}]}
- Classes{cid=2, className='三年二班', studentList=[Student{sid=3, name='尚尚', age=10, sex='男', classes=null}, Student{sid=4, name='百战', age=11, sex='男', classes=null}, Student{sid=5, name='王五', age=10, sex='男', classes=null}], teacherList=[Teacher{tid=2, tname='李老师', classes=null}, Teacher{tid=3, tname='张老师', classes=null}]}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。