<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>x.x.x</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>x.x.x</version>
</dependency>
business-layer.xml(root-context.xml) 설정
<!--생략-->
<!-- DataSource -->
<context:property-placeholder location="classpath:datasource.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Spring과 MyBATIS 연동 설정 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sql-map-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<!--생략-->
sql-map-config.xml 설정
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.school.service.student.studentVO" alias="student"/>
</typeAliases>
<mappers>
<mapper resource="mappings/student-mapping.xml"/>
</mappers>
</configuration>
<typeAliases>
을 이용해 객체 별칭을 정하여 사용할 수 있다.<mappers>
을 통해 mapping 정보가 담긴 xml파일을 알려준다.mappings/student-mapping.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDAO">
<resultMap id="student" type="student">
<id property="seq" column="SEQ" />
<result property="name" column="NAME" />
<result property="sex" column="SEX" />
<result property="role" column="ROLE" />
</resultMap>
<insert id="insertStudent" >
insert into student(seq, name, sex, role) values(#{seq}, #{name}, #{sex}, #{role})
</insert>
<select id="getStudent" resultMap="student">
<![CDATA[
select * from student where id = #{seq}
]]>
</select>
<select id="getStudentList" resultMap="student">
select * from student order by seq desc
</select>
</mapper>
<mapper id="">, <insert id="">
id를 통해 mapper의 sql문을 호출할 수 있다.<insert>, <update>, <delete>, <select>
)<, > ,&
을 사용한 sql문을 사용하려면 <![CDATA[ ]]>
로 감싸주면 된다.#{}
안에 적어주면된다. (JDBC의 ? 의 역할이다. )StudentDAO.java
@Repository
public class StudentDAOMybatis implements StudentDAO{
@Autowired
private SqlSessionTemplate sqlSession;
// 학생 정보 등록
public void insertStudent(StudentVO vo) {
sqlSession.insert("StudentDAO.insertStudent",vo);
}
// 학생 상세 조회
public StudentVO getStudent(StudentVO vo) {
return (StudentVO) sqlSession.selectOne("StudentDAO.getStudent",vo);
}
// 학생 목록 검색
public List<StudentVO> getStudentList(StudentVO vo) {
return sqlSession.selectList("StudentDAO.getStudentList", vo);
}
}
business-layer.xml(root-context.xml) 설정
<!-- DataSource -->
<context:property-placeholder
location="classpath:datasource.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Spring과 MyBATIS 연동 설정 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation"
value="classpath:sql-map-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
<!--위에까지는 일치한다.-->
<bean id="StudentDAO"
class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.school.service.student.studentDAO" />
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>
StudentDAO.java
public interface StudentDAO {
// 학생 등록
void insertStudent(studentVO vo);
// 학생 상세 조회
studentVO getStudent(studentVO vo);
// 학생 목록 검색
List<studentVO> getStudentList(studentVO vo);
}
sql-map-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.school.service.student.studentVO" alias="student"/>
</typeAliases>
<mappers>
<mapper resource="mappings/student-mapping(mapper).xml"/>
</mappers>
</configuration>
mappings/student-mapping(mapper).xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudentDAO">
<resultMap id="student" type="student">
<id property="seq" column="SEQ" />
<result property="name" column="NAME" />
<result property="sex" column="SEX" />
<result property="role" column="ROLE" />
</resultMap>
<insert id="insertStudent" >
insert into student(seq, name, sex, role) values(#{seq}, #{name}, #{sex}, #{role})
</insert>
<select id="getStudent" resultMap="student">
<![CDATA[
select * from student where id = #{seq}
]]>
</select>
<select id="getStudentList" resultMap="student">
select * from student order by seq desc
</select>
</mapper>
StudentServiceImpl.java
<!--생략-->
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDAO studentDAO;
public void insertStudent(StudentVO vo) {
studentDAO.insertStudent(vo);
}
public StudentVO getStudent(StudentVO vo) {
return studentDAO.getStudent(vo);
}
public List<StudentVO> getStudentList(StudentVO vo) {
return studentDAO.getStudentList(vo);
}
}
- DAO interface의 구현체가 여러 개 필요할 때 더 효율적인 방법일 것 같다.
- DAO interface를 직접 이용한다는 점이 유지 보수 측면에서 효율적이지 않지만 DAO 구현체가 1개보다 많이 필요하지 않은 로직에서는 좋은 방법일 것 같다.
- Java 소스 내 MyBatis에 의존적이지 않다.
Reference: