好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot整合第三方技术的详细步骤

SpringBoot整合第三方技术

一、整合Junit

新建一个SpringBoot项目

使用@SpringBootTest标签在test测试包内整合Junit

?

1

2

3

4

5

6

7

8

9

10

@SpringBootTest

class Springboot03JunitApplicationTests {

 

     @Autowired

     private BookService bookService;

     @Test

     void contextLoads() {

         bookService.save();

     }

}

名称:@SpringBootTest 类型:测试类注解 位置:测试类定义上方 作用:设置Junnit加载的SpringBoot启动类

注意:整合的Junit测试类需要和Java包中的配置文件类放在同一目录下,否则需要指定配置java文件的class

?

1

2

3

4

5

6

7

8

9

10

@SpringBootTest (classes = Springboot03JunitApplication. class )

class Springboot03JunitApplicationTests {

 

     @Autowired

     private BookService bookService;

     @Test

     void contextLoads() {

         bookService.save();

     }

}

二、整合Mybatis

创建新模块的时候选择需要的技术集

之后就可以看到mybatis相应的坐标已经导入完成

接着设置数据源

?

1

2

3

4

5

6

spring:

   datasource:

     driver- class -name: com.mysql.jdbc.Driver

     url: jdbc:mysql: //localhost:3306/test

     username: root

     password: 123456

定义数据层接口与映射配置

?

1

2

3

4

public interface UserDao {

     @Select ( "select * from test.sys_role;" )

     public List<Role> getAll();

}

测试类中注入dao接口,测试功能

?

1

2

3

4

5

6

7

8

9

10

@SpringBootTest

class Springboot04MybatisApplicationTests {

     @Autowired

     private UserDao userDao;

     @Test

     void contextLoads() {

         List<Role> roleList = userDao.getAll();

         System.out.println(roleList);

     }

}

注意:

数据库SQL映射需要添加@Mapper被容器识别到 数据库连接相关信息转换成配置 SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题

?

1

jdbc:mysql://localhost:3306/test?serverTimezone=UTC

三、整合Mybatis-Plus

Mybatis-Plus与Mybati 区别

导入坐标不同 数据层实现简化

注意:由于SpringBoot中未收录MyBatis-Plus的坐标版本,需要指定对应的Version

SpringBoot没有整合Mybatis-Plus,所以需要我们手动添加SpringBoot整合MyBatis-Plus的坐标,可以通过mvnrepository获取

?

1

2

3

4

5

<dependency>

          <groupId>com.baomidou</groupId>

          <artifactId>mybatis-plus-boot-starter</artifactId>

          <version> 3.4 . 3 </version>

      </dependency>

定义数据层接口与映射配置,继承BaseMapper

?

1

2

3

@Mapper

public interface UserDao extends BaseMapper<Role> {

}

在yml配置文件配置数据库前缀

?

1

2

3

4

5

#设置mp相关配置

mybatis-plus:

   global-config:

     db-config:

       table-prefix: sys_

测试

?

1

2

3

4

5

6

7

8

9

10

@SpringBootTest

class Springboot05MybatisPlusApplicationTests {

     @Autowired

     private UserDao userDao;

     @Test

     void contextLoads() {

         Role role = userDao.selectById( 1 );

         System.out.println(role);

     }

}

四、整合Druid

同样的,Druid也需要自己手工整合

Maven导入依赖

?

1

2

3

4

5

<dependency>

     <groupId>com.alibaba</groupId>

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

     <version> 1.2 . 6 </version>

</dependency>

在yml配置文件指定数据源

?

1

2

3

4

5

6

7

spring:

     datasource:

         driver- class -name: com.mysql.cj.jdbc.Driver

         url: jdbc:mysql: //localhost:3306/ssm_db?serverTimezone=UTC

         username: root

         password: root

         type: com.alibaba.druid.pool.DruidDataSource

或者

?

1

2

3

4

5

6

7

spring:

     datasource:

       druid:

         driver- class -name: com.mysql.cj.jdbc.Driver

         url: jdbc:mysql: //localhost:3306/ssm_db?serverTimezone=UTC

         username: root

         password: root

五、总结

整合第三方技术的步骤:

导入对应的starter

druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root

到此这篇关于SpringBoot整合第三方技术的文章就介绍到这了,更多相关SpringBoot整合第三方内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_51146329/article/details/123483892

查看更多关于SpringBoot整合第三方技术的详细步骤的详细内容...

  阅读:17次