1.连接redis
1.1pom.xml配置
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
1.2application.properties配置
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
1.3项目目录
1.4User.java
package com.shuai.spring_boot_1.domain;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1797812270097265263L;
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
1.5UserDao.java
package com.shuai.spring_boot_1.dao;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import com.shuai.spring_boot_1.domain.User;
@Repository
public class UserDao {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Resource(name="stringRedisTemplate")
ValueOperations<String, String> valOpsStr;
public void save(String key,String value){
valOpsStr.set(key, value);
}
public String getByKey(String key){
String value = valOpsStr.get(key);
return value;
}
@Autowired
RedisTemplate<Object, Object> redisTemplate;
@Resource(name="redisTemplate")
ValueOperations<Object, Object> valOps;
public void save(User user){
valOps.set(user.getId(), user);
}
public User getById(String id){
User user = (User) valOps.get(id);
return user;
}
}
1.6OneController.java
package com.shuai.spring_boot_1.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shuai.spring_boot_1.dao.UserDao;
import com.shuai.spring_boot_1.domain.User;
@RestController
public class OneController {
@Autowired
UserDao userDao;
@RequestMapping("/save")
public String save(){
User user = new User();
user.setId("1");
user.setName("zhangsan");
user.setAge(20);
userDao.save(user);
userDao.save("name", "lisi");
System.out.println("保存成功");
return "ok";
}
@RequestMapping("/find")
public String find(){
User byId = userDao.getById("1");
System.out.println(byId);
String byKey = userDao.getByKey("name");
System.out.println(byKey);
return "ok";
}
}
1.7App.java
package com.shuai.spring_boot_1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
1.8运行项目
运行App.java中的main方法
1.9访问项目
http://localhost:8080/save 保存
http://localhost:8080/find 获取全部