文章目录
一. 整合JUnit
简介:JUnit是一个java编程语言单元测试框架,其在测试驱动的开发方面有很重要的发展,是起源于JUnit的一个统称为xUnit的单元测试框架。JUnit使用的使用“先测试后编码”的理念
1 使用方法
创建与SprinBoot同样的目录结果的测试文件夹(目录结构不同测试类会失效),然后写入测试类的java文件即可,目录结构和具体代码和注释如下:
@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运行条件
- Mybatis的核心配置文件:配置数据库的连接信息
- Mybatis的Sql映射(XML方式/注解方式)
2 Mybatis搭建过程
-
创建SpringBoot项目,导入Mybatis框架和Mysql驱动
此时SpringBoot已经帮我们把Mybatis的依赖包导入了
-
在主配置文件中配置数据库参数
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatisplus
driver-class-name: com.mysql.cj.jdbc.Driver
- 安装原来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搭建过程
- 导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
- 配置配置文件
#方案一
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
- 编写Pojo
import lombok.*;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
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);
}
}