写点什么

MyBatis 实现一对一,一对多关联查询,java 程序设计教程课后题

用户头像
极客good
关注
发布于: 刚刚

<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-test</artifactId>


<scope>test</scope>


</dependency>


[](


)在 application.yml 文件中进行数据库和 mybatis 配置



spring 配置

spring:

数据库配置

datasource:


type: com.zaxxer.hikari.HikariDataSource


url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true&useSSL=false&tinyInt1isBit=false&serverTimezone=GMT%2B8


driverClassName: com.mysql.cj.jdbc.Driver


username: root


password: root

mybatis 配置

mybatis:

给实体类配置别名

type-aliases-package: com.jourwon.*.pojo

加载 mybatis 的 mapper 配置文件

mapper-locations: classpath:mybatis/mapper/**/*Mapper.xml


configuration:

开发环境控制台打印 sql 语句

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

开启驼峰规则自动映射字段属性值;如字段为 user_name 的可以映射到 userName 属性中

map-underscore-to-camel-case: true

设置 sql 执行超时时间,以秒为单位的全局 sql 超时时间设置,当超出了设置的超时时间时,会抛出 SQLTimeoutException

default-statement-timeout: 30

解决查询返回结果含 null 没有对应字段值问题

call-setters-on-nulls: true


[](


)新建老师类 Teacher




@Data


public class Teacher {


private int id;


private String name;


}


[](


)新建班级类 Classes




@Data


public class Classes {


private int id;


private String name;


private Teacher teacher;


private List<Student> studentList;


}


[](


)新建学生类 Student




@Data


public class Student {


private int id;


private String name;


}


[](


)新建 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">


<mapper namespace="com.jourwon.mybatis.mapper.ClassesMapper">


<select id="listClasses" parameterType="int" resultMap="ClassesResultMap">


select * from classes c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}


</select>


<resultMap type="com.jourwon.mybatis.pojo.Classes" id="ClassesResultMap">


<id property="id" column="c_id"/>


<result property="name" column="c_name"/>


<association property="teacher" javaType="com.jourwon.mybatis.pojo.Teacher">


<id property="id" column="t_id"/>


<result property="name" column="t_name"/>


</association>


</resultMap>


<select id="listClasses2" parameterType="int" resultMap="ClassesResultMap2">


select * from classes c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}


</select>


<resultMap type="com.jourwon.mybatis.pojo.Classes" id="ClassesResultMap2">


<id property="id" column="c_id"/>


<result property="name" column="c_name"/>


<association property="teacher" javaType="com.jourwon.mybatis.pojo.Teacher">


<id property="id" column="t_id"/>


<result property="name" column="t_name"/>


</association>


<collection property="studentList" ofType="com.jourwon.mybatis.pojo.Student">


<id property="id" column="s_id"/>


<result property="name" column="s_name"/>


</collection>


</resultMap>


</mapper>


[](


)Application 类




@MapperScan(basePackages = {"com.jourwon.mybatis.**.mapper"})


@SpringBootApplication


public class Application {


public static void main(String[] args) {


SpringApplication.run(Application.class, args);


}


}


[](


)测试类




@RunWith(SpringRunner.class)


@SpringBootTest


public class ApplicationTests {


@Autowired


private ClassesMapper classesMapper;


/**


  • Description: 测试班级与老师一对一关联关系

  • @author JourWon

  • @date 2019/9/24 23:18

  • @param

  • @return void


*/


@Test


public void test01() {


List<Classes> classes = classesMapper.listClasses(1);


for (Classes aClass : classes) {


System.out.println(classes);


}


}


/**


  • Description: 测试班级与学生一对多关联关系

  • @author JourWon

  • @date 2019/9/24 23:19

  • @param

  • @return void


*/


@Test


public void test02() {


List<Classes> classes = classesMapper.listClasses2(1);


for (Classes aClass : classes) {


System.out.println(classes);


}


}


}


[](


)测试结果




test01()测试结果输出


==> Preparing: select * from classes c,teacher t where c.teacher_id=t.t_id and c.c_id=?


==> Parameters: 1(Integer)


<== Columns: c_id, c_name, teacher_id, t_id, t_name


<== Row: 1, 高三 2 班, 1, 1, 老师张三


<== Total: 1


Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@140d1230]

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
MyBatis实现一对一,一对多关联查询,java程序设计教程课后题