一、整合Spring、Struts和Hibernate用到的jar包:
二、遇到的问题
1.nested exception is java.net.ConnectException: Connection timed out: -------------------------
<property name="connection.url">jdbc:mysql://localhost:3306/ssh?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false</property>
2. he project was not built since its build path is incomplete.
Cannot find the class file for org.springframework.dao.support.DaoSupport. Fix the build path then try building this project。 --------------------------------
加入spring-tx.jar包
3. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
加入spring-web.jar包 --------------------------------------------------------------------------------------------
加入spring-webmvc.jar包
4. java.lang.NoClassDefFoundError: org/springframework/jdbc/support/lob/LobHandler
加入spring_jdbc.jar
5. Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.HiberanteTransactionManager::::
HiberanteTransactionManager中的hibernate拼写错误
(类找不到的时候,一定要先检查类的全路径名是否正确!!!)
6. java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor::::::
增加aopalliance.jar包
7. java.lang.ClassNotFoundException: org.springframework.ao.framework.autoproxy.BeanNameAutoProxyCreator:::::::
拼写错误,改为 org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
增加spring-aop.jar包
(类找不到的时候,一定要先检查类的全路径名是否正确!!!)
8. org.springframework.dao.InvalidDataAccessApiUsageException:
Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL):
Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
9. Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: amigo.sshmemo.dao.Memo column: memoId
(should be mapped with insert="false" update="false"):::
Memo.hbm.xml文件中
<many-to-one name="memoType" 后面加“ insert="false" update="false" ”。
三、Spring的配置文件applicationContext.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<!-- c3p0 数据源 的配置 -->
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false" />
<property name="user" value="root"/>
<property name="password" value="123456"/>
<property name="maxPoolSize" value="80"/>
<property name="minPoolSize" value="1"/>
<property name="initialPoolSize" value="1"/>
<property name="maxIdleTime" value="20"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="c3p0DataSource"/>
<property name="mappingResources">
<list>
<value>amigo/ssh/dao/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref = "sessionFactory" />
</bean>
<!-- intercept:拦截,Interceptor:拦截器 -->
<!-- 定义事务拦截器:TansactionInterceptor -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoproxyCreator -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<!-- 需要自动创建事务代理的bean,一般是一些service类 -->
<value>userManager</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean id="baseDao" class="amigo.ssh.dao.BaseDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userManager" class="amigo.ssh.UserManagerImpl" >
<property name="dao">
<ref bean="baseDao" />
</property>
</bean>
<bean id="regAction" class="amigo.ssh.RegAction">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>
</beans>