数据库准备工作完成后,使用IDEA新建一个Spring Boot项目,工程目录如下
[]( )新建Spring Boot项目
[]( )在pom.xml文件中添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<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);
}
}
# 总结
虽然面试套路众多,但对于技术面试来说,主要还是考察一个人的技术能力和沟通能力。不同类型的面试官根据自身的理解问的问题也不尽相同,没有规律可循。
![](https://s2.51cto.com/images/20210905/1630818406201654.jpg)
![](https://s2.51cto.com/images/20210905/1630818407682070.jpg)
**上面提到的关于这些JAVA基础、三大框架、项目经验、并发编程、JVM及调优、网络、设计模式、spring+mybatis源码解读、Mysql调优、分布式监控、消息队列、分布式存储等等面试题笔记及资料都是免费分享的,有需要学习的小伙伴可以直接[点点点点点这里免费下载](https://gitee.com/vip204888/java-p7)**
有些面试官喜欢问自己擅长的问题,比如在实际编程中遇到的或者他自己一直在琢磨的这方面的问题,还有些面试官,尤其是大厂的比如 BAT 的面试官喜欢问面试者认为自己擅长的,然后通过提问的方式深挖细节,刨根到底。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/140453.html