当前位置: 代码迷 >> Eclipse >> eclipse 手动筹建ssh框架(spring整合struts,spring 整合hibernate)
  详细解决方案

eclipse 手动筹建ssh框架(spring整合struts,spring 整合hibernate)

热度:59   发布时间:2016-04-23 00:50:41.0
eclipse 手动搭建ssh框架(spring整合struts,spring 整合hibernate)

?

很开心,今天终于在eclipse手动的搭建好了SSH框架。在此记录下来待以后继续学习。??
很开心,今天终于在eclipse手动的搭建好了SSH框架。在此记录下来待以后继续学习。

1, spring整合struts

首先在web.xml里添加上

[html] view plaincopyprint?
  1. <!--?Spring?Framework?-->?????
  2. <listener>?????
  3. ?<listener-class>?????
  4. ?????????org.springframework.web.context.ContextLoaderListener?????
  5. ?</listener-class>?????
  6. </listener>?????
  7. <context-param>?????
  8. ????<param-name>contextConfigLocation</param-name>?????
  9. ????????<!--?applicationContext.xml路径?-->??
  10. ????<param-value>/WEB-INF/applicationContext*.xml</param-value>?????
  11. </context-param>??
<!-- Spring Framework -->   <listener>    <listener-class>        	 org.springframework.web.context.ContextLoaderListener    </listener-class>   </listener>   <context-param>       <param-name>contextConfigLocation</param-name>       	<!-- applicationContext.xml路径 -->   	<param-value>/WEB-INF/applicationContext*.xml</param-value>   </context-param>


在struts.xml里(以简单登陆为例)

[html] view plaincopyprint?
  1. <struts>??
  2. ????<!--?配置了系列常量?-->??
  3. ????<package?name="default"?extends="struts-default">??
  4. ????????<!--?common?action?其中class里应该是spring?中bean?的id-->??
  5. ????????<action?name="Login"?class="loginAction">??
  6. ????????????<result?name="success">/WEB-INF/CONTENT/common/homePage.jsp</result>??
  7. ????????????<result?name="error">/WEB-INF/CONTENT/common/error.jsp</result>??
  8. ????????????<result?name="INPUT">/WEB-INF/CONTENT/common/Login.jsp</result>??
  9. ????????</action>??
  10. ????</package>??
  11. </struts>??
<struts>	<!-- 配置了系列常量 -->	<package name="default" extends="struts-default">		<!-- common action 其中class里应该是spring 中bean 的id-->		<action name="Login" class="loginAction">			<result name="success">/WEB-INF/CONTENT/common/homePage.jsp</result>			<result name="error">/WEB-INF/CONTENT/common/error.jsp</result>			<result name="INPUT">/WEB-INF/CONTENT/common/Login.jsp</result>		</action>	</package></struts>


在applicationContext.xml中

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>?????
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????xmlns:aop="http://www.springframework.org/schema/aop"??
  5. ????xmlns:p="http://www.springframework.org/schema/p"??
  6. ????xmlns:tx="http://www.springframework.org/schema/tx"??
  7. ????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  8. ????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??
  9. ????http://www.springframework.org/schema/tx???
  10. ????http://www.springframework.org/schema/tx/spring-tx-3.0.xsd??
  11. ????http://www.springframework.org/schema/aop???
  12. ????http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">??
  13. ??
  14. ????<bean?name="loginService"?class="com.nwu.hrsystem.service.impl.LoginServiceImpl">??
  15. ????</bean>??
  16. ??????
  17. ????<!--?Action?注入给Service?-->?????
  18. ????<bean?id="loginAction"?class="com.nwu.hrsystem.action.LoginAction"???
  19. ????????????scope="prototype">??
  20. ????????<property?name="loginService"?ref="loginService"></property>??
  21. ????</bean>????
  22. </beans>??
<?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:aop="http://www.springframework.org/schema/aop"	xmlns:p="http://www.springframework.org/schema/p"	xmlns:tx="http://www.springframework.org/schema/tx"	xsi:schemaLocation="http://www.springframework.org/schema/beans 	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd	http://www.springframework.org/schema/tx 	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd	http://www.springframework.org/schema/aop 	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <bean name="loginService" class="com.nwu.hrsystem.service.impl.LoginServiceImpl">    </bean>        <!-- Action 注入给Service -->       <bean id="loginAction" class="com.nwu.hrsystem.action.LoginAction"     		scope="prototype">    	<property name="loginService" ref="loginService"></property>    </bean>  </beans>

