这次纯用XML整合了一回SSH, hibernate不用annotation的以后 发现自己完全不熟悉spring注入的过程 会少些很多配置 , 费了好大劲才弄好.
一:struts标签给action传值时候NullPointer
public class TestAction extends ActionSupport { private TestTable test = null;
检查了半天 ,最后原因是没有getter&getter ,
查出来发现自己不确定spring是怎么注入进来的 ,
包括struts2标签的 "%{ instance.field }", 试了一下 这个表达式会调用getField()
二:struts配置文件写全class包名
<action name="TestAction_*" class="com.rt.action.TestAction" method="{1}"> <result name="success">..........
到这儿也是没有找到相应类和方法, 查了一下 要写全包名 也就是地址,
之前还以为能自动找到的...
三:必须有Dao层和DaoImpl层
<!-- 必须注入Dao层 --> <bean name="testDao" class="com.rt.dao.TestDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
本来就想做个实验, 就去掉了service层, 觉得直接在action中实现DaoImpl不就行了么 就有"化简"掉了IDao层
如果只有DaoImpl的话 get和load没有问题 ,但是如果想写入数据的话 就会出这个目前我见过最长的异常
Unable to instantiate Action, com.rt.action.TestAction, defined for 'TestAction_add' in namespace '/'Failed to convert property value of type '$Proxy4 implementing org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised'
to required type 'com.rt.dao.TestDaoImpl' for property 'testDaoImpl'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy4 implementing org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised]
to required type [com.rt.dao.TestDaoImpl] for property 'testDaoImpl': no matching editors or conversion strategy found
错误类型是注入的类型不匹配,
大体意思是我想给你注入到了Dao层, 但是现在配置是把DaoImpl注入进Dao层, 类型转换不过来
所以虽然Dao不用更换实现, 但还是老老实实写出该有的层次好了
四:LazyInitialization
21:03:50,512 ERROR LazyInitializationException:42 - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
五:saveOrUpdate
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
使用的是hibernate的saveOrUpdate方法保存实例。saveOrUpdate方法要求ID为null时才执行SAVE,在其它情况下执行UPDATE。在保存实例的时候是新增,但你的ID不为null,所以使用的是UPDATE,但是数据库里没有主键相关的值,所以出现异常。
原来光套用这个,
<context:annotation-config/> <context:component-scan base-package="com.rt"/>
@Component、@Service和 @Controller
现在发现是手动配一下比较好
最后总结一下:
一:配置文件
<struts> <!-- 将Bean交给Spring管理 --> <constant name="struts.objectFactory" value="spring" /> <package name="default" extends="struts-default" namespace="/"> <action name="TestAction_*" class="com.rt.action.TestAction" method="{1}"> <result name="success">success.jsp</result> <result name="input">fail.jsp</result> </action>
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <!-- 必须注入 --> <bean name="testDao" class="com.rt.dao.TestDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 那些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.rt.dao.*.*(..))" /> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" /> </aop:config>
<!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <!-- 数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库连接信息 --> <property name="connection.url">jdbc:mysql://localhost:3306/myoa_dec29?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">890307</property> <!-- 打印SQL语句 --> <property name="hibernate.show_sql">true</property> <!-- 不格式化SQL语句 --> <property name="hibernate.format_sql">false</property> <!-- 为Session指定一个自定义策略 --> <property name="hibernate.current_session_context_class">thread</property> <!-- C3P0 JDBC连接池 --> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.timeout">120</property> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.idle_test_period">120</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.validate">true</property> <!-- 映射文件 --> <mapping resource="com/rt/model/TestTable.hbm.xml"/>
<display-name>MyOa_Dec29</display-name> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value><!-- applicationContext-*.xml --> </context-param> <!-- OpenSessionInViewFilter过滤器 --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Struts2配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 设置程序的默认欢迎页面--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
二: Spring注入过程
public class TestAction extends ActionSupport { private TestTable test = null; private ITestDao testDao = null; public TestAction() { } //必须有setter&getter
必须有Dao层和DaoImpl
public class TestDaoImpl extends HibernateDaoSupport implements ITestDao{ public void addTestTable(TestTable tt) { getHibernateTemplate().saveOrUpdate(tt); }
<hibernate-mapping package="com.rt.model"> <class name="TestTable" table="t_testtable"> <id name="id"> <generator class="native"/> </id> <property name="testname" not-null="true" length="45"/> </class> </hibernate-mapping>