Maven이 깔려 있다는 가정하에
1. pom.xml을 열어 dependency를 추가한다.
<!-- Mysql connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
2. *-context.xml 파일 설정
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="://ip주소:3306/db명"></property>
<property name="username" value="아이디"></property>
<property name="password" value="비밀번호"></property>
</bean>
<!-- Mybatis를 사용하는 패키지 경로를 적어준다. -->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<!-- 트랜잭션 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
datasource를 property파일로 빼서 관리하려면 빨간색 네모 부분에 프로퍼티 경로를 추가한
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>/WEB-INF/spring/dbpool.properties</value>
</property>
</bean>
경로 추가 후 datasource 내용도 바꿈
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
locations에 설정한 위치에 프로퍼티 파일 추가
dbpool.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://ip주소:3306/db명
jdbc.username= db아이디
jdbc.password= 비밀번호
DAO 서비스 구현
Member.java
- 간단하게 아이디 이름 비밀번호, getter setter 만들어 줌
package com.demo.dao;
public class Member {
String id;
String name;
String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
MemberDAO.java
package com.demo.dao;
import java.util.ArrayList;
public interface MemberDAO {
public ArrayList<Member> getMember();
}
MemberDAOImpl.java
package com.demo.dao;
import java.util.ArrayList;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class MemberDAOImpl implements MemberDAO {
@Autowired
private SqlSession sqlSession;
@Override
public ArrayList<Member> getMember() {
// TODO Auto-generated method stub
ArrayList<Member> result = new ArrayList<Member>();
MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);
result = memberMapper.getMember();
return result;
}
}
MemberMapper.java
package com.demo.dao;
import java.util.ArrayList;
public interface MemberMapper {
public ArrayList<Member> getMember();
}
MemberMapper.xml
- *-context.xml에 설정한 위치에 만들어 준다.
<mapper namespace="com.demo.dao.MemberMapper">
<!-- <cache-ref namespace=""/> -->
<resultMap type="Member" id="MemberResultMap">
<result property="id" column="id"/>
<result property="password" column="password"/>
<result property="name" column="name"/>
</resultMap>
<select id="getMember" resultMap="MemberResultMap">
select id , name , password from member
</select>
</mapper>