我们可以在LoginServiceImpl里写个验证代码检验一下。
2,spring整合hibernate

在applicationContext.xml里添加上

[html] view plaincopyprint?
  1. <!--?定义数据源Bean,使用C3P0数据源实现?-->??
  2. ????<!--?设置连接数据库的驱动、URL、用户名、密码??
  3. ????????连接池最大连接数、最小连接数、初始连接数等参数?-->??
  4. ????<bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource"??
  5. ????????destroy-method="close"??
  6. ????????p:driverClass="com.mysql.jdbc.Driver"??
  7. ????????p:jdbcUrl="jdbc:mysql://localhost:3306/myhrSystem"??
  8. ????????p:user="root"??
  9. ????????p:password="1"??
  10. ????????p:maxPoolSize="40"??
  11. ????????p:minPoolSize="1"??
  12. ????????p:initialPoolSize="1"??
  13. ????????p:maxIdleTime="20"/>??
  14. ??
  15. ????<!--?定义Hibernate的SessionFactory?-->??
  16. ????<!--?依赖注入数据源,注入正是上面定义的dataSource?-->??
  17. ????<bean?id="sessionFactory"??
  18. ????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"??
  19. ????????p:dataSource-ref="dataSource">??
  20. ????????<!--?mappingResouces属性用来列出全部映射文件?-->??
  21. ????????<property?name="mappingResources">??
  22. ????????????<list>??
  23. ????????????????<!--?以下用来列出Hibernate映射文件?-->??
  24. ????????????????<value>com/nwu/hrsystem/beans/User.hbm.xml</value>??
  25. ????????????</list>??
  26. ????????</property>??
  27. ????????<!--?定义Hibernate的SessionFactory的属性?-->??
  28. ????????<property?name="hibernateProperties">??
  29. ????????????<!--?指定数据库方言、是否自动建表??
  30. ????????????????是否生成SQL语句等??-->??
  31. ????????????<value>??
  32. ????????????hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect??
  33. ????????????hibernate.hbm2ddl.auto=update??
  34. ????????????hibernate.show_sql=true??
  35. ????????????hibernate.format_sql=true??
  36. ????????????#开启二级缓存??
  37. ????????????hibernate.cache.use_second_level_cache=true??
  38. ????????????#设置二级缓存的提供者??
  39. ????????????hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider??
  40. ????????????</value>??
  41. ????????</property>??
  42. ????</bean>??
<!-- 定义数据源Bean,使用C3P0数据源实现 -->	<!-- 设置连接数据库的驱动、URL、用户名、密码		连接池最大连接数、最小连接数、初始连接数等参数 -->	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"		destroy-method="close"		p:driverClass="com.mysql.jdbc.Driver"		p:jdbcUrl="jdbc:mysql://localhost:3306/myhrSystem"		p:user="root"		p:password="1"		p:maxPoolSize="40"		p:minPoolSize="1"		p:initialPoolSize="1"		p:maxIdleTime="20"/>	<!-- 定义Hibernate的SessionFactory -->	<!-- 依赖注入数据源,注入正是上面定义的dataSource -->	<bean id="sessionFactory"		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"		p:dataSource-ref="dataSource">		<!-- mappingResouces属性用来列出全部映射文件 -->		<property name="mappingResources">			<list>				<!-- 以下用来列出Hibernate映射文件 -->				<value>com/nwu/hrsystem/beans/User.hbm.xml</value>			</list>		</property>		<!-- 定义Hibernate的SessionFactory的属性 -->		<property name="hibernateProperties">			<!-- 指定数据库方言、是否自动建表				是否生成SQL语句等 	-->			<value>			hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect			hibernate.hbm2ddl.auto=update			hibernate.show_sql=true			hibernate.format_sql=true			#开启二级缓存			hibernate.cache.use_second_level_cache=true			#设置二级缓存的提供者			hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider			</value>		</property>	</bean>

在User.hbm.xml中要注意与PO类User.java 里定义的各种属性及数据库要对应,不然会出错


这样SSH框架就搭建好了,我们可以用一个简单的登陆系统的例子检验一下:

完整的配置文件

