首先要打开redis,然后创建springboot。
spring:
# REDIS (RedisProperties)
redis:
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
pool:
# 连接池中的最大空闲连接
max-idle: 8
# 连接池中的最小空闲连接
min-idle: 0
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接超时时间(毫秒)
timeout: 1000
# Redis数据库索引(默认为0)
database: 0
# Redis服务器连接密码(默认为空)
password: 123456
package com.example.demo2.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@PropertySource("classpath:application.yml")
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.pool.max-wait}")
private long maxWait;
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(host);
factory.setPort(port);
factory.setPassword(password);
factory.setDatabase(database);
factory.setTimeout(timeout);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxWaitMillis(maxWait);
factory.setPoolConfig(jedisPoolConfig);
return factory;
}
@Bean
public RedisTemplate redisTemplate() {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
package com.example.demo2.test;
public class User {
String uid;
String userName;
String passWord;
String name;
public User() {
}
public User(String uid, String userName, String passWord, String name) {
this.uid = uid;
this.userName = userName;
this.passWord = passWord;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id='" + uid + '\'' +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
", name='" + name + '\'' +
'}';
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWrod() {
return passWord;
}
public void setPassWrod(String passWord) {
this.passWord = passWord;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo2.test;
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserDao {
private static Jedis jedis;
public UserDao(Jedis jedis) {
this.jedis = jedis;
}
public void addUser(User user) {
//首先保存user-id
jedis.sadd("useradd", "user-" + user.getUid());
//-----添加数据----------
Map map = new HashMap();
map.put("uid", user.getUid());
map.put("userName", user.getUserName());
map.put("passWord", user.getPassWrod());
map.put("name", user.getName());
jedis.hmset("user-" + user.getUid(), map);
}
/**
* 获取单个User
*
* @return
*/
public List getById(String id) {
if (exists()) {
return jedis.hmget("user-" + id, "id", "userName", "passWord", "name");
}
return null;
}
/**
* 获取全部
43 *
44 * @return
45 */
public List listAll() {
List list = new ArrayList();
User user = null;
if (exists()) {
for (String useradd : jedis.smembers("useradd")) {
user = new User();
List lists = jedis.hmget(useradd, "id", "userName", "passWord", "name");
// User user2= (User) jedis.hmget(useradd, "id", "userName", "passWord", "name");
user.setUid(lists.get(0));
user.setUserName(lists.get(1));
user.setPassWrod(lists.get(2));
user.setName(lists.get(3));
list.add(user);
}
return list;
}
return null;
}
//删除全部
public boolean delAll() {
if (exists()) {
jedis.del("useradd");
return true;
}
return false;
}
//判断是否存在
public boolean exists() {
return jedis.exists("useradd");
}
}
package com.example.demo2;
import com.example.demo2.test.User;
import com.example.demo2.test.UserDao;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.List;
@RestController
@SpringBootApplication
public class Demo2Application {
private static Jedis jedis =null;
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public List TextController(){
jedis = new Jedis("localhost");
System.out.println("连接成功");
//查看服务是否运行
System.out.println("服务正在运行: "+jedis.ping());
UserDao user = new UserDao(jedis);
List listUser=new ArrayList();
user.delAll();
user.addUser(new User("21","ldl","123456","刘地林"));
user.addUser(new User("31","oyl","123456","欧一乐"));
user.addUser(new User("41","tyj","123456","唐玉棋"));
user.addUser(new User("51","cs","123456","陈胜"));
user.addUser(new User("61","gsq","123456","郭世棋"));
for (User user1 : user.listAll()) {
System.out.println(user1);
listUser.add(user1);
}
return listUser;
}
}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-data-redis
com.alibaba
fastjson
1.2.4
redis.clients
jedis
2.9.0
这样就可以运行了,访问网页;