文章目录
- 1、整合JUnit
- 2、整合Mybatis
- 3、案例
1、整合JUnit
SSM下整合JUnit:
每一个JUnit的整合都需要设置运行器、加载环境,可以省略。
而SpringBoot加速开发:
加了@SpringBootTest后,平替掉设置运行器和加载环境这两步,默认加载引导类(启动类)作为配置文件(前提是启动类和测试类都属于"com.itheima"包下)。
启动类的作用是把启动类所在的包及其子包全部扫描一遍,自动实现bean的加载和配置类的加载(扫描@bean、@Configuration等注解自动注入)
也可以自己指定加载引导类:
小结:
SpringBoot整合JUnit只需要在类上加@SpringBootTest即可
2、整合Mybatis
boot整合SSM只需要整合MyBatis
SSM整合MyBaits的繁琐操作:
1、创建Spring核心配置类导入Jdbc和MyBatis的配置类
2、Jdbc的配置类需要读取Properties文件中的数据源(数据库的访问参数)
3、MyBatis的配置文件里面需要定义sqlSessionFactoryBean和映射配置(代理)
上述操作几乎全部省略
boot整合MyBatis:
1、创建项目的时候勾选boot整合mybatis的起步依赖
可以2个看到起步依赖已经加上了
2、创建实体类,用来对应数据库中字段(名字最好和数据库一样)
3、在Dao层方法上加@Mapper,映射
4、在配置文件里面加数据源的账号密码等参数
5、进行测试
小结:
3、案例
题目:将SSM整合的案例改写在SpringBoot项目里面
步骤:
1、pom.xml:除了勾选web和mysql、mybatis相关的起步依赖,再引入Druid数据库连接池的依赖
2、Dao:给Dao层加@Mapper注解
3、在application.yml文件里面设置端口号,使用Druid数据库连接池,且配置数据源的参数
4、测试Service层(不包页面)
5、将页面放在static文件夹里面
6、启动即可