文章目录

  • 1、整合JUnit
  • 2、整合Mybatis
  • 3、案例


1、整合JUnit

SSM下整合JUnit:

每一个JUnit的整合都需要设置运行器、加载环境,可以省略

springboot 启动时加载一个类 springboot test加载启动类_spring boot


而SpringBoot加速开发:

加了@SpringBootTest后,平替掉设置运行器和加载环境这两步,默认加载引导类(启动类)作为配置文件(前提是启动类和测试类都属于"com.itheima"包下)。

springboot 启动时加载一个类 springboot test加载启动类_配置文件_02

启动类的作用是把启动类所在的包及其子包全部扫描一遍,自动实现bean的加载和配置类的加载(扫描@bean、@Configuration等注解自动注入)

springboot 启动时加载一个类 springboot test加载启动类_配置文件_03

也可以自己指定加载引导类:

springboot 启动时加载一个类 springboot test加载启动类_springboot 启动时加载一个类_04

小结:

SpringBoot整合JUnit只需要在类上加@SpringBootTest即可

springboot 启动时加载一个类 springboot test加载启动类_springboot 启动时加载一个类_05


springboot 启动时加载一个类 springboot test加载启动类_spring boot_06



2、整合Mybatis

boot整合SSM只需要整合MyBatis

springboot 启动时加载一个类 springboot test加载启动类_spring boot_07


SSM整合MyBaits的繁琐操作:

1、创建Spring核心配置类导入Jdbc和MyBatis的配置类

springboot 启动时加载一个类 springboot test加载启动类_加载_08

2、Jdbc的配置类需要读取Properties文件中的数据源(数据库的访问参数)

springboot 启动时加载一个类 springboot test加载启动类_spring boot_09

3、MyBatis的配置文件里面需要定义sqlSessionFactoryBean和映射配置(代理)

springboot 启动时加载一个类 springboot test加载启动类_配置文件_10


上述操作几乎全部省略

boot整合MyBatis:

1、创建项目的时候勾选boot整合mybatis的起步依赖

springboot 启动时加载一个类 springboot test加载启动类_配置文件_11


可以2个看到起步依赖已经加上了

springboot 启动时加载一个类 springboot test加载启动类_配置文件_12

2、创建实体类,用来对应数据库中字段(名字最好和数据库一样)

springboot 启动时加载一个类 springboot test加载启动类_springboot 启动时加载一个类_13


3、在Dao层方法上加@Mapper,映射

springboot 启动时加载一个类 springboot test加载启动类_加载_14


4、在配置文件里面加数据源的账号密码等参数

springboot 启动时加载一个类 springboot test加载启动类_spring boot_15

5、进行测试

springboot 启动时加载一个类 springboot test加载启动类_springboot 启动时加载一个类_16


小结:

springboot 启动时加载一个类 springboot test加载启动类_springboot 启动时加载一个类_17


springboot 启动时加载一个类 springboot test加载启动类_配置文件_18


springboot 启动时加载一个类 springboot test加载启动类_springboot 启动时加载一个类_19


springboot 启动时加载一个类 springboot test加载启动类_配置文件_20


springboot 启动时加载一个类 springboot test加载启动类_springboot 启动时加载一个类_21



3、案例

题目:将SSM整合的案例改写在SpringBoot项目里面

步骤:

springboot 启动时加载一个类 springboot test加载启动类_数据源_22


1、pom.xml:除了勾选web和mysql、mybatis相关的起步依赖,再引入Druid数据库连接池的依赖

springboot 启动时加载一个类 springboot test加载启动类_数据源_23


2、Dao:给Dao层加@Mapper注解

springboot 启动时加载一个类 springboot test加载启动类_spring boot_24


3、在application.yml文件里面设置端口号,使用Druid数据库连接池,且配置数据源的参数

springboot 启动时加载一个类 springboot test加载启动类_配置文件_25


4、测试Service层(不包页面)

springboot 启动时加载一个类 springboot test加载启动类_spring boot_26

5、将页面放在static文件夹里面

springboot 启动时加载一个类 springboot test加载启动类_spring boot_27


6、启动即可

springboot 启动时加载一个类 springboot test加载启动类_加载_28