之前的博客我们总结了spring基础、spring分别整合struts2、hibernate、mybatis等,今天我们来同时整合下 struts、spring、hibernate,也就是所谓的 ssh 。
整合流程:
1 首先整合spring和hibernate,这次我们在spring 中配置bean使用注解的方式 ,hibernate实体映射关系也使用注解的方式,配置完毕后用简单方法测试下hibernate是否整合成功。
a 加入支持:添加 spring核心包、hibernate 3.6 包、 spring整合hibernate包 , 在src下建立applicationContext.xml (先建立src下便于测试hibernate)。
b 编写实体类,加入hibernate注解,编写方法类测试类,在applicationContext.xml中添加hibernate模板类配置以及包扫描语句 。在类中添加spring bean注解。
c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试
2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。
a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立struts.xml,复制头文件进去。将applicationContext.xml移到WEB-INF目录下。web容器中(web.xml)中添加struts2 filter以及spring 监听器。
b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ioc注解的方式, 所以默认的bean的id 为 类名(首字母小写)
c 编写action类、页面进行测试
测试项目结构如下:
lib包:加上mysql的 一共29个
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>etoak</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>etoak</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com"></context:component-scan>
<!-- <bean id="advLocationAction" class="com.etoak.action.AdvLocationAction" scope="prototype" ></bean> -->
<!--
配置hibernate注解形式:
使用AnnotationSessionFactoryBean工厂bean进行配置
-->
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean name="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="packagesToScan" value="com.etoak.bean"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/yitu"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
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>
<constant name="struts.devMode" value="true" /> <!-- 开发模式 -->
<package name="adv" namespace="/page" extends="struts-default">
<!--
使用通配符简化配置
e.g : advLocation_add
-->
<action name="*_*" class="{1}Action" method="{2}">
<result>/msgPage/{1}_{2}_success.jsp</result>
</action>
</package>
</struts>
AdvLocationAction.java
package com.etoak.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.etoak.bean.Adarea;
import com.etoak.dao.AdvLocationDao;
import com.opensymphony.xwork2.ActionSupport;
@Controller
public class AdvLocationAction extends ActionSupport {
@Autowired
private AdvLocationDao adldao;
@Autowired
private Adarea adarea;
private String adpage;
private String adlocation;
private String adtype;
private Integer sizeh;
private Integer sizew;
private Double price;
public AdvLocationDao getAdldao() {
return adldao;
}
public void setAdldao(AdvLocationDao adldao) {
this.adldao = adldao;
}
public Adarea getAdarea() {
return adarea;
}
public void setAdarea(Adarea adarea) {
this.adarea = adarea;
}
public String getAdpage() {
return adpage;
}
public void setAdpage(String adpage) {
this.adpage = adpage;
}
public String getAdlocation() {
return adlocation;
}
public void setAdlocation(String adlocation) {
this.adlocation = adlocation;
}
public String getAdtype() {
return adtype;
}
public void setAdtype(String adtype) {
this.adtype = adtype;
}
public Integer getSizeh() {
return sizeh;
}
public void setSizeh(Integer sizeh) {
this.sizeh = sizeh;
}
public Integer getSizew() {
return sizew;
}
public void setSizew(Integer sizew) {
this.sizew = sizew;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String add(){
System.out.println(adpage+"--"+adlocation+"--"+adtype+"--"+sizeh+"--"+sizew+"--"+price);
adarea.setAdlocation(adlocation);
adarea.setAdpage(adpage);
adarea.setAdtype(adtype);
adarea.setSizeh(sizeh);
adarea.setSizew(sizew);
adarea.setPrice(price);
adldao.addAdvLocate(adarea);
return SUCCESS;
}
public String delete(){
return SUCCESS;
}
public String update(){
return SUCCESS;
}
}
AdvLocationDaoImpl.java
package com.etoak.dao;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.etoak.bean.Adarea;
@Repository
public class AdvLocationDaoImpl implements AdvLocationDao{
@Autowired
private HibernateTemplate ht;
public boolean addAdvLocate(Adarea ad){
//System.out.println("id为:"+ad.getAdlocation());
Serializable se = ht.save(ad);
//se 添加成功后该数据的主键值
Integer id = (Integer)se;
return id>0;
}
public boolean deleteAdvLocate(Integer id){
return true;
}
public boolean updateAdvLocate(Adarea ad){
return true;
}
}
AdvLocationDao.java:
package com.etoak.dao;
import org.springframework.stereotype.Repository;
import com.etoak.bean.Adarea;
@Repository
public interface AdvLocationDao {
public boolean addAdvLocate(Adarea adarea);
public boolean deleteAdvLocate(Integer id);
public boolean updateAdvLocate(Adarea adarea);
}
AdvArea.java
package com.etoak.bean;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.springframework.stereotype.Component;
@Component
@Entity(name="adarea")
public class Adarea implements java.io.Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="adpage")
private String adpage;
@Column(name="adlocation")
private String adlocation;
@Column(name="adtype")
private String adtype;
@Column(name="sizeh")
private Integer sizeh;
@Column(name="sizew")
private Integer sizew;
@Column(name="price")
private Double price;
@OneToMany(cascade=CascadeType.ALL,mappedBy="adarea")
private Set<Ad> ads;
public Adarea() {
}
public Adarea(String adpage, String adlocation, String adtype,
Integer sizeh, Integer sizew, Double price, Set<Ad> ads) {
this.adpage = adpage;
this.adlocation = adlocation;
this.adtype = adtype;
this.sizeh = sizeh;
this.sizew = sizew;
this.price = price;
this.ads = ads;
}
public Adarea(String adpage, String adlocation, String adtype,
Integer sizeh, Integer sizew, Double price) {
this.adpage = adpage;
this.adlocation = adlocation;
this.adtype = adtype;
this.sizeh = sizeh;
this.sizew = sizew;
this.price = price;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAdpage() {
return this.adpage;
}
public void setAdpage(String adpage) {
this.adpage = adpage;
}
public String getAdlocation() {
return this.adlocation;
}
public void setAdlocation(String adlocation) {
this.adlocation = adlocation;
}
public String getAdtype() {
return this.adtype;
}
public void setAdtype(String adtype) {
this.adtype = adtype;
}
public Integer getSizeh() {
return this.sizeh;
}
public void setSizeh(Integer sizeh) {
this.sizeh = sizeh;
}
public Integer getSizew() {
return this.sizew;
}
public void setSizew(Integer sizew) {
this.sizew = sizew;
}
public Double getPrice() {
return this.price;
}
public void setPrice(Double price) {
this.price = price;
}
public Set<Ad> getAds() {
return ads;
}
public void setAds(Set<Ad> ads) {
this.ads = ads;
}
}