Search in sources :

Example 1 with Person

use of org.ko.web.domain.Person in project tutorials-java by Artister.

the class PersonService method findById.

/**
 * 通过ID查找当前对象
 * @param id
 * @return
 */
public Person findById(Long id) {
    Person person = new Person();
    jdbcTemplate.query("SELECT * FROM person p WHERE p.id = ?", (ResultSet resultSet) -> buildPerson(resultSet, person), id);
    return person;
}
Also used : ResultSet(java.sql.ResultSet) Person(org.ko.web.domain.Person)

Example 2 with Person

use of org.ko.web.domain.Person in project tutorials-java by Artister.

the class RedisConfig method redisTemplate.

@Bean
public RedisTemplate<String, Person> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Person> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new RedisObjectSerializer());
    return template;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) Person(org.ko.web.domain.Person) Bean(org.springframework.context.annotation.Bean)

Example 3 with Person

use of org.ko.web.domain.Person in project tutorials-java by Artister.

the class PersonService method findAll.

/**
 * 查询全部
 * @return
 */
public List<Person> findAll() {
    return jdbcTemplate.query("SELECT * FROM person", (ResultSet resultSet, int i) -> {
        Person person = new Person();
        buildPerson(resultSet, person);
        return person;
    });
}
Also used : ResultSet(java.sql.ResultSet) Person(org.ko.web.domain.Person)

Example 4 with Person

use of org.ko.web.domain.Person in project tutorials-java by Artister.

the class ApplicationTests method test.

@Test
public void test() throws Exception {
    // 创建三个Person,并验证Person总数
    personRepository.save(new Person(1L, "didi", (short) 30));
    personRepository.save(new Person(2L, "mama", (short) 40));
    personRepository.save(new Person(3L, "kaka", (short) 50));
    Assert.assertEquals(3, personRepository.findAll().size());
    // 删除一个Person,再验证Person总数
    Person u = personRepository.findOne(1);
    personRepository.delete(u);
    Assert.assertEquals(2, personRepository.findAll().size());
    // 删除一个Person,再验证Person总数
    u = personRepository.findByName("mama");
    personRepository.delete(u);
    Assert.assertEquals(1, personRepository.findAll().size());
}
Also used : Person(org.ko.web.domain.Person) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

Person (org.ko.web.domain.Person)4 ResultSet (java.sql.ResultSet)2 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Bean (org.springframework.context.annotation.Bean)1 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)1 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)1