필요한 dependency를 pom.xml에 추가
(다른 건 다 있음)
Mybatis 3.5.9(Mybatis Module)
MyBatis - spring(연동 모듈) - 2.0.7 version
spring JDBC 5.3.23 version
MySQL connector j
pom.xml에 추가
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
SqlMapConfig.xml 생성??
① mybatis-config.xml파일 생성
src/main/resources폴더에 !
<?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>
<!--
property는 하나만 설정할꺼예요!
외부파일을 이용해서 데이터베이스 접속정보를 가져올꺼예요!
외부파일을 하나 만들꺼구요. => driver.properties 파일을 만들꺼예요!
-->
<properties resource="./driver.properties"/>
<!--
MyBatis에 대한 기본 세팅정보가 나와요!
MyBatis가 동작하는 기본적인 방식에 대한 세팅정보가 있어요!
-->
<settings>
<!-- Data가 없을경우 NULL로 처리할 꺼예요! -->
<setting name="jdbcTypeForNull" value="NULL"/>
<!-- Log4J와 Mybatis를 연동해서 Query문과 실행결과를 log로 출력해요! -->
<setting name="logImpl" value="LOG4J2"/>
</settings>
<!-- 별명지정하는게 나와요! 타이핑 줄이기위해서 사용해요! -->
<typeAliases>
<!--
<typeAlias type="member.vo.Member" alias="Member"/>
<typeAlias type="board.vo.Board" alias="Board"/>
-->
</typeAliases>
</configuration>
② DataSource 설정
: 연결정보가 있어야 함 - 별도의 property파일로 등록
src/main/resources/config생성/database.properties생성
db.driverClassName=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/spring?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false&autoReconnect=true
db.username=root
db.password=test1234
context 선택 후 생성
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- src/main/resources 경로는 classpath에 포함됩니다.
location경로의 파일을 property파일로 불러와 사용하겠다는 설정입니다. -->
<context:property-placeholder
location="classpath:config/database.properties" />
<!-- dataSource를 Bean으로 등록합니다.
dataSource는 connection에 필요한 정보가지고 있습니다. -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<!-- sqlSessionFactory를 Bean으로 등록합니다.
SqlSessionFactory는 SqlSession을 만들기 위한 정보들을 가지고 있습니다.
mapper를 생성하면 list에 추가해야 합니다. -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:mybatis-config.xml" />
<property name="mapperLocations">
<list>
<value>classpath:mappers/BookMapper.xml</value>
</list>
</list>
</property>
</bean>
<!-- sqlSession를 Bean으로 등록합니다. 기존의 sqlSession은 Thread-Sage하지 않습니다.
그래서 Bean으로 등록해서 사용하면 Transaction처리에 문제가 발생할 수 있지만
여기서는 Thread-Safe한 SqlSessionTemplate 클래스를 사용합니다. -->
<bean id="sqlSession"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/spring/root-context.xml</param-value> -->
<param-value>/WEB-INF/spring/*-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Log4j 설정 -->
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:/log4j2.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener
</listener-class>
</listener>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener
</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 추가 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
BookMapper.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="myBook">
<select id="countBooks" resultType="int">
select count(*) from book
</select>
<!-- resultMap의 일반적인 사용법 -->
<resultMap id="result"
type="my.spring.springweb.sample12.vo.Book">
<result property="bookIsbn" column="bisbn" jdbcType="VARCHAR"
javaType="String" />
<result property="bookTitle" column="btitle" jdbcType="VARCHAR"
javaType="String" />
<result property="bookAuthor" column="bauthor" />
<result property="bookPrice" column="bprice" />
</resultMap>
<select id="selectAll" resultMap="result">
<![CDATA[
SELECT bisbn, btitle, bauthor, bprice
FROM book
]]>
</select>
<select id="selectBookByKeyword" parameterType="String"
resultMap="result">
<![CDATA[
SELECT bisbn, btitle, bauthor, bprice
FROM book
WHERE btitle LIKE CONCAT('%', #{Keyword}, '%')
]]>
</select>
</mapper>