Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 인턴 후기
- jsp
- spring
- 정보보호병 후기
- 행정지역 DB
- 인턴 지원
- react
- PyAmdecoder
- SessionAttribute
- Layered Architecture
- 3단계 지역 DB
- JSTL
- riceteacatpanda
- Database
- reversing.kr
- ㅁㅇㅂ??ㅇㅈㄷ ㅎㅇㅌ...
- Django
- 소개딩
- frontend
- 메모리 포랜식
- mysql
- EER
- 소프트웨어 개발보안 경진대회
- Forensic 절차
- DBMS
- 네이버 인턴
- restapi
- 동읍면 DB
- 방명록 만들기
- webhacking 처음
Archives
- Today
- Total
웹찢남
Spring - JDBC 본문
JDBC 프로그래밍 속 반복되는 개발 요소가 많음
-> spring framework가 이 부분을 처리해줌
DTO란?
- 계층간 데이터 교환을 위한 자바빈즈
- 계층이란 컨트롤러 뷰, 비지니스 계층, 퍼시스턴스 계층을 의미
DAO란?
- 데이터를 조회하거나 조작하는 기능을 전담
- 데이터베이스를 조작하는 기능을 전담하는 목적
Connection Pool이란?
- DB 연결은 비용이 많이 듬
- 커넥션 풀은 미리 커넥션을 여러개 맺어둠
- 커넥션이 필요하면 커넥션 풀에게 빌려서 사용 후 반납
DataSource란?
- 커넥션 풀을 관리하는 목적으로 사용되는 객체
- 커넥션을 얻어오고 반납하는 등의 작업 수행
사용법
Role.java
package kr.or.connect.daoexam.dto;
public class Role {
private int roleId;
private String description;
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Role [roleId=" + roleId + ", description=" + description + "]";
}
}
RoleDao.java
package kr.or.connect.daoexam.dao;
import static kr.or.connect.daoexam.dao.RoleDaoSqls.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.EmptySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import kr.or.connect.daoexam.dto.Role;
@Repository
public class RoleDao {
private NamedParameterJdbcTemplate jdbc;
private SimpleJdbcInsert insertAction;
private RowMapper<Role> rowMapper = BeanPropertyRowMapper.newInstance(Role.class);
public RoleDao(DataSource dataSource) {
this.jdbc = new NamedParameterJdbcTemplate(dataSource);
this.insertAction=new SimpleJdbcInsert(dataSource).withTableName("role");
}
public List<Role> selectAll(){
return jdbc.query(SELECT_ALL,EmptySqlParameterSource.INSTANCE,rowMapper);
}
public int insert(Role role) {
SqlParameterSource params = new BeanPropertySqlParameterSource(role);
return insertAction.execute(params);
}
public int update(Role role) {
SqlParameterSource params = new BeanPropertySqlParameterSource(role);
return jdbc.update(UPDATE, params);
}
public int update_one(Role role) {
SqlParameterSource params = new BeanPropertySqlParameterSource(role);
return jdbc.update(UPDATE_ONE, params);
}
public int deleteById(Integer id) {
Map<String, ?> params = Collections.singletonMap("roleId", id);
return jdbc.update(DELETE_BY_ROLE_ID, params);
}
public Role selectById(Integer id) {
try {
Map<String, ?> params = Collections.singletonMap("roleId", id);
return jdbc.queryForObject(SELECT_BY_ROLE_ID, params, rowMapper);
}catch(EmptyResultDataAccessException e) {
return null;
}
}
}
RoleDaoSqls.java
package kr.or.connect.daoexam.dao;
public class RoleDaoSqls {
public static final String SELECT_ALL = "SELECT role_id,description FROM role order by role_id";
public static final String UPDATE = "UPDATE role SET description = :description WHERE ROLE_ID = :roleId";
public static final String UPDATE_ONE = "UPDATE role SET description = :description WHERE ROLE_ID = :roleId limit 1";
public static final String SELECT_BY_ROLE_ID = "SELECT role_id, description FROM role where role_id = :roleId";
public static final String DELETE_BY_ROLE_ID = "DELETE FROM role WHERE role_id = :roleId Limit 1";
}
JDBCTest.java
package kr.or.connect.daoexam.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import kr.or.connect.daoexam.config.ApplicationConfig;
import kr.or.connect.daoexam.dao.RoleDao;
import kr.or.connect.daoexam.dto.Role;
public class JDBCTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
RoleDao roleDao = ac.getBean(RoleDao.class);
Role role = new Role();
role.setRoleId(201);
role.setDescription("GATERS");
/*int count = roleDao.insert(role);
System.out.println(count + "건 입력하였습니다.");
*/
Role resultRole = roleDao.selectById(510);
System.out.println(resultRole);
int deleteCount = roleDao.deleteById(510);
System.out.println(deleteCount + "건 삭제하였습니다.");
Role resultRole2 = roleDao.selectById(510);
System.out.println(resultRole2);
}
}
'BACK_END > Spring 공부' 카테고리의 다른 글
Spring - 레이어드 아키텍처 (0) | 2021.02.23 |
---|---|
Spring - MVC (0) | 2021.02.17 |
Spring - Spring Core (0) | 2021.02.13 |
Spring - JSTL (0) | 2021.02.03 |
Spring - EL (Expression Language) (0) | 2021.02.02 |
Comments