SpringBoot学习2—整合第三方技术

文章目录

  • 一. 整合JUnit
    • 1 使用方法
    • 2 目录结构若改变让测试类生效
  • 二. 整合Mybatis
    • 1 Mybatis运行条件
    • 2 Mybatis搭建过程
  • 三. 整合Mybatis-plus
  • 四. 整合Druid
    • 1 简介
    • 2 Druid搭建过程

一. 整合JUnit

简介:JUnit是一个java编程语言单元测试框架,其在测试驱动的开发方面有很重要的发展,是起源于JUnit的一个统称为xUnit的单元测试框架。JUnit使用的使用“先测试后编码”的理念

1 使用方法

创建与SprinBoot同样的目录结果的测试文件夹(目录结构不同测试类会失效),然后写入测试类的java文件即可,目录结构和具体代码和注释如下:
SpringBoot学习2—整合第三方技术_第1张图片

@SpringBootTest //测试类核心注解,必须加
class GetspringApplicationTests {
    @Autowired  //自动注入要测试的类对象
    Mydatasource mydatasource; 
    @Test //测试方法注解
    void contextLoads() {
        mydatasource.setDriver("1");
        System.out.println("mydatasource.getDriver()");
    }
}

2 目录结构若改变让测试类生效

只需要在测试核心注解加入classes属性即可或在测试类上加入@ContextConfiguration注解

@SpringBootTest(classes=GetspringApplication.class)//GetspringApplication是springboot的启动类类名

@ContextConfiguration(classes=GetspringApplication.class)
//classes用于设置SpringBoot的启动类

二. 整合Mybatis

1 Mybatis运行条件

  1. Mybatis的核心配置文件:配置数据库的连接信息
  2. Mybatis的Sql映射(XML方式/注解方式)

2 Mybatis搭建过程

  1. 创建SpringBoot项目,导入Mybatis框架和Mysql驱动

    此时SpringBoot已经帮我们把Mybatis的依赖包导入了
    SpringBoot学习2—整合第三方技术_第2张图片

  2. 在主配置文件中配置数据库参数

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

  1. 安装原来SSM中的开发流程开发即可
  • 创建pojo
//使用Lomboku
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Department_information {
    private int detid;
    private String detname;
    private String dephonenumber ;
}
  • 创建Mapper(使用的是注解方式)
@Mapper
@Component
public interface Department_informationDao {
    @Select("select * from Department_information")
    public List<Department_information> getAll();

}

  • 测试类中测试
@SpringBootTest
class SpringTest2ApplicationTests {
    @Autowired
    private Department_informationDao departmentInformationDao;
    @Test
    void contextLoads() {
        List<Department_information> department_informationList=departmentInformationDao.getAll();
        department_informationList.forEach(System.out::println);
    }
}

查询结果:
在这里插入图片描述

三. 整合Mybatis-plus

见我这篇博客

四. 整合Druid

1 简介

在项目与数据库建立连接(Connection)和断开连接是有时间开销的,而且在某一个时间点(同时)连接数据库的客户端可能并没有想象中的那么多,这就意味着数据库连接对象是可以重用的。数据库连接池的概念就是,预先准备好若干个与数据库建立好的连接,未来谁需要使用,就直接取一个连接,在使用完毕后,再将连接还给数据库连接池(而不是真正的断开连接),如此来提高程序性能。在使用数据库连接池时,对于Connection对象的管理工作就完全转交到了它们手中,Connection对象不再由我们创建和销毁。

2 Druid搭建过程

  1. 导入依赖
<dependency>
       <groupId>com.alibabagroupId>
       <artifactId>druid-spring-boot-starterartifactId>
       <version>1.2.9version>
 dependency>
  1. 配置配置文件
#方案一
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/Hrmsdatabase
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
#方案二
spring:
  datasource:
    druid:
      username: root
      password: 123456
      url:jdbc:mysql://localhost:3306/Hrmsdatabase
      driver-class-name:com.mysql.cj.jdbc.Driver
  1. 编写Pojo
import lombok.*;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department_information {
    private int detid;
    private String detname;
    private String dephonenumber ;
}
  1. 编写Mapper(使用的是注解方式)
@Mapper
@Component
public interface Department_informationDao {
    @Select("select * from Department_information")
    public List<Department_information> getAll();

}
  1. 测试类测试
@SpringBootTest
class SpringTest2ApplicationTests {
    @Autowired
    private Department_informationDao departmentInformationDao;
    @Test
    void contextLoads() {
        List<Department_information> department_informationList=departmentInformationDao.getAll();
        department_informationList.forEach(System.out::println);
    }
}

在这里插入图片描述

你可能感兴趣的:(springboot,springboot)