SpringBoot整合其他技术
Spring整合Mybatis
添加MyBatis的起步依赖
<!--mybatis起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
添加数据库驱动坐标
<!-- MySQL连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
添加数据库连接信息和mybatis的信息
#数据库连接信息
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123
#spring集成Mybatis环境 #pojo别名扫描包
mybatis.type-aliases-package=com.leewle.domain
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
整合Junit
添加Junit
的起步依赖
<!--SpringBoot集成junit测试的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMyBatis.class)
public class MybatisTest {
@Autowired
private UserMapper userMapper;
@Test
public void test(){
List<User> users = userMapper.queryUserList();
for(User user : users){
System.out.println(user);
}
}
}
整合Spring Data Jpa
添加Spring Data Jpa
的起步依赖
<!-- springBoot JPA的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
添加数据库驱动依赖
<!-- MySQL连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置数据库和jpa的相关属性
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123
#JPA Configuration:
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
整合Redis
添加redis
的起步依赖
<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置redis
的连接信息
#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
注入RedisTemplate
测试redis
操作
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootJpaApplication.class)
public class RedisTest {
@Autowired
private UserRepository userRepository;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
public void test() throws JsonProcessingException {
//1.从redis中会的数据(数据的形式json字符串)
String userListJson = redisTemplate.boundValueOps("user.findAll").get();
//2.判断redis中是否存在数据
if(null == userListJson){
//3.不存在数据 从数据库查询
List<User> users = userRepository.findAll();
//4.将查询出的数据存储到redis缓存中
//4.1 将list集合转换成json格式的字符串
ObjectMapper objectMapper = new ObjectMapper();
userListJson = objectMapper.writeValueAsString(users);
redisTemplate.boundValueOps("user.findAll").set(userListJson);
System.out.println("=============从数据库中查询=============");
}else {
System.out.println("=============从redis缓存中查询=============");
}
//4.将数据在控制台打印
System.out.println(userListJson);
}
}