파일 구조 >
; Referenced Library 에 해당 라이브러리
; Mybatis 관련 jar 파일
(1) TestMain.java
package test.app;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.dao.MembersDao;
import test.vo.MembersVo;
public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("test/app/app.xml");
MembersDao dao = (MembersDao)context.getBean("membersDao");
int n = dao.insert(new MembersVo("my1","122","m","00",null));
if(n>0){
System.out.println("추가.성공!!");
}
n = dao.delete("my1");
if(n>0){
System.out.println("삭제성공!!");
}
MembersVo vo = dao.getInfo("my");
if(vo!=null){
System.out.println("아이디:" + vo.getId() );
System.out.println("비밀번호:" + vo.getPwd() );
System.out.println("이메일:" + vo.getEmail() );
System.out.println("전화:" + vo.getPhone() );
}else{
System.out.println("검색된 아이디가 없어요!!");
}
System.out.println("---------------------------------------");
List<MembersVo> list = dao.getList();
for( MembersVo vo1: list){
System.out.println("아이디:" + vo1.getId() );
System.out.println("비밀번호:" + vo1.getPwd() );
System.out.println("이메일:" + vo1.getEmail() );
System.out.println("전화:" + vo1.getPhone() );
System.out.println("=============");
}
}
}
2. SqlSessionFactory설정(dataSource주입,mapper파일에 대한 정보)
3. SqlSessionTemplate설정(SqlSessionFactory주입)
4. Dao설정(sqlSessionTemplate주입)
(2) app.xml
;
<?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-3.1.xsd">
<!-- 1. DataSource설정(DB접속 정보) -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
</bean>
<!-- 2. SqlSessionFactory설정(dataSource주입,mapper파일에 대한 정보) -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- dataSource설정 -->
<property name="dataSource" ref="dataSource"/>
<!--
SQLMapper파일 설정 : 클래스패스위치의 test/mybatis 경로의 모든xml파일을 읽어와라.!
-->
<property name="mapperLocations" value="classpath:test/mybatis/ *.xml"/>
</bean>
<!-- 3. SqlSessionTemplate설정(SqlSessionFactory주입) -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 생성자 0번째 인자에 sqlSessionFactory를 넣어라 -->
<constructor-arg ref="sqlSessionFactory" index="0"/>
</bean>
<!-- 4. Dao설정(sqlSessionTemplate주입) -->
<bean id="membersDao" class="test.dao.MembersDao">
<property name="sqlSession" ref="sqlSession" />
</bean>
</beans>
( SqlSession 오픈, 종료에 관련된 작업을 관리해줌 )
(3) MembersDao.java
;
package test.dao;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import test.vo.MembersVo;
public class MembersDao {
private SqlSessionTemplate sqlSession;
//스프링을 통해 주입받아야 하므로 setter메소드가 있어야 함.
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public int insert(MembersVo vo){
int n = sqlSession.insert("insert", vo);
return n;
}
public int delete(String id){
return sqlSession.delete("delete", id);
}
public MembersVo getInfo(String id){
return (MembersVo)sqlSession.selectOne("getInfo", id);
}
public List<MembersVo> getList(){
return (List<MembersVo>)sqlSession.selectList("getList");
}
}
;
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="orm.mybatis.MembersMapper">
<insert id="insert" parameterType="test.vo.MembersVo">
insert into Members values ( #{ id }, #{ pwd }, #{ email }, #{ phone }, sysdate )
</insert>
<select id="getList" resultType="test.vo.MembersVo">
select * from members
</select>
<delete id="delete" parameterType="String">
delete from members where id=#{value}
</delete>
<select id="getInfo" resultType="test.vo.MembersVo" parameterType="String">
select * from members where id=#{value}
</select>
</mapper>
(4) MembersVo.java
package test.vo;
import java.sql.Timestamp;
public class MembersVo {
private String id;
private String pwd;
private String email;
private String phone;
private Timestamp regdate;
public MembersVo(){}
public MembersVo(String id, String pwd, String email, String phone, Timestamp regdate) {
super();
this.id = id;
this.pwd = pwd;
this.email = email;
this.phone = phone;
this.regdate = regdate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Timestamp getRegdate() {
return regdate;
}
public void setRegdate(Timestamp regdate) {
this.regdate = regdate;
}
}
'WEB > Spring3.0' 카테고리의 다른 글
SPRING3.0 #10( MVC ) (0) | 2013.01.27 |
---|---|
SPRING3.0 #09( MVC ) (0) | 2013.01.27 |
SPRING3.0 #07( jdbc ) (0) | 2013.01.27 |
SPRING3.0 #06( annotation ) (0) | 2013.01.27 |
SPRING3.0 #05( init-method, destroy-method, scope ) (0) | 2013.01.27 |