반응형
Spring jdbc repository sample 코드 입니다.
import java.util.List; import xxx.Category; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class CategoryRepository { @Autowired private JdbcTemplate jdbcTemplate; StringBuffer query; public List<Category> selectCategorys() { query = new StringBuffer(); query.append("SELECT id, name "); query.append(" FROM category "); query.append(" ORDER BY id"); return jdbcTemplate.query(query.toString(), new Object[] {}, new BeanPropertyRowMapper<Category>(Category.class)); } public int selectCategoryTotalCount() { return jdbcTemplate.queryForObject("SELECT count(*) AS total_count FROM category", Integer.class); } public Category selectCategory(int id) { return (Category) jdbcTemplate.queryForObject("SELECT id, name FROM category WHERE id = ?", new Object[] { id }, new BeanPropertyRowMapper<>(Category.class)); } public int insertCategory(Category category) { return jdbcTemplate.update("INSERT INTO category (id, name) VALUES(?, ?)", category.getId(), category.getName()); } public int updateCategory(Category category) { return jdbcTemplate.update("UPDATE category SET name=? WHERE id=?", category.getName(), category.getId()); } public int deleteCategory(int id) { return jdbcTemplate.update("DELETE FROM category WHERE id = ?", id); } } |
반응형
'java' 카테고리의 다른 글
jackson fasterxml custom filter (exclude, masking) (0) | 2018.07.18 |
---|---|
spring boot multi db and jndi in mybatis (0) | 2018.07.13 |
JNDI TEST (0) | 2018.07.09 |
fasterxml json camelcase (2) | 2018.06.18 |
spring boot logback.xml file rolling example 스프링부트 logback.xml 파일 롤링 예제 (0) | 2018.05.17 |
spring boot tomcat accesslog 스프링부트 탐캣 엑세스로그 (0) | 2018.05.17 |
spring boot tomcat port 스프링부트 포트 (0) | 2018.05.17 |
자바 - 리눅스에서 특정 프로세스 찾기. ps -ef command java on linux (2) | 2018.05.16 |