[SpringBoot]SpringBoot整合第三方技术

导读:本篇文章讲解 [SpringBoot]SpringBoot整合第三方技术,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

也许你感觉自己的努力总是徒劳无功,但不必怀疑,你每天都离顶点更进一步。今天的你离顶点还遥遥无期。但你通过今天的努力,积蓄了明天勇攀高峰的力量。加油!

SpringBoot整合junit

回顾 Spring 整合 junit

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {
    
    @Autowired
    private BookService bookService;
    
    @Test
    public void testSave(){
        bookService.save();
    }
}

使用 @RunWith 注解指定运行器,使用 @ContextConfiguration 注解来指定配置类或者配置文件。而 SpringBoot 整合 junit 特别简单,分为以下三步完成

  • 在测试类上添加 SpringBootTest 注解
  • 使用 @Autowired 注入要测试的资源
  • 定义测试方法进行测试

环境准备

创建一个名为 springboot_07_testSpringBoot 工程,工程目录结构如下
在这里插入图片描述

com.itheima.service 下创建 BookService 接口,内容如下

public interface BookService {
    public void save();
}

com.itheima.service.impl 包写创建一个 BookServiceImpl 类,使其实现 BookService 接口,内容如下

@Service
public class BookServiceImpl implements BookService {
    @Override
    public void save() {
        System.out.println("book service is running ...");
    }
}

编写测试类

test/java 下创建 com.itheima 包,在该包下创建测试类,将 BookService 注入到该测试类中

@SpringBootTest
class Springboot07TestApplicationTests {

    @Autowired
    private BookService bookService;

    @Test
    public void save() {
        bookService.save();
    }
}

注意这里的引导类所在包必须是测试类所在包及其子包
我们的测试类会自动加载引导类来初始化Spring的环境。如果不在一个包里那么就找不着引导类了。

例如:

  • 引导类所在包是 com.itheima
  • 测试类所在包是 com.itheima

如果不满足这个要求的话,就需要在使用 @SpringBootTest 注解时,使用 classes 属性指定引导类的字节码对象。如 @SpringBootTest(classes = Springboot07TestApplication.class)

在这里我们认识了一个新的注解:
在这里插入图片描述

原来我们在Spring整合Junit的时候还要在类上面配上加载的配置文件(@ContextConfiguration(classes = SpringConfig.class))。我们在使用SpringBoot整合Junit的时候虽然没有明着写,但其实也加载了。
我们的引导类其实启到了配置类的作用,它会把他所在的包及其子包全部扫描一遍,所以说我们写的@Service才能加载成bean:
在这里插入图片描述

SpringBoot整合mybatis

回顾Spring整合Mybatis

Spring 整合 Mybatis 需要定义很多配置类

  • SpringConfig 配置类

    • 导入 JdbcConfig 配置类

    • 导入 MybatisConfig 配置类

      @Configuration
      @ComponentScan("com.itheima")
      @PropertySource("classpath:jdbc.properties")
      @Import({JdbcConfig.class,MyBatisConfig.class})
      public class SpringConfig {
      }
      
      
  • JdbcConfig 配置类

    • 定义数据源(加载properties配置项:driver、url、username、password)

      public class JdbcConfig {
          @Value("${jdbc.driver}")
          private String driver;
          @Value("${jdbc.url}")
          private String url;
          @Value("${jdbc.username}")
          private String userName;
          @Value("${jdbc.password}")
          private String password;
      
          @Bean
          public DataSource getDataSource(){
              DruidDataSource ds = new DruidDataSource();
              ds.setDriverClassName(driver);
              ds.setUrl(url);
              ds.setUsername(userName);
              ds.setPassword(password);
              return ds;
          }
      }
      
  • MybatisConfig 配置类

    • 定义 SqlSessionFactoryBean

    • 定义映射配置

      @Bean
      public MapperScannerConfigurer getMapperScannerConfigurer(){
          MapperScannerConfigurer msc = new MapperScannerConfigurer();
          msc.setBasePackage("com.itheima.dao");
          return msc;
      }
      
      @Bean
      public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource dataSource){
          SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
          ssfb.setTypeAliasesPackage("com.itheima.domain");
          ssfb.setDataSource(dataSource);
          return ssfb;
      }
      
      

SpringBoot整合mybatis

创建模块

  • 创建新模块,选择 Spring Initializr,并配置模块相关基础信息

    在这里插入图片描述

  • 选择当前模块需要使用的技术集(MyBatis、MySQL)

    在这里插入图片描述

定义实体类

com.itheima.domain 包下定义实体类 Book,内容如下

public class Book {
    private Integer id;
    private String name;
    private String type;
    private String description;
    
    //setter and  getter
    
    //toString
}

定义dao接口

com.itheima.dao 包下定义 BookDao 接口,内容如下

public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}

定义测试类

test/java 下定义包 com.itheima ,在该包下测试类,内容如下

@SpringBootTest
class Springboot08MybatisApplicationTests {

	@Autowired
	private BookDao bookDao;

	@Test
	void testGetById() {
		Book book = bookDao.getById(1);
		System.out.println(book);
	}
}

编写配置

我们代码中并没有指定连接哪儿个数据库,用户名是什么,密码是什么。所以这部分需要在 SpringBoot 的配置文件中进行配合。

application.yml 配置文件中配置如下内容

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db
    username: root
    password: root

测试

运行测试方法,我们会看到如下错误信息

在这里插入图片描述

错误信息显示在 Spring 容器中没有 BookDao 类型的 bean。为什么会出现这种情况呢?

原因是 Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口。而我们要解决这个问题需要在BookDao 接口上使用 @MapperBookDao 接口改进为

@Mapper
public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}

注意:

SpringBoot 版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区 jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC,或在MySQL数据库端配置时区解决此问题

使用Druid数据源

现在我们并没有指定数据源,SpringBoot 有默认的数据源,我们也可以指定使用 Druid 数据源,按照以下步骤实现

  • 导入 Druid 依赖

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.16</version>
    </dependency>
    
  • application.yml 配置文件配置

    可以通过 spring.datasource.type 来配置使用什么数据源。配置文件内容可以改进为

    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
    

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/122044.html

(0)

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!