티스토리 뷰
pom.xml
- dependencies
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- MyBatis 3.4.1 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- MyBatis-Spring -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- Mybatis log -->
<!-- https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1 -->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4</artifactId>
<version>1.16</version>
</dependency>
<!-- Spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Spring-test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Common-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
root-context.xml
- [localhost 및 주소] : 데이터베이스 주소 입력
- [shema] : 데이터베이스 안에서 지정한 스키마 입력
- [username] : 데이터베이스에서 접속하기 위해 입력한 username
- [password] : 데이터베이스 접속 비밀번호
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://[localhost 및 주소]/[schema]?serverTimezone=UTC" />
<property name="username" value="[username]" />
<property name="password" value="[password]" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath:mappers/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
- Mapper.xml에서 사용하는 resultType 경로 설정
<property name="configLocation" value="classpath:mybatis-config.xml" />
- Mapper.xml 위치(경로) 지정
<property name="mapperLocations" value="classpath:mappers/*.xml" />
TEST CODE
- MyBatisTest.java
package com.project.controller;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/root-context.xml" })
public class MyBatisTest {
@Inject
private SqlSessionFactory sqlFactory;
@Test
public void testFactory() {
System.out.println("\n >>>>>>>>>> sqlFactory : " + sqlFactory);
}
@Test
public void testSession() throws Exception {
try (SqlSession session = sqlFactory.openSession()) {
System.out.println(" >>>>>>>>>> session : " + session + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
- MySQLConnectionTest.java
package com.project.controller;
import java.sql.Connection;
import javax.inject.Inject;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/**/root-context.xml" })
public class MySQLConnectionTest {
@Inject
private DataSource ds;
@Test
public void testConnection() throws Exception {
try (Connection con = ds.getConnection()) {
System.out.println("\n >>>>>>>>>> Connection : " + con + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
문제가 없으면 성공!
'BACK-END > SPRING' 카테고리의 다른 글
[SPRING] LOGIN MVC (1) | 2019.09.26 |
---|---|
[SPRING] MVC 게시판 (0) | 2019.09.25 |
[SPRING] IoC - SPRING CONTAINER (0) | 2019.09.25 |
[SPRING] AOP - 관점 지향 프로그래밍 (0) | 2018.05.17 |
[SPRING] FRAMEWORK (0) | 2018.05.15 |
댓글