目录
1.属性配置文件(application.properties):
2.application.properties中加入redis相关配置
一、使用idea创建spring boot项目:
https://blog.csdn.net/qq_34205356/article/details/81098354
二、spring boot连接MySQL数据库:
1.属性配置文件(application.properties):
##端口号
server.port=8088
#mysql
spring.datasource.url=jdbc:jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
##控制台打印sql
spring.jpa.show-sql=true
2.pom.xml 配置maven依赖
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Spring Boot JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
三.spring boot集成JPA组件:
1.pom.xml 配置maven依赖
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.entity映射实体类
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @Auther: yaohongan
* @Date: 2019/8/28 09:53
* @Description:
*/
//使用JPA注解配置映射关系
@Entity//告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "user") //@Table来指定和哪个数据表对应;如果省略默认表名就是user;
public class UserEntity {
@Id //这是一个主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键
private Integer id;
@Column(name = "username") //这是和数据表对应的一个列
private String username;
@Column(name = "password") //这是和数据表对应的一个列
private String password;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
3.dao接口层
import com.qt.intelliteach5g.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @Auther: yaohongan
* @Date: 2019/8/28 09:59
* @Description:
*/
public interface UserDao extends JpaRepository<UserEntity,Integer> {
}
4.service层
import com.qt.intelliteach5g.dao.UserDao;
import com.qt.intelliteach5g.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Auther: yaohongan
* @Date: 2019/8/28 10:02
* @Description:
*/
@Service
public class UserService {
@Autowired
private UserDao userDao;
//查询数据
public Map<String,Object> userList() {
Map<String, Object> resultMap = new HashMap<String, Object>();
List<UserEntity> userEntityList = userDao.findAll();
if(userEntityList==null){
resultMap.put("ret", "-1");
resultMap.put("msg", "数据不存在");
return resultMap;
}
resultMap.put("ret", "0");
resultMap.put("msg", "SUCCESS");
resultMap.put("body", userEntityList);// 数据结果
return resultMap;
}
}
5.controller层
import com.qt.intelliteach5g.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @Auther: yaohongan
* @Date: 2019/8/28 10:08
* @Description:
*/
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
//查询数据
@RequestMapping(value = "/userList", produces = "application/json;charset=utf-8")
@ResponseBody
public Map<String,Object> userList() {
Map<String, Object> resultMap = userService.userList();
return resultMap;
}
}
三、spring boot集成redis
1.添加pom依赖
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2.application.properties中加入redis相关配置
#Redis
# Redis地址
spring.redis.host=10.111.96.26
# Redis端口号
spring.redis.port=9300
# Redis库
spring.redis.database=0
# Redis密码
spring.redis.password=hW9xogX5~
# 数据库连接超时时间
spring.redis.duration=5000
# 最大空闲连接数
spring.redis.jedis.pool.max-idle=10
# 最小空闲连接数
spring.redis.jedis.pool.min-idle=8
# 等待可用连接的最大时间,负数为不限制
spring.redis.jedis.pool.max-wait=30000
# 最大活跃连接数,负数为不限制
spring.redis.jedis.pool.max-active=1000
3.Redis工具类
package com.qt.intelliteach5g.util;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
/**
*
* @ClassName: RedisService
* @Description: TODO
* @author
* @date 2019年5月30日
*
*/
@Component
public class RedisService {
Logger logger = LoggerFactory.getLogger(RedisService.class);
@Autowired
private RedisTemplate redisTemplate;
/**
*
* @Title: set
* @Description: 写入缓存
* @param @param key
* @param @param value
* @param @return
* @return boolean
* @throws
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
logger.error(e.getMessage());
}
return result;
}
/**
*
* @Title: set
* @Description: 写入缓存设置时效时间
* @param @param key
* @param @param value
* @param @param expireTime
* @param @return
* @return boolean
* @throws
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
logger.error(e.getMessage());
}
return result;
}
/**
*
* @Title: set
* @Description: 设置时间为int
* @param @param key
* @param @param exp
* @param @param o
* @param @return
* @return boolean
* @throws
*/
public boolean set(String key, int exp, Object o) {
logger.debug("进入SET方法");
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, o);
redisTemplate.expire(key, exp, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
logger.error(e.getMessage());
}
return result;
}
/**
*
* @Title: remove
* @Description: 批量删除对应的value
* @param @param keys
* @return void
* @throws
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
*
* @Title: removePattern
* @Description: 批量删除key
* @param @param pattern
* @return void
* @throws
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0) {
redisTemplate.delete(keys);
}
}
/**
*
* @Title: remove
* @Description: 删除对应的value
* @param @param key
* @return void
* @throws
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
*
* @Title: exists
* @Description: 判断缓存中是否有对应的value
* @param @param key
* @param @return
* @return boolean
* @throws
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
*
* @Title: get
* @Description: 读取缓存
* @param @param key
* @param @return
* @return Object
* @throws
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
*
* @Title: hmSet
* @Description: 哈希 添加
* @param @param key
* @param @param hashKey
* @param @param value
* @return void
* @throws
*/
public void hmSet(String key, Object hashKey, Object value) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key, hashKey, value);
}
/**
*
* @Title: hmGet
* @Description: 哈希获取数据
* @param @param key
* @param @param hashKey
* @param @return
* @return Object
* @throws
*/
public Object hmGet(String key, Object hashKey) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key, hashKey);
}
/**
*
* @Title: lPush
* @Description: 列表添加
* @param @param k
* @param @param v
* @return void
* @throws
*/
public void lPush(String k, Object v) {
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k, v);
}
/**
*
* @Title: lRange
* @Description: 列表获取
* @param @param k
* @param @param l
* @param @param l1
* @param @return
* @return List<Object>
* @throws
*/
public List<Object> lRange(String k, long l, long l1) {
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k, l, l1);
}
/**
*
* @Title: add
* @Description: 集合添加
* @param @param key
* @param @param value
* @return void
* @throws
*/
public void add(String key, Object value) {
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key, value);
}
/**
*
* @Title: setMembers
* @Description: 集合获取
* @param @param key
* @param @return
* @return Set<Object>
* @throws
*/
public Set<Object> setMembers(String key) {
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
/**
*
* @Title: zAdd
* @Description: 有序集合添加
* @param @param key
* @param @param value
* @param @param scoure
* @return void
* @throws
*/
public void zAdd(String key, Object value, double scoure) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key, value, scoure);
}
/**
*
* @Title: rangeByScore
* @Description: 有序集合获取
* @param @param key
* @param @param scoure
* @param @param scoure1
* @param @return
* @return Set<Object>
* @throws
*/
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
}
4.测试redis
import com.qt.intelliteach5gsso.util.RedisService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Intelliteach5gssoApplicationTests {
@Autowired
RedisService redisService;
@Test
public void contextLoads() {
redisService.set("key1",456);
System.out.println(redisService.get("key1"));
}
}