1. pom.xml 에 mybatis 라이브러리를 추가한다. ( 추가 되어있다면 패스 )
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis</artifactid>
<version>3.2.2</version>
</dependency>
<dependency>
<groupid>org.mybatis</groupid>
<artifactid>mybatis-spring</artifactid>
<version>1.2.0</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-jdbc</artifactid>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupid>commons-dbcp</groupid>
<artifactid>commons-dbcp</artifactid>
<version>1.4</version>
</dependency>
2. mysql 라이브러리 추가 ( 추가 되어있다면 패스 )
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
3. web.xml 설정 확인 ( 추가 되어있다면 패스 )
다음과 같은 코드가 있어야 한다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring/context-*.xml</param-value>
</context-param>
4. mysql xml 설정
src/java/resource 폴더 밑에 config > spring 폴더를 만들고 context-datasource.xml 파일을 만들고 코드를 추가한다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://주소:포트/데이터베이스명"/>
<property name="username" value="아이디"/>
<property name="password" value="비밀번호"/>
</bean>
</beans>
5. mybatis 와 spring 연동
5-1. config > spring 폴더에 context-mapper.xml 파일을 만들고 코드를 추가한다.
<?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.xsd">
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:/mapper/**/*_SQL.xml" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSession"/>
</bean>
</beans>
0 개의 댓글:
댓글 쓰기