Spring MVCとMyBatisを連携させるための設定手順と各設定ファイルの役割について解説します。
1. 統合の概要
この統合プロセスは、アプリケーションの各層(Dao、Service、Controller)で必要なSpringの設定ファイルと、Webアプリケーションのエントリーポイントとなるweb.xmlの構成によって実現されます。
1.1. Dao層の設定
mybatis/SqlMapConfig.xml: MyBatisのグローバル設定ファイル。この統合においては、空のXMLファイルで十分ですが、DOCTYPE宣言は必要です。applicationContext-dao.xml:- データベース接続プール(
DataSource)の設定。 SqlSessionFactoryビーンの設定。SpringとMyBatisの連携ライブラリを使用します。- MyBatisのMapperインターフェースをスキャンする
MapperScannerConfigurerの設定。
- データベース接続プール(
1.2. Service層の設定
applicationContext-service.xml: Service層のコンポーネント(@Serviceアノテーションが付与されたクラス)をスキャンするための設定。applicationContext-transaction.xml: トランザクション管理の設定。
1.3. Controller層(Web層)の設定
springmvc.xml: Spring MVC関連の設定。- Controllerコンポーネント(
@Controllerアノテーションが付与されたクラス)をスキャンするための設定。 - MVCアノテーションの有効化(
mvc:annotation-driven)。 - ビューリゾルバの設定。
- Controllerコンポーネント(
web.xml:- Springのフロントコントローラ(
DispatcherServlet)の設定。 contextConfigLocationパラメータによるSpring設定ファイルのロード。
- Springのフロントコントローラ(
2. 各設定ファイルの解説
2.1. mybatis/SqlMapConfig.xml
MyBatisのコア設定ファイルです。この統合では、主にSqlSessionFactoryBeanが参照する設定ファイルとして機能します。
<?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>
<!-- MyBatis のグローバル設定 -->
</configuration>
2.2. applicationContext-dao.xml
データベース接続とMyBatisのMapperインターフェースの管理を行います。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- データベース接続情報プロパティファイルの読み込み -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- データベース接続プール -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!-- SqlSessionFactoryBean: MyBatisとSpringの統合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- MyBatis グローバル設定ファイルのパス -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>
<!-- Mapperインターフェースのスキャナ -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- Mapperインターフェースが存在するパッケージを指定 -->
<property name="basePackage" value="cn.itcast.springmvc.mapper"/>
</bean>
</beans>
db.properties
データベース接続に必要な情報を記述します。
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
2.3. applicationContext-service.xml
Service層のコンポーネントをSpringの管理下に置きます。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Service層のコンポーネントスキャン -->
<context:component-scan base-package="cn.itcast.springmvc.service"/>
</beans>
2.4. applicationContext-transaction.xml
Service層のメソッドに対するトランザクション管理を設定します。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- トランザクションマネージャー -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- トランザクションアスペクト定義 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 書き込み系メソッドのトランザクション設定 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<!-- 読み取り系メソッドのトランザクション設定 -->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- AOP設定: Service層のメソッドにトランザクションアスペクトを適用 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.itcast.springmvc.service.*.*(..))"/>
</aop:config>
</beans>
2.5. springmvc.xml
Spring MVCのディスパッチャーサーブレットの設定を行います。
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Controller層のコンポーネントスキャン -->
<context:component-scan base-package="cn.itcast.springmvc.controller"/>
<!-- MVCアノテーションの有効化 -->
<mvc:annotation-driven/>
<!-- 内部リソースビューリゾルバの設定 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- JSPファイルが存在するディレクトリ -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- JSPファイルの拡張子 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
2.6. web.xml
Webアプリケーションのデプロイメントディスクリプタです。Springのルートアプリケーションコンテキストと、Spring MVCのフロントコントローラーDispatcherServletを設定します。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc-web</display-name>
<!-- Springアプリケーションコンテキストのロードリスナー -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC DispatcherServlet 設定 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!-- サーブレットの起動順序を制御(早期起動させる場合) -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- DispatcherServletのマッピング -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>