这篇“MyBatis中怎么实现一对多查询和多对一查询”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“MyBatis中怎么实现一对多查询和多对一查询”文章吧。
数据库
CREATE TABLE teacher ( id INT(10) NOT NULL, NAME VARCHAR(64) DEFAULT NULL, PRIMARY KEY (id), )ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO teacher (id ,NAME) VALUES (1,'罗老师'); CREATE TABLE student ( id INT(10) NOT NULL, NAME VARCHAR(64) DEFAULT NULL, tid INT(10) DEFAULT NULL, PRIMARY KEY (id), KEY fktid (tid), CONSTRAINT fktid FOREIGN KEY (tid) REFERENCES teacher (id) ) ALTER TABLE student ENGINE=INNODB DEFAULT CHARSET=utf8; INSERT INTO student (id ,NAME ,tid) VALUES (1,'小明',1); INSERT INTO student (id ,NAME ,tid) VALUES (2,'小红',1); INSERT INTO student (id ,NAME ,tid) VALUES (3,'小张',1); INSERT INTO student (id ,NAME ,tid) VALUES (5,'小罗',1);
MyBatis.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> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/><!--日记log4j--> </settings> <typeAliases> <package name="com.Google.pojo"/><!--给实体类取别名--> </typeAliases> <!--<typeAliases> <typeAlias type="com.Google.pojo.User" alias="user"/> </typeAliases>--> <environments default="development"><!--可以创建多个环境--> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/><!--加载驱动--> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/><!--连接数据库--> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!--注册接口--> <mappers> <mapper resource="com/Google/Dao/StudentMapper.xml"/> <mapper resource="com/Google/Dao/TeacherMapper.xml"/> </mappers> </configuration>
学生·
@Data public class Student { private int id; private String name; private Teacher teacher; }
老师·
@Data public class Teacher { private int id; private String name; }
public interface StudentMapper { List<Student> getStudentList(); List<Student> getStudentList1(); }
<?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.Google.Dao.StudentMapper"> <!--按照查询嵌套处理--> <select id="getStudentList" resultMap="StudentMap"> select * from student </select> <resultMap id="StudentMap" type="Student" > <result property="id" column="id"/> <result property="name" column="name"/> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from teacher </select> <!--按照结果嵌套处理--> <select id="getStudentList1" resultMap="StudentMap1"> select s.id sid,s.name sname,t.name tname from Student s,Teacher t where s.tid=t.id </select> <resultMap id="StudentMap1" type="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="Teacher" > <result property="name" column="tname"/> </association> </resultMap> </mapper>
package com.Google.Dao; import com.Google.pojo.Student; import com.Google.units.sqlSessionFactory; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class StudentMapperText { @Test public void getStudent(){ SqlSession sqlSession = sqlSessionFactory.getsqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> studentList = mapper.getStudentList(); for (Student student : studentList) { System.out.println(student); } sqlSession.close(); } @Test public void getStudent1(){ SqlSession sqlSession = sqlSessionFactory.getsqlSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); List<Student> studentList = mapper.getStudentList1(); for (Student student : studentList) { System.out.println(student); } sqlSession.close(); } }
Student(id=1, name=小明, teacher=Teacher(id=0, name=罗老师))
Student(id=2, name=小红, teacher=Teacher(id=0, name=罗老师))
Student(id=3, name=小张, teacher=Teacher(id=0, name=罗老师))
Student(id=4, name=小王, teacher=Teacher(id=0, name=罗老师))
Student(id=5, name=小罗, teacher=Teacher(id=0, name=罗老师))
@Data public class Student { private int id; private String name; private int tid; }
@Data public class Teacher { private int id; private String name; //一个老师拥有多个学生,给老师创建一个学生集合 private List<Student> student; }
public interface TeacherMapper { Teacher getTeacher(@Param("tid") int id); Teacher getTeacher1(@Param("tid") int id); }
<?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.Google.Dao.TeacherMapper"> <!--按结果嵌套查询--> <select id="getTeacher" 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"/> <!--ofType="" 用于获取集合中泛型的信息--> <collection property="student" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <!--嵌套表查询(子查询)--> <select id="getTeacher1" resultMap="TeacherStudent1"> select * from teacher where id=#{tid} </select> <resultMap id="TeacherStudent1" type="Teacher"> <result property="id" column="id"/> <collection property="student" javaType="ArrayList" ofType="Student" select="getStudetByID" column="id"/> </resultMap> <select id="getStudetByID" resultType="Student"> select * from student where tid=#{tid} </select> </mapper>
public class TeacherMapperText { @Test public void getTeacher(){ SqlSession sqlSession = sqlSessionFactory.getsqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher = mapper.getTeacher(1); System.out.println(teacher); sqlSession.close(); } @Test public void getTeacher1(){ SqlSession sqlSession = sqlSessionFactory.getsqlSession(); TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class); Teacher teacher1 = mapper.getTeacher1(1); System.out.println(teacher1); sqlSession.close(); } }
Teacher(id=1, name=罗老师, student=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小张, tid=1), Student(id=4, name=小王, tid=1), Student(id=5, name=小罗, tid=1)])
以上就是关于“MyBatis中怎么实现一对多查询和多对一查询”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。