web.xml

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns="http://java.sun.com/xml/ns/javaee"?xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"?id="WebApp_ID"?version="3.0">??
  3. ??<display-name>HRSystem</display-name>??
  4. ????
  5. ??<!--?Struts?2?Filter?-->??
  6. ????<filter>??
  7. ????????<filter-name>struts2</filter-name>??
  8. ????????<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>??
  9. ????</filter>??
  10. ????<filter-mapping>??
  11. ????????<filter-name>struts2</filter-name>??
  12. ????????<url-pattern>/*</url-pattern>??
  13. ????</filter-mapping>??
  14. ??????
  15. ????<!--?Spring?Framework?-->?????
  16. ????<listener>?????
  17. ????????<listener-class>?????
  18. ?????????org.springframework.web.context.ContextLoaderListener?????
  19. ????????</listener-class>?????
  20. ????</listener>?????
  21. ????<context-param>?????
  22. ????<param-name>contextConfigLocation</param-name>?????
  23. ????????????<!--?applicationContext.xml路径?-->??
  24. ????????????<param-value>/WEB-INF/applicationContext*.xml</param-value>?????
  25. ????</context-param>?????
  26. ??????
  27. ????<!--?welcome?file?-->??
  28. ????<welcome-file-list>??
  29. ????????<welcome-file>/WEB-INF/CONTENT/common/Login.jsp</welcome-file>??
  30. ????</welcome-file-list>??
  31. ??????
  32. </web-app>??
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>HRSystem</display-name>    <!-- Struts 2 Filter -->	<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>		<!-- Spring Framework -->   	<listener>     	    <listener-class>        	 org.springframework.web.context.ContextLoaderListener       	</listener-class>   	</listener>   	<context-param>       <param-name>contextConfigLocation</param-name>       		<!-- applicationContext.xml路径 -->   		 	<param-value>/WEB-INF/applicationContext*.xml</param-value>   	</context-param>   		<!-- welcome file -->	<welcome-file-list>    	<welcome-file>/WEB-INF/CONTENT/common/Login.jsp</welcome-file>  	</welcome-file-list>	</web-app>


struts.xml

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <!--?指定Struts2配置文件的DTD信息?-->??
  3. <!DOCTYPE?struts?PUBLIC??
  4. ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.1.7//EN"??
  5. ????"http://struts.apache.org/dtds/struts-2.1.7.dtd">??
  6. <!--?Struts2配置文件的根元素?-->??
  7. <struts>??
  8. ????<!--?配置了系列常量?-->??
  9. ????<package?name="default"?extends="struts-default">??
  10. ????????<!--?common?action?-->??
  11. ????????<action?name="Login"?class="loginAction">??
  12. ????????????<result?name="success">/WEB-INF/CONTENT/common/homePage.jsp</result>??
  13. ????????????<result?name="error">/WEB-INF/CONTENT/common/error.jsp</result>??
  14. ????????</action>??
  15. ????</package>??
  16. </struts>??
<?xml version="1.0" encoding="UTF-8"?><!-- 指定Struts2配置文件的DTD信息 --><!DOCTYPE struts PUBLIC	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"	"http://struts.apache.org/dtds/struts-2.1.7.dtd"><!-- Struts2配置文件的根元素 --><struts>	<!-- 配置了系列常量 -->	<package name="default" extends="struts-default">		<!-- common action -->		<action name="Login" class="loginAction">			<result name="success">/WEB-INF/CONTENT/common/homePage.jsp</result>			<result name="error">/WEB-INF/CONTENT/common/error.jsp</result>		</action>	</package></struts>


applicationContext.xml

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>?????
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????xmlns:aop="http://www.springframework.org/schema/aop"??
  5. ????xmlns:p="http://www.springframework.org/schema/p"??
  6. ????xmlns:tx="http://www.springframework.org/schema/tx"??
  7. ????xsi:schemaLocation="http://www.springframework.org/schema/beans???
  8. ????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??
  9. ????http://www.springframework.org/schema/tx???
  10. ????http://www.springframework.org/schema/tx/spring-tx-3.0.xsd??
  11. ????http://www.springframework.org/schema/aop???
  12. ????http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">??
  13. ??
  14. ????<!--?定义数据源Bean,使用C3P0数据源实现?-->??
  15. ????<!--?设置连接数据库的驱动、URL、用户名、密码??
  16. ????????连接池最大连接数、最小连接数、初始连接数等参数?-->??
  17. ????<bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource"??
  18. ????????destroy-method="close"??
  19. ????????p:driverClass="com.mysql.jdbc.Driver"??
  20. ????????p:jdbcUrl="jdbc:mysql://localhost:3306/myhrSystem"??
  21. ????????p:user="root"??
  22. ????????p:password="1"??
  23. ????????p:maxPoolSize="40"??
  24. ????????p:minPoolSize="1"??
  25. ????????p:initialPoolSize="1"??
  26. ????????p:maxIdleTime="20"/>??
  27. ??
  28. ????<!--?定义Hibernate的SessionFactory?-->??
  29. ????<!--?依赖注入数据源,注入正是上面定义的dataSource?-->??
  30. ????<bean?id="sessionFactory"??
  31. ????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"??
  32. ????????p:dataSource-ref="dataSource">??
  33. ????????<!--?mappingResouces属性用来列出全部映射文件?-->??
  34. ????????<property?name="mappingResources">??
  35. ????????????<list>??
  36. ????????????????<!--?以下用来列出Hibernate映射文件?-->??
  37. ????????????????<value>com/nwu/hrsystem/beans/User.hbm.xml</value>??
  38. ????????????</list>??
  39. ????????</property>??
  40. ????????<!--?定义Hibernate的SessionFactory的属性?-->??
  41. ????????<property?name="hibernateProperties">??
  42. ????????????<!--?指定数据库方言、是否自动建表??
  43. ????????????????是否生成SQL语句等??-->??
  44. ????????????<value>??
  45. ????????????hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect??
  46. ????????????hibernate.hbm2ddl.auto=update??
  47. ????????????hibernate.show_sql=true??
  48. ????????????hibernate.format_sql=true??
  49. ????????????#开启二级缓存??
  50. ????????????hibernate.cache.use_second_level_cache=true??
  51. ????????????#设置二级缓存的提供者??
  52. ????????????hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider??
  53. ????????????</value>??
  54. ????????</property>??
  55. ????</bean>??
  56. ??
  57. ??????
  58. ??????
  59. ????<!--?Dao?注入session?工厂?-->??
  60. ????<bean?id="employeeDao"?class="com.nwu.hrsystem.dao.impl.EmployeeDaoImpl">??
  61. ????????<property?name="sessionFactory">??
  62. ????????????<ref?bean="sessionFactory"></ref>??
  63. ????????</property>??
  64. ????</bean>??
  65. ??????
  66. ???<!--??Service?注入给Dao?-->??
  67. ????<bean?name="loginService"?class="com.nwu.hrsystem.service.impl.LoginServiceImpl">??
  68. ????????<property?name="employeeDao">??
  69. ????????????<ref?bean="employeeDao"></ref>??
  70. ????????</property>??
  71. ????</bean>??
  72. ??????
  73. ????<!--?Action?注入给Service?-->?????
  74. ????<bean?id="loginAction"?class="com.nwu.hrsystem.action.LoginAction"???
  75. ????????????scope="prototype">??
  76. ????????<property?name="loginService"?ref="loginService"></property>??
  77. ????</bean>????
  78. </beans>?????
<?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:aop="http://www.springframework.org/schema/aop"	xmlns:p="http://www.springframework.org/schema/p"	xmlns:tx="http://www.springframework.org/schema/tx"	xsi:schemaLocation="http://www.springframework.org/schema/beans 	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd	http://www.springframework.org/schema/tx 	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd	http://www.springframework.org/schema/aop 	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">	<!-- 定义数据源Bean,使用C3P0数据源实现 -->	<!-- 设置连接数据库的驱动、URL、用户名、密码		连接池最大连接数、最小连接数、初始连接数等参数 -->	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"		destroy-method="close"		p:driverClass="com.mysql.jdbc.Driver"		p:jdbcUrl="jdbc:mysql://localhost:3306/myhrSystem"		p:user="root"		p:password="1"		p:maxPoolSize="40"		p:minPoolSize="1"		p:initialPoolSize="1"		p:maxIdleTime="20"/>	<!-- 定义Hibernate的SessionFactory -->	<!-- 依赖注入数据源,注入正是上面定义的dataSource -->	<bean id="sessionFactory"		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"		p:dataSource-ref="dataSource">		<!-- mappingResouces属性用来列出全部映射文件 -->		<property name="mappingResources">			<list>				<!-- 以下用来列出Hibernate映射文件 -->				<value>com/nwu/hrsystem/beans/User.hbm.xml</value>			</list>		</property>		<!-- 定义Hibernate的SessionFactory的属性 -->		<property name="hibernateProperties">			<!-- 指定数据库方言、是否自动建表				是否生成SQL语句等 	-->			<value>			hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect			hibernate.hbm2ddl.auto=update			hibernate.show_sql=true			hibernate.format_sql=true			#开启二级缓存			hibernate.cache.use_second_level_cache=true			#设置二级缓存的提供者			hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider			</value>		</property>	</bean>            <!-- Dao 注入session 工厂 -->    <bean id="employeeDao" class="com.nwu.hrsystem.dao.impl.EmployeeDaoImpl">    	<property name="sessionFactory">    		<ref bean="sessionFactory"></ref>    	</property>    </bean>       <!--  Service 注入给Dao -->    <bean name="loginService" class="com.nwu.hrsystem.service.impl.LoginServiceImpl">    	<property name="employeeDao">    		<ref bean="employeeDao"></ref>    	</property>    </bean>        <!-- Action 注入给Service -->       <bean id="loginAction" class="com.nwu.hrsystem.action.LoginAction"     		scope="prototype">    	<property name="loginService" ref="loginService"></property>    </bean>  </beans>   

User.hbm.xml

[html] view plaincopyprint?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <!--?指定Hibernate映射文件的DTD信息?-->??
  3. <!DOCTYPE?hibernate-mapping?PUBLIC???
  4. ????"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"??
  5. ????"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">??
  6. <!--?Hibernate映射文件的根元素?-->??
  7. <hibernate-mapping?package="com.nwu.hrsystem.beans">??
  8. ????<class?name="User"?table="user_tab"??
  9. ????????discriminator-value="1">??
  10. ????????<!--?使用只读缓存?-->??
  11. ????????<cache?usage="read-only"/>??
  12. ????????<!--?映射标识属性?-->??
  13. ????????<id??name="user_id"?type="java.lang.Integer"?column="user_id">??
  14. ????????????<!--?指定使用native主键生成策略?-->??
  15. ????????????<generator?class="native"/>??
  16. ????????</id>??
  17. ????????<!--?映射普通属性?-->??
  18. ????????<property?name="user_name"?column="user_name"?type="java.lang.String"??
  19. ????????????not-null="true"?length="15"?/>??
  20. ????????<property?name="user_pwd"?column="user_pwd"??type="java.lang.String"??
  21. ????????????not-null="true"?length="15"/>??
  22. ????</class>??
  23. </hibernate-mapping>??
<?xml version="1.0" encoding="UTF-8"?><!-- 指定Hibernate映射文件的DTD信息 --><!DOCTYPE hibernate-mapping PUBLIC 	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- Hibernate映射文件的根元素 --><hibernate-mapping package="com.nwu.hrsystem.beans">	<class name="User" table="user_tab"		discriminator-value="1">		<!-- 使用只读缓存 -->		<cache usage="read-only"/>		<!-- 映射标识属性 -->		<id	name="user_id" type="java.lang.Integer" column="user_id">			<!-- 指定使用native主键生成策略 -->			<generator class="native"/>		</id>		<!-- 映射普通属性 -->		<property name="user_name" column="user_name" type="java.lang.String"			not-null="true" length="15" />		<property name="user_pwd" column="user_pwd"	type="java.lang.String"			not-null="true" length="15"/>	</class></hibernate-mapping>

值得注意的是:User.java文件里首先其定义的成员变量的类型要和数据库里数据表里的每列的类型要一样。其次其成员变量要设定get,set方法。

在action层里声明service层时,在service层声明Dao层时也需要添加get,set方法 且其引用名得和spring配置文件里各层bean里property的name属性要一致。

比如在action里定义了个 private LoginService loginService; 然后就要给其添加get,set方法。并且在applicationContext.xml中,

[html] view plaincopyprint?
  1. <bean?id="loginAction"?class="com.nwu.hrsystem.action.LoginAction"???
  2. ????????????scope="prototype">??
  3. ????????<property?name="loginService"?ref="loginService"></property>??
  4. ????</bean>???
<bean id="loginAction" class="com.nwu.hrsystem.action.LoginAction"     		scope="prototype">    	<property name="loginService" ref="loginService"></property>    </bean> 


在搭建框架时我遇到过这样的一个异常:

Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

在网上查了很多资料有人说是缺jar包,有人说是配置文件有问题。经过我细细排查发现在User.hbm.xml文件里的一个user_pwd属性列里我写成了uesr_pwd,修改过后,终于跑通了。所以在我们写这类文件时千万要注意,不能写错,不然的话又要花很多时间来检查。

  相关解决方案