Spring+Hibernate+MySQLの連携を設定するファイルのサンプル

設定ファイル
<?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-2.5.xsd">
<!– C3P0 Poolのdatasourceを定義–>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!– JDBCドライバを指定–>
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<!– DBに接続するURL –>
<property name="jdbcUrl">
<value>jdbc:mysql://localhost:3306/bookman?useUnicode=
true&characterEncoding=utf8</value>
</property>
<!– DBユーザ名 –>
<property name="user">
<value>sampleuser#01</value>
</property>
<!– DBパスワード –>
<property name="password">
<value>samplepwd#01</value>
</property>
<!– maxPoolSize –>
<property name="maxPoolSize">
<value>20</value>
</property>
<!– minPoolSize –>
<property name="minPoolSize">
<value>2</value>
</property>
<!– initialPoolSize –>
<property name="initialPoolSize">
<value>2</value>
</property>
<!– maxIdleTime –>
<property name="maxIdleTime">
<value>20</value>
</property>
</bean>
<!–HibernateのSessionFactoryを定義 –>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!–dataSourceの参照設定 –>
<property name="dataSource" ref="dataSource" />
<!– HibernateのORMマッピングファイル –>
<property name="mappingResources">
<list>
<value>com/bookman/ORM/News.hbm.xml</value>
<value>com/eportal/ORM/Admin.hbm.xml</value>
<value>com/bookman/ORM/Memberlevel.hbm.xml</value>
</list>
</property>
<!– Hibernateのプロパティ –>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!– 開発モードtrue –>
<prop key="show_sql">true</prop>
<!– 最大SQLパッチ数–>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="show_sql">50</prop>
</props>
</property>
</bean>
<!–トランザクション管理HibernateTransactionManager –>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!–sessionFactory依存性を定義 –>
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!–トランザクションインターセプターTransactionInterceptor –>
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!– 制御タイプを定義–>
<property name="transactionAttributes">
<props>
<prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!– BeanNameAutoProxyCreatorfでSpringトランザクションを管理 –>
<bean
class="org.springframework.aop.framework.autoproxy.
BeanNameAutoProxyCreator">
<!– listとvalueの設定 –>
<property name="beanNames">
<list>
<value>sampleService</value>
<value>xxxService</value>
</list>
</property>
<!– proxyTargetClass–>
<property name="proxyTargetClass">
<value>true</value>
</property>
<!– トランザクションインターセプターtransactionInterceptor –>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!– BaseDAOImpl –>
<bean id="dao" class="com.bookman.DAO.BaseDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!– SampleServiceImpl –>
<bean id="sampleService" class="com.bookman.service.SampleServiceImpl"> <property name="dao" ref="dao" />
</bean>
<!– SampleAction –>
<bean id="sampleAction" class="com.bookman.struts.action.
SampleAction"
scope="prototype">
<property name="service" ref="sampleService" />
</bean>
</beans>

Java

Posted by arkgame