• [SpringBoot]SpringBoot整合第三方技术


    SpringBoot整合junit

    回顾 Spring 整合 junit

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfig.class)
    public class UserServiceTest {
        
        @Autowired
        private BookService bookService;
        
        @Test
        public void testSave(){
            bookService.save();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

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

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

    环境准备

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

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

    public interface BookService {
        public void save();
    }
    
    • 1
    • 2
    • 3

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

    @Service
    public class BookServiceImpl implements BookService {
        @Override
        public void save() {
            System.out.println("book service is running ...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    编写测试类

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

    @SpringBootTest
    class Springboot07TestApplicationTests {
    
        @Autowired
        private BookService bookService;
    
        @Test
        public void save() {
            bookService.save();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注意这里的引导类所在包必须是测试类所在包及其子包
    我们的测试类会自动加载引导类来初始化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 {
        }
        
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
    • 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;
            }
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
    • 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;
        }
        
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15

    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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    定义dao接口

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

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

    定义测试类

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

    @SpringBootTest
    class Springboot08MybatisApplicationTests {
    
    	@Autowired
    	private BookDao bookDao;
    
    	@Test
    	void testGetById() {
    		Book book = bookDao.getById(1);
    		System.out.println(book);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    编写配置

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

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

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ssm_db
        username: root
        password: root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    测试

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

    在这里插入图片描述

    错误信息显示在 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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意:

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

    使用Druid数据源

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

    • 导入 Druid 依赖

      <dependency>
          <groupId>com.alibabagroupId>
          <artifactId>druidartifactId>
          <version>1.1.16version>
      dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 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
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
  • 相关阅读:
    Java中的::
    Part 4.2 背包动态规划
    excel将文件夹下面的表格文件指定名称的sheet批量导出到指定文件中,并按照文件名保存在新文件的不同sheet中
    GRACE-FO L2产品的发布说明 - 版本UTCSR RL-06.1产品
    案例分享|企小码为博尔迈生物实现会话存档私有化,助生物医药产业管理优化
    Matlab|基于生物地理学的优化 (BBO)
    Visual Studio 2019 简单安装教程
    如何解决 npm ERR! Cannot read properties of null (reading ‘pickAlgorithm‘)报错问题
    基于Android studio开发一款垃圾分类知识宣传APP
    Programming Differential Privacy第十四章Local Differential Privacy本地化差分隐私
  • 原文地址:https://blog.csdn.net/zyb18507175502/article/details/126012372