SSMフレームワークの統合-springを作成する.xml(アプリケーションContext.xml)プロファイル
31777 ワード
1.コントローラ以外のすべてのコンポーネントをスキャンする
2.データソースを構成し、dbconfigを作成する.propertiesデータソース情報の保存
dbconfig.propertiesファイルの内容(ここでは著者を例に挙げます):
アプリケーションContext.xmlでデータソースを構成するには、次の手順に従います.
3.構成とmybatisの統合
4.スキャナを構成し、mybatisインタフェースの実装をiocコンテナに追加する
5.取引制御の構成
全体的なコードは次のとおりです.
<context:component-scan base-package="com.lc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
2.データソースを構成し、dbconfigを作成する.propertiesデータソース情報の保存
dbconfig.propertiesファイルの内容(ここでは著者を例に挙げます):
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_study
jdbc.username=root
jdbc.password=root
アプリケーションContext.xmlでデータソースを構成するには、次の手順に従います.
<!-- -->
<!-- -->
<context:property-placeholder location="dbconfig.properties"/>
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
3.構成とmybatisの統合
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--mybatis -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="comboPooledDataSource"></property>
<!-- mybatis mapper -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
4.スキャナを構成し、mybatisインタフェースの実装をiocコンテナに追加する
<!-- , mybatis ioc -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lc.crud.dao"></property>
</bean>
5.取引制御の構成
<!-- -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- -->
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- , xml ( )-->
<aop:config>
<!-- -->
<aop:pointcut id="txPoint" expression="execution(* com.lc.crud.service..*(..))"/>
<!-- -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"></aop:advisor>
</aop:config>
<!-- -->
<tx:advice id="txAdvice">
<tx:attributes>
<!-- -->
<tx:method name="*"/>
<!-- get -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
全体的なコードは次のとおりです.
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--spring , -->
<!-- controller -->
<context:component-scan base-package="com.lc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
<!-- -->
<!-- -->
<context:property-placeholder location="dbconfig.properties"/>
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- mybatis -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--mybatis -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="comboPooledDataSource"></property>
<!-- mybatis mapper -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- , mybatis ioc -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lc.crud.dao"></property>
</bean>
<!-- -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- -->
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- , xml ( )-->
<aop:config>
<!-- -->
<aop:pointcut id="txPoint" expression="execution(* com.lc.crud.service..*(..))"/>
<!-- -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"></aop:advisor>
</aop:config>
<!-- -->
<tx:advice id="txAdvice">
<tx:attributes>
<!-- -->
<tx:method name="*"/>
<!-- get -->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>