在 bind 127.0.0.1 前加上 # 變註解就行了,也就是不限制那個 IP 都可以連進來。
protected-mode 改為 no 取消保護模式。
- build.gradle
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.springframework.boot:spring-boot-starter-web')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
}
- JedisConfig.java
@Configuration
public class JedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
jedisConFactory.setHostName("192.168.70.233");
jedisConFactory.setPort(6379);
return jedisConFactory;
}
}
- Student.java
@RedisHash("Student")
@Data
public class Student implements Serializable {
public enum Gender {
MALE, FEMALE
}
private String id;
private String name;
private Gender gender;
private int grade;
public Student() { }
public Student(String id, String name, Gender gender, int grade) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
}
}
- StudentRepository.java
@Repository public interface StudentRepository extends CrudRepository<Student, String> { }
- DemoTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoTest {
@Autowired
private StudentRepository studentRepository;
@Test
public void test() {
Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
studentRepository.save(student);
Student retrievedStudent = studentRepository.findById("Eng2015001").get();
System.out.println(retrievedStudent.toString());
}
}
簡單的單元測試程式,將 id 為 Eng2015001 的 Student 物件放入 Redis,接著我們可以用「Redis Desktop Manager」這個小工具連到 Redis 後看到內容,如下。
沒有留言:
張貼留言