Spring+Sturts2+Hibernate集成
Spring+Sturts2+Hibernate集成
2009-01-27 作者:风达
找到的资料是关于Spring 、Struts2与Hibernate的集成,但是之前我所学习的是Struts2,所以做起集成来有些麻烦,但是最终还是做好了。
现在将我所做的操作过程分享出来,肯定有不到位的地方,还请大家多多指教。
首先,导入如下包,可能有些包在SSH目录中找不到,没关系,找不到的估计没有太大的影响,我是新手对此也不大了解。
xwork.jar |
|||||
struts2-core.jar |
Struts 2 |
||||
struts2-spring-plugin.jar |
Struts 2 |
||||
ognl.jar |
Struts 2 |
||||
freemarker.jar |
Struts 2 |
||||
commons-logging-api.jar |
Struts 2 |
||||
mysql-connector-java.jar |
|||||
spring.jar |
|||||
aspectjrt.jar |
Spring 2.0 |
|
|||
aspectjweaver.jar |
Spring 2.0 |
|
|||
antlr.jar |
|||||
asm.jar |
Hibernate Core |
||||
asm-attrs.jar |
Hibernate Core |
||||
cglib.jar |
Hibernate Core |
||||
dom4j.jar |
Hibernate Core |
||||
jdbc2_0-stdext.jar |
Hibernate Core |
||||
ehcache.jar |
Hibernate Core |
||||
hibernate3.jar |
Hibernate Core |
||||
xml-apis.jar |
Hibernate Core |
||||
commons-collections.jar |
Hibernate Core |
||||
ejb3-persistence.jar |
|||||
jta.jar |
Hibernate Annotations |
||||
hibernate-commons-annotations.jar |
Hibernate Annotations |
||||
hibernate-annotations.jar |
Hibernate Annotations |
||||
hibernate-entitymanager.jar |
|||||
javassist.jar |
Hibernate Entity Manager |
||||
jboss-archive-browsing.jar |
Hibernate Entity Manager |
先来说下我们要进行SSH集成的例子:
一个简单的表单页面,
点击按钮,向数据库中插入一条记录,然后返回到本页面,因为是SSH的简单集成,所以功能设计以及页面设计就简陋一些。
好,现在开始。
1.打开MyEclipse,建立WebProject 名为"ssh",文件列表如下,
文件看起来是比较多,让我们一步一步来,将思路理顺了,就会感觉其实也没有什么难的。
2.建立实体Bean,名为UserEntity,UserEntity的文件内容如下:
package fengda.ssh.model; public class UserEntity { private int id; private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getId() { return id; } public void setId(int id) { this.id = id; } } |
3.在UserEntity目录下建立配置文件,UserEntity.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="fengda.ssh.model.UserEntity" table="t_user"> <id name="id"> <generator class="native" /> </id> <property name="username"></property> </class> </hibernate-mapping> |
4.在src目录下 hibernate.cfg.xml,文件信息如下
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url">jdbc:mysql://localhost/ssh</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">aaaa</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="fengda/ssh/model/UserEntity.hbm.xml"/> </session-factory> </hibernate-configuration> |
5.在src目录下建立log4j.properties,文件信息如下:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=warn, stdout |
6.打开MySQL数据库服务器,建立数据库"ssh"
7.建立导出表文件"ExportTable",内容如下
public static void main(String[] args) { //Configuration读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport( cfg ); export.create( true , true ); } |
8.现在建立实体Bean的管理类,先建立一接口"UserManager"
package fengda.ssh.manager; import fengda.ssh.model.UserEntity; public interface UserManager { public void insertUser( UserEntity user ); public void deleteUser( UserEntity user ); public void updateUser( UserEntity user ); } |
9.建立其相应的实现类"UserManagerImpl"
package fengda.ssh.manager;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import fengda.ssh.model.UserEntity;
public class UserManagerImpl extends HibernateDaoSupport implements UserManager {
public void insertUser( UserEntity user ){ this.getHibernateTemplate().save( user ); }
public void deleteUser( UserEntity user ){ this.getHibernateTemplate().delete( user ); }
public void updateUser( UserEntity user ){ this.getHibernateTemplate().update( user ); }
}
|
10.表导出来了,管理的接口也写完了,现在配置Spring与Hibernate的集成,在src目录下建立文件
applicationContext-common.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </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="insert*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="del*" 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(* fengda.ssh.manager.*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> </aop:config> </beans>
|
11.好了Spring与Hibernate已经集成完毕,现在来设置Struts2。建立一个Action
"InsertAction":
package fengda.ssh.action;
import com.opensymphony.xwork2.ActionSupport;
import fengda.ssh.manager.UserManager; import fengda.ssh.model.UserEntity;
public class InsertAction extends ActionSupport {
private static final long serialVersionUID = 4650196867928997266L; private UserManager userManager; private String username;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String execute() throws Exception { this.insert(); System.out.println( this.username ); return "success"; }
public void insert() {
UserEntity user = new UserEntity(); user.setUsername(this.getUsername());
userManager.insertUser(user); }
public void setUserManager(UserManager userManager) { this.userManager = userManager; } }
|
12.将UserManagerImpl和InsertAction交给Spring管理
,建立applicationContext-beans.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="userManager" class="fengda.ssh.manager.UserManagerImpl"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean>
<bean id="insert" class="fengda.ssh.action.InsertAction"> <property name="userManager"> <ref bean="userManager"/> </property> </bean> </beans> |
13.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
<!-- Struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- Hibernate --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- Spring --> <filter> <filter-name>Spring character encoding filter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>Spring character encoding filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
</web-app>
|
14.建立insert.jsp文件,写入body标签内写入
<center> <br /> <br /> <form name="form" action="insert.action"> <input type="text" name="username" /> <input type="submit" value="添加新记录"/> </form> </center> |
15.好了,现在SSH集成完毕,部署到服务器,然后在浏览器的地址栏输入
http://localhost:8080/ssh/insert.action
16.可以运行成功,并且向数据库中存入数据,但是由于中文乱码的问题没有解决,Spring
的过滤器不知道怎么不管用,所以如果存入中文数据,会报出异常。
文章来源: blog.csdn.net,作者:fengda2870,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/fengda2870/article/details/3853593
- 点赞
- 收藏
- 关注作者
评论(0)