티스토리 뷰
jdbcTemplate |
1. pom.xml 에다 dependency를 추가 해준다
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 | <dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.6.RELEASE</version> </dependency> </dependencies> | cs |
2. jdbcTemplate을 사용해서 db에 값 넣기(update,insert,delete)
3. RowMapper을 사용해서 db 값 가져오기(select)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public Member selectOne(String id){ String sql = "select * from member where m_id=?"; return jdbcTemplate.queryForObject(sql, rowMapper, id); } public List<Member> selectAll(){ String sql = "select * from member"; return jdbcTemplate.query(sql, rowMapper); } RowMapper<Member> rowMapper = new RowMapper<Member>() { @Override public Member mapRow(ResultSet rs, int index) throws SQLException { Member member = new Member(); member.setId(rs.getString("m_id")); member.setPw(rs.getString("m_pw")); member.setName(rs.getString("m_name")); member.setEmail(rs.getString("m_email")); member.setRegData(rs.getDate("m_reg_date")); return member; } }; | cs |
jdbcTemplate.query 와 jdbcTemplate.queryForObject 를 유심히 봐야 한다.
4. Dao를 만든 후 bean에 Dao를 추가 및 property를 추가해주면 된다.
1 2 3 | <bean class="MemberDao" id="memberdao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> | cs |
5. 테스트 해보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; public class MemberDaoTest { public static void main(String[] args) { ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); MemberDao memberDao = context.getBean("memberdao",MemberDao.class); Member member = new Member(); member.setId("test_id"); member.setPw("test_pw"); member.setName("test_name"); member.setEmail("test_email"); memberDao.insertMember(member); } } | cs |
DB 화면
NamedParameterJdbcTemplate |
1. pom.xml 에다 dependency를 추가 해준다
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 | <dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.6.RELEASE</version> </dependency> </dependencies> | cs |
2. xml 먼저 작성
dataSource -> NamedParameterJdbcTemplate bean 등록해준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost/hanbit"></property> <property name="username" value="root"></property> <property name="password" value="****"></property> </bean> <bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg name="dataSource" ref="dataSource"></constructor-arg> </bean> </beans> | cs |
3. Dao만들기
4. Dao를 만든 후 bean에 Dao를 추가 및 property를 추가해주면 된다.
1 2 3 4 | <bean class="dao.deptDao" id="deptDao"> <property name="jdbcTemplate" ref="namedParameterJdbcTemplate"></property> </bean> | cs |
5. 테스트 해보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Main { public static void main(String[] args) { ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); deptDao dao = context.getBean("deptDao",deptDao.class); Dept d = new Dept(); d.setDeptno(50); d.setDname("TEST1"); d.setDloc("TEST2"); dao.insertDept(d); } } | cs |
DB 화면
반응형
'언어 > SPRING' 카테고리의 다른 글
[SPRING] resultMap 사용 (0) | 2016.11.10 |
---|---|
[SPRING] MyBatis (0) | 2016.11.09 |
[SPRING] @postConstruct, @PreDestory 어노테이션 (0) | 2016.11.08 |
[SPRING] @Autowired, @Qualifier 어노테이션 (0) | 2016.11.08 |
[SPRING] @Component 어노테이션 (0) | 2016.11.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 생성
- jsp
- spring
- 자바
- Controller
- UTF-8
- 안드로이드
- Default
- 연결
- js
- 종류
- Delete
- 사용
- Android
- 어노테이션
- jdbc
- Class
- nsis
- mybatis
- 클래스
- 라이브러리
- synchronized
- 음식
- iNT
- java
- 하기
- Server
- 스프링
- Create
- mysql
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
글 보관함