TIL_Book Project #3 마이바티스 문서 작성

ednadev·2020년 8월 17일
0

DB 서버 정보

jdbc.mysql.driver=com.mysql.cj.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://127.0.0.1:3306/book?characterEncoding=UTF-8&serverTimezone=UTC
jdbc.mysql.username=root
jdbc.mysql.password=1234

Mapper 작성

<?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="BookMapper">
    <insert id="insertBook" parameterType="book">
 	INSERT 
 	INTO book(book_img, book_title, book_author, book_publish, book_date, book_desc, book_main_category, book_detail_category)
 	VALUES(#{bookImg}, #{bookTitle}, #{bookAuthor}, #{bookPublish}, 
 		#{bookDate}, #{bookDesc}, #{bookMainCategory}, #{bookDetailCategory})
    </insert>
    <update id="updateBook" parameterType="book">
 	UPDATE book
 	SET 
 	    book_img=#{bookImg}, book_title=#{bookTitle}, book_author=#{bookAuthor}, book_publish=#{bookPublish}, 
 	    book_date=#{bookDate}, book_desc=#{bookDesc}, 
 	    book_main_category=#{bookMainCategory}, book_detail_category=#{bookDetailCategory}
 	WHERE book_code=#{bookCode}
    </update>
    <delete id="deleteBook" parameterType="int">
 	DELETE FROM book
 	WHERE book_code=#{value}
    </delete>
 	
    <resultMap type="book" id="bookRM">
 	<result property="bookCode" column="book_code"/>
 	<result property="bookImg" column="book_img"/>
 	<result property="bookTitle" column="book_title"/>
 	<result property="bookAuthor" column="book_author"/>
 	<result property="bookPublish" column="book_publish"/>
 	<result property="bookDate" column="book_date"/>
 	<result property="bookDesc" column="book_desc"/>
 	<result property="bookMainCategory" column="book_main_category"/>
 	<result property="bookDetailCategory" column="book_detail_category"/>
    </resultMap>
    <sql id="select-book">
 	SELECT * FROM book
    </sql>
    <select id="selectAllBook" resultType="bookRM">
 	<include refid="select-book"/>
    </select>
    <select id="selectBook" parameterType="int" resultType="bookRM">
 	<include refid="select-book"/>
 	WHERE book_code=#{value}
    </select>
 </mapper>

나머지 mapper 문서도 CRUD만 작성

SqlMapConfig.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/dbconn.properties"/>
 	
    <typeAliases>
        <typeAlias type="com.ednadev.book.domain.Book" alias="book"/>
 	<typeAlias type="com.ednadev.book.domain.BookList" alias="bookList"/>
 	<typeAlias type="com.ednadev.book.domain.BookUser" alias="bookUser"/>
 	<typeAlias type="com.ednadev.book.domain.IdeaNote" alias="ideaNote"/>
 	<typeAlias type="com.ednadev.book.domain.ReadNote" alias="readNote"/>
 	<typeAlias type="com.ednadev.book.domain.ReadPlan" alias="readPlan"/>
    </typeAliases>
 	
    <environments default="develop">
 	<environment id="develop">
 	    <transactionManager type="JDBC"/>
 	    <dataSource type="UNPOOLED">
 		<property name="driver" value="${jdbc.mysql.driver}"/>
 		<property name="url" value="${jdbc.mysql.url}"/>
 		<property name="username" value="${jdbc.mysql.username}"/>
 		<property name="password" value="${jdbc.mysql.password}"/>
 	    </dataSource>
 	</environment>
    </environments>
 	
    <mappers>
 	<mapper resource="mapper/booklist-mapping.xml"/>
 	<mapper resource="mapper/book-mapping.xml"/>
 	<mapper resource="mapper/bookuser-mapping.xml"/>
 	<mapper resource="mapper/ideanote-mapping.xml"/>
 	<mapper resource="mapper/readnote-mapping.xml"/>
 	<mapper resource="mapper/readplan-mapping.xml"/>
    </mappers>
</configuration>

SqlMapConfig.xml에서 Wiring 하는 것으로 마이바티스에서 꼭 작성해야 하는 문서 작성 끝!

관심 있을 만한 포스트

0개의 댓글