SQL문을 XML에 저장하여 보다 Java 코드 작성을 줄여줌
MyBatise 설치
- Help – eclipse Market에서 java orm 검색
- MyBatise와 MyBatis Generator 플러그인을 설치
pom.xml에 DBCP 라이브러리 추가
- sonatype 에서 mybatis 검색하여 Apache Maven 코드 복사
<dependency>에 추가
<!-- MyBatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- MyBatis Spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
mapping.xml 파일 생성
Project 오른쪽 마우스 클릭 – New – Other – MyBatis XML Mapper 클릭 – Next
/src/main/resources/mappings에 book-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="BookDAO">
<resultMap id="bookResult" type="book">
<id property="no" column="no"/>
<result property="title" column="title"/>
<result property="price" column="price"/>
<result property="publ" column="publ"/>
</resultMap>
<select id="getOne" parameterType="int" resultType="book">
SELECT * FROM book WHERE no=#{no}
</select>
<select id="getAll" resultType="book">
SELECT * FROM book
</select>
<insert id="insert" parameterType="book">
INSERT INTO book(title,price,publ)
VALUES(#{title}, #{price}, #{publ})
</insert>
<update id="update" parameterType="book">
UPDATE book SET
title = #{title},
price = #{price},
publ = #{publ}
WHERE no = #{no}
</update>
<delete id="delete" parameterType="int">
DELETE FROM book WHERE no = #{no}
</delete>
</mapper>
Configuration.xml 파일 생성
Project 오른쪽 마우스 클릭 – New – Other – MyBatis Genetator Configuration 클릭 – Next
/src/main/resources 폴더에 mybatis-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>
<properties resource="config/database.properties" />
<typeAliases>
<typeAlias type="krac.jj.boardprj.dto.book.Book" alias="book"></typeAlias>
</typeAliases>
<mappers>
<mapper resource="mappings/book-mapping.xml" />
</mappers>
</configuration>
root-context.xml에 SqlSession 객체 생성
<!-- Spring과 Mybatis 연동설정 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- SqlSession Template 생성 -->
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSession"></constructor-arg>
</bean>
DAOMyBatis Class 생성
@Repository
public class BookDAOMyBatis implements BookDAO{
@Autowired
private SqlSessionTemplate mybatis;
@Override
public int insert(Book vo) {
// TODO Auto-generated method stub
return mybatis.insert("BookDAO.insert",vo);
}
@Override
public int update(Book vo) {
// TODO Auto-generated method stub
return mybatis.insert("BookDAO.update",vo);
}
@Override
public int delete(Book vo) {
// TODO Auto-generated method stub
return mybatis.delete("BookDAO.delete",vo);
}
@Override
public Book getOne(Book vo) {
// TODO Auto-generated method stub
return mybatis.selectOne("BookDAO.getOne",vo);
}
@Override
public List<Book> getAll() {
// TODO Auto-generated method stub
return mybatis.selectList("BookDAO.getAll")
;
}
}