SSH(Struts2 Spring Hibernate)整合
Posted IT成长助手
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH(Struts2 Spring Hibernate)整合相关的知识,希望对你有一定的参考价值。
新建一个maven项目
一、先导入Struts2
1、在pom.xml 中一个个按下面的顺序引用jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b07</version>
<scope>provided</scope>
</dependency>
<!-- 引入Struts2依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.33</version>
</dependency>
<!--依赖 spring orm 和hibernate整合-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- 引入Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- 引入mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- 引入c3p0 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- 解析事务切点的表达式 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!-- 引入 struts2整合Spring插件 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.33</version>
</dependency>
2、在src/main/java下新建一个包com.wecrm.action,在这个包下新建TestAction.java类,这个类继承ActionSupport,写test方法
package com.wecrm.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport{
public String test(){
System.out.println("请求进来了");
return SUCCESS;
}
}
3、在src/main/resources下新建一个sruts.xml文件,从Maven 库中找到struts2-core-.....jar文件下的struts-default.xml文件,从中复制头头<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">,粘贴到我们的struts.xml的文件中,
再在struts.xml文件中写<struts></struts>,具体代码如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="testAction_*" class="com.wecrm.action.TestAction"
method="{1}">
<result>/success.jsp</result>
</action>
</package>
</struts>
4、这回我们需要写跳转页面success.jsp了,在src-main-webapp下新建一个success.jsp页面。页面内容如下
<body>成功页面
</body>
5、接下来要在我们的src-main-webapp-WEB-INF下的web.xml中写struts2的核心过滤器,
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置struts2核心过滤器 在struts2-core中找到....ng.filter中的....AndExecuteFilter.class复制全路径-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
6、由此我们struts已经搭建好了
二、我们在struts2的基础上搭建我们的spring
1、在src/main/resources包下,新建一个other,搜索spring,选中spring bean configuration file,命名为applicationContext.xml,在这个文件中写<bean></bean>,代码如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 由Spring代理产生action对象 prototype每次访问都可以产生一个action -->
<bean id="testAction" class="com.wecrm.action.TestAction" scope="prototype"></bean>
</beans>
2、然后在src/test/java下新建一个com.wecrm.test下新建一个Test_Spring.java,在这个类中我们写如下代码
package com.wecrm.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test_Spring {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
applicationContext.getBean("testAction");
}
/* 我们会发现这样写,当我们访问的时候只会,只会到struts中去,因为struts.xml中的<action name="testAction_*" class="com.wecrm.action.TestAction"
method="{1}"> 中可以看出 ,外部访问路径直接在struts中,这样的话,就不会在spring的配置文件applicationContext.xml中去。可是如果我们不这样访问,我们要怎样才能得到testAction中的bean ? 看第3条-->解决方法*/
}
3、为了解决以上问题,我们将spring的配置文件applicationContext.xml中的bean的id“testAction”复制,到struts2的配置文件struts.xml中的对应的action中的class中去。
由此我们的struts.xml的代码就是(红色部分为修改的部分)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="testAction_*" class="testAction"
method="{1}">
<result>/success.jsp</result>
</action>
</package>
</struts>
4、之后我们还要在我们的web.xml中加入一下代码,至于配置集成的类加载器监听中的<listener-class>中的内容来自:在Maven仓库中,找到spring-web-3.0.5.RELEASE.jar下的org.springframework.web.context下的ContextLoaderListener.class,复制他的全路径,粘贴就好了。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- web项目 Tomcat去加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置struts2核心过滤器 在struts2-core中找到....ng.filter中的....AndExecuteFilter.class复制全路径 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<!-- 配置集成的类加载器监听 -->
<listener-class>org.springframework.web.context.ContextLoaderListener.class</listener-class>
</listener>
</web-app>
//如此的话,我们的外部访问路径下,是由struts2中的testAction到struts.xml
//到applicationContext.xml到Test_Spring.java中。
5、到此我们的struts和spring集成好了。
三、在Struts2和Spring集成Hibernate
1、在mysql中建一个表,例如uuser
2、在src/main.java中新建com.wecrm.entity包,在这个包中新建实体类Uuser.java.
包中的属性对应表中的列。并得到get和set的方法,以及构造方法,以及空的构造方法。
如下:
package com.wecrm.entity;
public class Uuser {
private int uid;
private String uname;
private String usex;
private int uage;
private String utel;
private String uadd;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUsex() {
return usex;
}
public void setUsex(String usex) {
this.usex = usex;
}
public int getUage() {
return uage;
}
public void setUage(int uage) {
this.uage = uage;
}
public String getUtel() {
return utel;
}
public void setUtel(String utel) {
this.utel = utel;
}
public String getUadd() {
return uadd;
}
public void setUadd(String uadd) {
this.uadd = uadd;
}
public Uuser(int uid, String uname, String usex, int uage, String utel, String uadd) {
this.uname = uname;
this.usex = usex;
this.uage = uage;
this.utel = utel;
this.uadd = uadd;
}
public Uuser() {
}
}
3、在包com.wecrm.entity,右键新建,查找hibernate,然后选中Hibernate XML Mapping file(hbm.xml),之后选中包,一路next后,我们就可以在这个包中看到自动产生了一个Uuser.hbm.xml文件。
我们要做的就是,在我们的mysql中的Uuser表中的uid列是自动增长,所以,我们要将uid的generator的class由class="assigned"改为class="native"
如下:
<hibernate-mapping>
<class name="com.wecrm.entity.Uuser" table="UUSER">
<id name="uid" type="int">
<column name="UID" />
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="UNAME" />
</property>
<property name="usex" type="java.lang.String">
<column name="USEX" />
</property>
<property name="uage" type="int">
<column name="UAGE" />
</property>
<property name="utel" type="java.lang.String">
<column name="UTEL" />
</property>
<property name="uadd" type="java.lang.String">
<column name="UADD" />
</property>
</class>
</hibernate-mapping>
4、在src/main/resources右键other查找hibernate,选中······cfg.xml,之后选择包,然后next,然后选择Get value from Connection-->New-->MySQL,点击Drivers右边的那个三角形旁的那个类似西瓜形状的,选择MySQL版本,然后在本地选择jar包,然后填写Properties的关于数据库的数据,弄好之后直接finish就好了。之后在src/main/resources下回自动生成一个hibernae.cfg.xml。
代码如下,我们还要加一些代码
<?xml version="1.0" encoding="UTF-8"?>
<!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.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:mysql://mysql.lanlj.com/lanxiaofang</property>
<property name="hibernate.connection.username">lanxiaofang</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/wecrm/entity/Uuser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5、在我们的src/test/test下的Test_Spring中写入代码
package com.wecrm.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.wecrm.entity.Uuser;
public class Test_Spring {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void before(){
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void after(){
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void test(){
/*ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
applicationContext.getBean("testAction");*/
Uuser uuser = new Uuser("tomcat","男",19,"18711016909","湖南长沙");
session.save(uuser);
}
}
6、在src/main/java下新建以下的包
com.wecrm.dao、
package com.wecrm.dao;
import com.wecrm.entity.Uuser;
public interface UuserDao {
public void addUuser(Uuser uuser);
}
com.wecrm.dao.impl、
package com.wecrm.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.wecrm.dao.UuserDao;
import com.wecrm.entity.Uuser;
public class UuserDaoImpl implements UuserDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession(){
return sessionFactory.getCurrentSession();//获取当前的session
}
/*private Session session;
private SessionFactory sessionFactory;
private Transaction transaction;*/
public void addUuser(Uuser uuser) {
// TODO Auto-generated method stub
/*Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction(); */
getSession().save(uuser);
/*transaction.commit();
session.close();
sessionFactory.close();*/
}
}
6、以上我们是假设sessionFactory,已经有了,现在我们需要获取sessionFactory;
在spring的配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建sessionFactory代理对象的bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 配置hibernate的信息 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 创建UuserDaoImpl的对象 -->
<bean id="uuserDao" class="com.wecrm.dao.impl.UuserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 由Spring代理产生action对象 prototype每次访问都可以产生一个action -->
<bean id="testAction" class="com.wecrm.action.TestAction" scope="prototype"></bean>
</beans>
7、虽然以上已经可以了,但是有数据库连接池c3po,总比hibernate自带的好,因此我们再在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="lanxiaofang"></property>
<property name="password" value="******"></property>
<property name="jdbcUrl" value="jdbc:mysql://mysql.lanlj.com/lanxiaofang"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="20"></property>
</bean>
<!-- 创建sessionFactory代理对象的bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 配置数据源 连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate的信息 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置映射信息 -->
<property name="mappingResources" value="classpath:com/wecrm/entity/*.hbm.xml" ></property>
</bean>
<!-- 创建UuserDaoImpl的对象 -->
<bean id="uuserDao" class="com.wecrm.dao.impl.UuserDaoImpl">
<!-- UuserDaoImpl中的属性sessionFactory依赖注入 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 由Spring代理产生action对象 prototype每次访问都可以产生一个action -->
<bean id="testAction" class="com.wecrm.action.TestAction" scope="prototype"></bean>
</beans>
8、有了c3p0的数据库连接池,因此我们的hibernate自带的数据库连接就可以不用了,我们把他注释掉,在hibernate.cfg.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!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.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:mysql://mysql.lanlj.com/lanxiaofang</property>
<property name="hibernate.connection.username">lanxiaofang</property> -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- <mapping resource="com/wecrm/entity/Uuser.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
9、接下来我们在src/main/java中新建com.wecrm.biz和com.wecrm.biz.impl包
com.wecrm.biz下新建UuserBiz.java
package com.wecrm.biz;
import com.wecrm.entity.Uuser;
public interface UuserBiz {
public void addUuser(Uuser uuser);
}
在com.wecrm.biz.impl中新建UuserBizImpl.java
package com.wecrm.biz.impl;
import com.wecrm.biz.UuserBiz;
import com.wecrm.dao.UuserDao;
import com.wecrm.entity.Uuser;
public class UuserBizImpl implements UuserBiz{
private UuserDao uuserDao;
public void setUuserDao(UuserDao uuserDao) {
this.uuserDao = uuserDao;
}
public void addUuser(Uuser uuser) {
// TODO Auto-generated method stub
uuserDao.addUuser(uuser);
}
}
10、以上的biz写好后,我们回到applicationContext.xml中,为UuserBizImpl引用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="lanxiaofang"></property>
<property name="password" value="********"></property>
<property name="jdbcUrl" value="jdbc:mysql://mysql.lanlj.com/lanxiaofang"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="20"></property>
</bean>
<!-- 创建sessionFactory代理对象的bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 配置数据源 连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate的信息 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置映射信息 -->
<property name="mappingResources" value="classpath:com/wecrm/entity/*.hbm.xml" ></property>
</bean>
<!-- 创建UuserDaoImpl的对象 -->
<bean id="uuserDao" class="com.wecrm.dao.impl.UuserDaoImpl">
<!-- UuserDaoImpl中的属性sessionFactory依赖注入 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建UuserBizImpl的对象 -->
<bean id="studentBiz" class="com.wecrm.biz.impl.UuserBizImpl">
<property name="uuserDao" ref="uuserDao"></property>
</bean>
<!-- 由Spring代理产生action对象 prototype每次访问都可以产生一个action -->
<bean id="testAction" class="com.wecrm.action.TestAction" scope="prototype"></bean>
</beans>
以上是关于SSH(Struts2 Spring Hibernate)整合的主要内容,如果未能解决你的问题,请参考以下文章
spring+struts2+hibernate整合(ssh)
SSH(struts2+spring+hibernate)三大框架整合
Struts2+Spring+Hibernate(SSH)框架的搭建