// 这里就是包,倘若为com.itheima1,classes需要设置为启动类.classpackage com.itheima;import com.itheima.Springboot07TestApplication;import com.itheima.service.BookService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;// 设置JUnit加载的SpringBoot启动类(类似于@RunWith和@ContextConfiguration的整合)@SpringBootTestclass Springboot07TestApplicationTests {// 自动装配测试对象(未发生变化)@Autowiredprivate BookService bookService;// 测试方法(未发生变化)@Testpublic void save() {bookService.save();}}/*名称:@SpringBootTest类型:测试类注解位置:测试类定义上方作用:设置JUnit加载的SpringBoot启动类相关属性: classes:设置SpringBoot启动类注意点: 如果该测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定 当该测试类与启动主Java类不属于同一目录名称下时,需要设置classes属性为启动类 @SpringBootTest(classes = Springboot07TestApplication.class)*/
整合MyBatis我们如果想要采用SpringBoot整合SSM,那么就需要同时整合以下三门技术:
- Spring
- SpringMVC
- MyBatis
所以我们将MyBatis单列出来提前进行整合学习,为后续的SSM整合打下基础##
Spring整合MyBatisSpring对MyBatis的整合主要从三部分进行:
- SpringConfig
- 导入JdbcConfig
- 导入MyBatisConfig
- JdbcConfig
- 定义数据源(加载properties项:driver,url,username,password)
- MyBatisConfig
- 定义sqlSessionFactoryBean
- 定义映射配置
SpringBoot整合MyBatis我们同样从头开始整合:
- 创建项目(这次我们需要MyBatis和Mysql两门技术栈)

文章插图
- 查看pom.xml并稍作讲解
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0</version> </parent> <groupId>com.itheima</groupId> <artifactId>springboot_08_mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <properties><java.version>1.8</java.version> </properties> <dependencies><!--自动添加MyBatis相关依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><!--自动添加mysql相关依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--手动添加druid--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency> </dependencies> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build></project>
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 法伯丽护肤品是哪里的护肤品品牌?
- 阿玛尼红气垫适合什么肤质?
- 阿玛尼满天星男生可以带吗?
- 阿玛尼满天星有几个版本?
- 阿玛尼护肤品适合年龄?
- 电视剧伪钞者之末路剧情介绍?
- 法兰琳卡什么档次的护肤品?
- 羽田浩司出现在哪一集?
- 电视剧羽你同行演员表介绍?
- 电视剧伪钞者之末路结局是什么?