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)
项目结构
文章图片
导入依赖
mysql mysql-connector-java5.1.48 org.projectlombok lombok1.18.20 org.mybatis mybatis3.5.6 junit junit4.11 test
mysql 配置文件
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/ssm-study?useSSL=falsejdbc.username=rootjdbc.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 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 {// 获取老师ListgetTeacher(); //获取指定老师下的所有学生及老师的信息Teacher getTeacher2(@Param("tid") Integer id); //获取指定老师下的所有学生及老师的信息Teacher getTeacher3(@Param("tid") Integer id); }
写一个TeacherMapper.xml文件
select * from teacherselect 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 * from teacher where id=#{tid} select* fromstudent_2 where tid=#{tid}
【Mybatis结果集映射一对多简单入门教程】mybatis-config.xml 文件
测试:
/** * @Author: crush * @Date: 2021-06-17 18:22 * version 1.0 */public class MyTest {@Testpublic void getTeacher(){SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Listteacher = mapper.getTeacher(); System.out.println(teacher); session.close(); }@Testpublicvoid getTeacher2(){SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Teacher teacher2 = mapper.getTeacher2(1); System.out.println(teacher2); session.close(); }@Testpublicvoid getTeacher3(){SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Teacher teacher2 = mapper.getTeacher3(1); System.out.println(teacher2); session.close(); }}
以上就是Mybatis结果集映射一对多 的详细内容,更多关于Mybatis结果集映射的资料请关注脚本之家其它相关文章!
推荐阅读
- 图书集合完毕
- Activiti(一)SpringBoot2集成Activiti6
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- mybatisplus|mybatisplus where QueryWrapper加括号嵌套查询方式
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- 雅集
- 解读《摩根集团》(1)
- 私有化轻量级持续集成部署方案--03-部署web服务(下)