这里写目录标题
- 一、SpringBoot整合Junit
- 1.1 搭建SpringBoot工程
- 1.2 引入starter-test起步依赖
- 1.3 编写类
- 1.4 测试
- 二、SpringBoot整合mybatis
- 2.1 搭建SpringBoot工程
- 2.2 引入mybatis起步依赖,添加驱动
- 2.3 编写DataSource和MyBatis相关配置
- 2.4 定义表和实体类
- 2.5 编写dao和mapper文件/纯注解开发
- 2.5.1 纯注解开发
- 2.5.2 配置mapper文件
- 2.6 测试
- 三、SpringBoot整合redis
- 3.1 搭建SpringBoot工程
- 3.2 引入redis起步依赖
- 3.3 配置redis相关属性
- 3.4 注入RedisTemplate模板
- 3.5 编写测试方法,测试
一、SpringBoot整合Junit
1.1 搭建SpringBoot工程
1.2 引入starter-test起步依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.3 编写类
@Service
public class UserService {
public void show(){
System.out.println("这是Service里面的show....");
}
}
1.4 测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoJunitApplication.class )
public class UserServiceTest {
@Autowired
UserService service;
@Test
public void test(){
System.out.println(111);
}
@Test
public void test1(){
service.show();
}
}
二、SpringBoot整合mybatis
2.1 搭建SpringBoot工程
2.2 引入mybatis起步依赖,添加驱动
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<scope>runtime</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
2.3 编写DataSource和MyBatis相关配置
spring:
datasource:
url: jdbc:mysql://192.168.147.128:3306/springboot?serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
2.4 定义表和实体类
CREATE DATABASE `springboot` ;
USE `springboot`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
insert into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');
实体类
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.5 编写dao和mapper文件/纯注解开发
2.5.1 纯注解开发
@Mapper
@Repository
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}
2.5.2 配置mapper文件
@Mapper
@Repository
public interface UserXmlMapper {
public List<User> findAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.study.mapper.UserXmlMapper">
<select id="findAll" resultType="user">
select * from t_user
</select>
</mapper>
配置文件添加一些修改
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml # mapper映射文件路径
type-aliases-package: cn.study.domain
2.6 测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private UserXmlMapper userXmlMapper;
@Test
public void testFindAll() {
List<User> list = userMapper.findAll();
System.out.println(list);
}
@Test
public void testFindAll2() {
List<User> list = userXmlMapper.findAll();
System.out.println(list);
}
}
三、SpringBoot整合redis
3.1 搭建SpringBoot工程
按照上一篇里面介绍的新建一个springboot项目或者是模块。
3.2 引入redis起步依赖
在pom.xml里面添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3.3 配置redis相关属性
在resource文件下,application.yml文件里面添加配置参数,如果文件不存在则新建
spring:
redis:
host: 127.0.0.1
port: 6379
3.4 注入RedisTemplate模板
@Autowired
private RedisTemplate redisTemplate;
3.5 编写测试方法,测试
在测试文件夹下编写测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSet() {
//存入数据
redisTemplate.boundValueOps("name").set("zhangsan");
}
@Test
public void testGet() {
//获取数据
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}