스프링 07. MyBatis 사용하기
- 참조 :
- Java ORM 설치
- Help -> eclipse Marketplace
- java orm 검색
- Java ORM Plugin for Eclipse 1.0 설치
- Java ORM Plugin for Eclipse 1.0 이 없는 경우
- https://sourceforge.net/projects/java-orm-plugin/ 에서 zip파일 다운로드
- \eclipse\dropins 폴더에 압축해제
- Eclipse 재실행
- pom.xml에 DBCP 라이브러리 추가
- <dependency> 코드 찾기 : http://search.maven.org 접속하여 mybatis 검색
123456789101112<!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.5</version></dependency><!-- Mybatis Spring--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.5</version></dependency>
- <dependency> 코드 찾기 : http://search.maven.org 접속하여 mybatis 검색
- book-mapping.xml 파일 생성
- 프로젝트에서 마우스 오른쪽 버튼 클릭 -> New -> Other -> mybatis 검색후 book-mapping.xml 파일 생성(/src/main/resources/mappings 폴더에서는 생성되지 않음) 후 mappings 폴더로 이동
1234567891011121314151617181920212223242526272829303132333435363738394041<?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="pric" column="pric" /><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 (no, title, pric, publ)VALUES((select max(no)+1 from book),#{title},#{pric},#{publ})</insert><update id="update" parameterType="book">UPDATE bookSETtitle = #{title},pric = #{pric},publ = #{publ}WHEREno=#{no}</update><delete id="delete" parameterType="int">DELETE FROM book WHERE no = #{no}</delete></mapper>
- 프로젝트에서 마우스 오른쪽 버튼 클릭 -> New -> Other -> mybatis 검색후 book-mapping.xml 파일 생성(/src/main/resources/mappings 폴더에서는 생성되지 않음) 후 mappings 폴더로 이동
- Configuration.xml 파일 생성
- 프로젝트에서 마우스 오른쪽 버튼 클릭 -> New -> Other -> mybatis 검색후 mybatis-config.xml 파일 생성(/src/main/resources 폴더에서는 생성되지 않음) 후 resources 폴더로 이동
12345678910111213<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><properties resource="config/database.properties" /><typeAliases><typeAlias type="krco.moak.bookapp.dto.book.Book" alias="book"></typeAlias></typeAliases><mappers><mapper resource="mappings/book-mapping.xml" /></mappers></configuration>
- 프로젝트에서 마우스 오른쪽 버튼 클릭 -> New -> Other -> mybatis 검색후 mybatis-config.xml 파일 생성(/src/main/resources 폴더에서는 생성되지 않음) 후 resources 폴더로 이동
- SqlSession 객체 생성 : root-context.xml 에 추가
12345678910<!-- Spring과 Mybatis 연동 설정 --><bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="configLocation" value="classpath:mybatis-config.xml" /></bean><!-- SqlSessionTemplate 생성 --><bean class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg ref="sqlSession"></constructor-arg></bean> - BookDAOMyBatis 클래스 생성
1234567891011121314151617181920212223242526272829303132333435363738394041package krco.moak.bookapp.dao.book;import java.util.List;import org.mybatis.spring.SqlSessionTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import krco.moak.bookapp.dto.book.Book;@Repositorypublic class BookDAOMyBatis implements BookDAO{@Autowiredprivate SqlSessionTemplate mybatis;@Overridepublic int insert(Book vo) {return mybatis.insert("BookDAO.insert", vo);}@Overridepublic int update(Book vo) {return mybatis.update("BookDAO.update", vo);}@Overridepublic int delete(Book vo) {return mybatis.delete("BookDAO.delete", vo);}@Overridepublic Book getOne(Book vo) {return mybatis.selectOne("BookDAO.getOne", vo);}@Overridepublic List<Book> getAll() {return mybatis.selectList("BookDAO.getAll");}}