【Spring框架家族】SpringBoot整合第三方
程序员文章站
2024-01-28 12:46:52
...
SpringBoot整合Junit
- 搭建SpringBoot工程
- 引入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>
- 编写测试类
/**
* 测试类
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJunitApplication.class )
public class UserServiceTest {
@Test
public void test(){
System.out.println(111);
}
}
SpringBoot整合mybatis
-
搭建SpringBoot工程
-
引入mybatis起步依赖,添加mysql驱动
<dependencies>
<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>
</dependencies>
- 编写DataSource和MyBatis相关配置
application.yml
# datasource
spring:
datasource:
# serverTimezone=UTC 指定标准时区
url: jdbc:mysql:///springboot?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml # mapper映射文件路径
type-aliases-package: cs.wy.springbootmybatis.domain
# config-location: # 指定mybatis的核心配置文件
- 定义表和实体类
public class User {
private int id;
private String username;
private String password;
get...set...
-
编写dao和mapper文件/纯注解开发
-
编写dao
@Mapper
@Repository
public interface UserXmlMapper {
public List<User> findAll();
}
mapper.xml
<?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="cs.wy.springbootmybatis.mapper.UserXmlMapper">
<select id="findAll" resultType="user">
select * from t_user
</select>
</mapper>
纯注解开发
@Mapper
@Repository
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}
SpringBoot整合redis
-
搭建SpringBoot工程
-
引入redis起步依赖
<dependencies>
<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>
</dependencies>
- 配置redis相关属性
spring:
redis:
host: 127.0.0.1 # redis的主机ip
port: 6379
- 注入RedisTemplate模板,编写测试方法
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {
@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);
}
}
上一篇: PHP Session 保险
下一篇: 关于猴子选大王程序的课程推荐
推荐阅读
-
【Spring框架家族】SpringBoot整合第三方
-
SpringBoot2 整合ElasticJob框架,定制化管理流程
-
ASP.NET Core应用中与第三方IoC/DI框架的整合
-
手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis
-
基于ssm(Spring+SpringMVC+MybatisPlus)框架整合Security(一)
-
使用爬虫框架htmlunit整合springboot出现的一个不兼容问题
-
springboot、mybatisplus框架整合搭建
-
spring整合cxf框架实例
-
spring整合shiro框架的实现步骤记录
-
详解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)