Mybatis结果集映射一对多简单入门教程
Mybatis结果集映射一对多简单入门教程
Mybatis 一对多 简单入门 易懂
搭建数据库环境
student 表
DROP TABLE IF EXISTS `student_2`; CREATE TABLE `student_2` ( `id` int(10) NOT NULL, `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `tid` int(10) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `fktid`(`tid`) USING BTREE, CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; INSERT INTO `student_2` VALUES (1, '小明', 1); INSERT INTO `student_2` VALUES (2, '邱ss', 2); INSERT INTO `student_2` VALUES (3, '邱大哥', 3); INSERT INTO `student_2` VALUES (4, '杨大哥', 1); INSERT INTO `student_2` VALUES (5, '杨ss', 2);
teacher
DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES (1, '小王老师'); INSERT INTO `teacher` VALUES (2, '小李老师'); INSERT INTO `teacher` VALUES (3, '小黑老师');
idea 搭建maven 项目 (mybatis-demo)
项目结构
导入依赖
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>
mysql 配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm-study?useSSL=false jdbc.username=root jdbc.password=123456
创建pojo 类
学生
/** * @Author: crush * @Date: 2021-06-17 18:23 * version 1.0 */ public class Student { /** * 学生id */ private Integer id; /** * xueshneg xingming */ private String name; /** * 老师id */ private Integer tid; }
老师
/** * @Author: crush * @Date: 2021-06-17 18:23 * version 1.0 */ public class Teacher { /** * 老师id */ private Integer id; /** * 老师的姓名 */ private String name; /** * 每个老师是不是有很多学生 */ private List<Student> students; }
写一个mybatis 的工具类
/** * @author crush */ public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory; static { try { String resource="mybatis-config.xml"; InputStream inputStream= Resources.getResourceAsStream(resource); sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSession(){ return sqlSessionFactory.openSession(true); } }
写一个TeacherMapper
import com.crush.pojo.Teacher; import org.apache.ibatis.annotations.Param; import java.util.List; public interface TeacherMapper { // 获取老师 List<Teacher> getTeacher(); //获取指定老师下的所有学生及老师的信息 Teacher getTeacher2(@Param("tid") Integer id); //获取指定老师下的所有学生及老师的信息 Teacher getTeacher3(@Param("tid") Integer id); }
写一个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="com.crush.dao.TeacherMapper"> <select id="getTeacher" resultType="Teacher"> select * from teacher </select> <select id="getTeacher2" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.name tname, t.id tid from student_2 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"/> <!-- 复杂的属性,我们需要单独处理 对象 association 集合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="getTeacher3" resultMap="TeacherStudent3"> select * from teacher where id=#{tid} </select> <resultMap id="TeacherStudent3" type="Teacher"> <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id" /> </resultMap> <select id="getStudentByTeacherId" resultType="Student"> select * from student_2 where tid=#{tid} </select> </mapper>
mybatis-config.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="dbconfig.properties"/> <!--设置--> <settings> <!-- STDOUT_LOGGING 打印到控制台 --> <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 开启驼峰 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!--别名--> <typeAliases> <!--这是 自己 取别名--> <typeAlias alias="Student" type="com.crush.pojo.Student"/> <typeAlias alias="Teacher" type="com.crush.pojo.Teacher"/> </typeAliases> <environments default="development"> <environment id="development"> <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="mapper/StudentMapper.xml"/> <mapper resource="mapper/TeacherMapper.xml"/> </mappers> </configuration>
测试:
/** * @Author: crush * @Date: 2021-06-17 18:22 * version 1.0 */ public class MyTest { @Test public void getTeacher(){ SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); List<Teacher> teacher = mapper.getTeacher(); System.out.println(teacher); session.close(); } @Test public void getTeacher2(){ SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Teacher teacher2 = mapper.getTeacher2(1); System.out.println(teacher2); session.close(); } @Test public void getTeacher3(){ SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Teacher teacher2 = mapper.getTeacher3(1); System.out.println(teacher2); session.close(); } }
以上就是Mybatis结果集映射一对多 的详细内容,更多关于Mybatis结果集映射的资料请关注趣讯吧其它相关文章!
推荐阅读
-
洗衣机不脱水了是怎么回事(洗衣机不甩干的处理方法)
洗衣机作为大家日常生活必备的家用电器,其利用率频繁,难免会因为机械磨损、缺乏润滑油、机件老化、弹簧疲劳变形等原因,出现各种不正...
-
电子表格零基础自学教程(小白也能学明白)
可能很多人(包括我)觉得Excel不就是做个表吗,没什么好学的。然而很多大型企业在面试的时候还是会问,“会Excel吗?”“会...
-
笔记本电脑报价大全(联想笔记本多少钱)
(注意:建议在旗舰店、官方旗舰店、官网购买) 一、游戏本设计本、办公本推荐如下: 华为品牌:(全球第一大电信设备商) 1...
-
煲机软件哪个好(让耳机有个思想准备)
《无间道》中陈永仁与刘建明有过一句经典对白&mdash;&mdash;“高音甜、中音准、低音沉,总之一个词通透”。这一句话也一...
-
viewsonic平板电脑(viewsonic平板电脑刷机)
ViewSonic是一个视讯品牌,中文名字:优派。 ViewSonic 一、读音:英[vju:][?s?n?k],美[vj...
-
采访麦克风户外哪款好(讯飞智能无线麦克风C1采访神器)
对于视频创作者、直播工作者、远程培训老师、记者等媒体工作者来说,工作过程中,最让人费心的莫过于如何确保收音纯正、字幕快速生成、...
-
电脑硬件配置怎么查(详述两招快速查看电脑配置参数信息)
大家好,今天跟大家分享两个快速查看电脑配置参数信息的办法。 操作步骤如下: 1右击电脑屏幕最下方任务栏左侧的电脑徽标按钮,...
-
数据线没坏但充不上电怎么办(数据线充不上电处理方法)
苹果充电器突然充不上电是比较尴尬的问题,首先看自己的充电器数据线是不是原装,如果非原装在第一次充电时,苹果手机会提示你是否要适...
-
电脑开机出现黑屏如何处理(电脑不能开机黑屏解决方法)
电脑不能开机或者开机以后黑屏怎么解决?这里收集了所有常见的维修方法,看完秒变维修高手,实在是一篇不能错过的电脑维修教程。简单易...
-
手机宝典怎么搞(小米手机性能优化宝典)
别再总是抱怨手机卡顿,系统臃肿,反应慢,现在看完这篇文章,你会发现你并不了解小米手机,当然,文中许多方法并不是仅仅适用于小米手...