본문 바로가기

기존카테고리/Mybatis

Mybatis 설정( pom.xml, root-context.xml)



pom.xml


<!-- Mybatis -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.2.8</version>

</dependency>

<!-- Mybatis-spring -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.2.2</version>

</dependency>


<!-- spring-jdbc -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${org.springframework-version}</version>

</dependency>


<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${org.springframework-version}</version>

</dependency>



1) servlet-context.xml에 dataSource 를 추가함.

보통은 root-context.xml에 작성함.



  <beans:bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<beans:property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />

<beans:property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />

<beans:property name="username" value="scott"/>

<beans:property name="password" value="1234"/>

</beans:bean>

<beans:bean name="tmplate" class="org.springframework.jdbc.core.JdbcTemplate">

<beans:property name="dataSource" ref="dataSource" />

</beans:bean>


  <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<beans:property name="dataSource" ref="dataSource" />

<beans:property name="mapperLocations" value="classpath:com/mybatis/mapper/*.xml" />

</beans:bean>

<beans:bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

<beans:constructor-arg ref="sqlSessionFactory" />

</beans:bean>



2) root-context.xml 에 작성할 시


아래는 SimpleDriverDataSource 소스로 바꾸어서 작성했다.

<?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.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">

<property name="driverClass" value="oracle.jdbc.OracleDriver" />

<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />

<property name="username" value="hr" />

<property name="password" value="1234" />

</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mapperLocations" value="classpath:com/spring/hr/mappers/*.xml" />

</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />

</bean>

</beans>

















'기존카테고리 > Mybatis' 카테고리의 다른 글

ResultMap 을 이용한 mybatis  (1) 2017.07.25
기본 XML 설정과 사용예  (0) 2017.07.25