?
很开心,今天终于在eclipse手动的搭建好了SSH框架。在此记录下来待以后继续学习。
1, spring整合struts
首先在web.xml里添加上
- <!--?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>??
<!-- 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里(以简单登陆为例)
- <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>??
<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中
- <?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>??
<?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里添加上
- <!--?定义数据源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>??
<!-- 定义数据源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
- <?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>??
<?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
- <?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>??
<?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
- <?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>?????
<?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
- <?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>??
<?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中,
- <bean?id="loginAction"?class="com.nwu.hrsystem.action.LoginAction"???
- ????????????scope="prototype">??
- ????????<property?name="loginService"?ref="loginService"></property>??
- ????</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,修改过后,终于跑通了。所以在我们写这类文件时千万要注意,不能写错,不然的话又要花很多时间来检查。