- 步骤1:
根据进行整合
2.步骤2:
创建Package
再创建类:
创建
Product
实体类:
package com.rvelamen.domain;
public class Product {
private Integer pid;
private String pname;
private Double price;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.rvelamen.domain.Product" table="students">
<id name="pid" column="pid">
<generator class="native"/><!-- native表示自增 -->
</id>
<property name="pname" column="pname" length="20"/>
<property name="price" column="price"/>
</class>
</hibernate-mapping>
package com.rvelamen.service;创建ProductAction:
import com.rvelamen.dao.ProductDao;
import com.rvelamen.domain.Product;
import org.springframework.transaction.annotation.Transactional;
/**
*
*/
@Transactional
public class ProductService {
//业务层注入Dao的类
private ProductDao productDao;
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
//业务层保存商品的方法
public void save(Product product) {
System.out.println("Service中的save方法执行了...");
productDao.save(product);
}
}
package com.rvelamen.action;创建ProductDao:
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rvelamen.service.ProductService;
import com.rvelamen.domain.Product;
/**
*
*/
public class ProductAction extends ActionSupport implements ModelDriven<Product> {
private static final long serialVersionUID=1L;
/**
* 模型驱动使用的类
*/
private Product product=new Product();
@Override
public Product getModel() {
return this.product;
}
/**
* Struts和Spring整合过程中按名称自动注入的业务层类
*/
private ProductService productService;
public void setProductService(ProductService productService) {
this.productService = productService;
}
//保存商品的执行的方法:save
public String save(){
System.out.println("Action中的save方法执行了...");
productService.save(product);
return NONE;
}
}
package com.rvelamen.dao;步骤3:
import com.rvelamen.domain.Product;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
/**
*
* 商品管理的Dao类
*/
public class ProductDao extends HibernateDaoSupport {
//DAO中的保存商品的方法
public void save(Product product){
//调用sessionFactory中的getHibernateTemplate的方法
this.getHibernateTemplate().save(product);
System.out.println("DAO中的save方法执行了。。。");
}
}
在web下创建addProduct.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1 align="center">保存商品的页面</h1>
<s:form action="product_save" method="post" namespace="/" theme="simple">
<table border="1" width="400" align="center">
<tr>
<td>商品名称</td>
<td><s:textfield name="pname"></s:textfield></td>
</tr>
<tr>
<td>商品价格</td>
<td><s:textfield name="price"></s:textfield></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="添加"></td>
</tr>
</table>
</s:form>
</body>
</html>
步骤4:
配置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="ssh" extends="struts-default" namespace="/">
<!--Action自身管理类:Struts2和Spring整合的方法一 -->
<!--<action name="product" class="com.rvelamen.action.ProductAction" method="{1}"/>-->
<!--交给Spring管理,能够实现AOP处理,建议使用第二种方式:就是class中使用Spring中的id名称即可-->
<action name="product_*" class="productAction" method="{1}"/>
</package>
</struts>
在src目录下创建jdbc.properties文件:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student
jdbc.username=root
jdbc.password=12345678
在src目录下创建log4j2.xml文件:(配置文件,直接复制就可以了)
<?xml version="1.0" encoding="UTF-8"?>
<!--设置log4j2的自身log级别为warn-->
<configuration status="warn">
<appenders>
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</console>
<!-- 文件将会被存储在System.getProperty("user.home");目录下面 -->
<RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/hpaasvc/info.log"
filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="INFO"/>
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
<RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/hpaasvc/warn.log"
filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="WARN"/>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
<RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/hpaasvc/error.log"
filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="ERROR"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<!--过滤掉spring和hibernate的一些无用的debug信息-->
<logger name="org.springframework" level="INFO">
</logger>
<logger name="org.hibernate" level="INFO">
</logger>
<root level="all">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>
步骤5:
在web/WEB-INF/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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 启动服务器,自动加载配置文件,将自动创建表 -->
<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置Hibernate的相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.fromat_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载Hibernate的映射文件 -->
<property name="mappingResources">
<list>
<value>com/rvelamen/domain/Product.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置Action的类,其中scope是指action中的productService变量是多例模式 -->
<bean id="productAction" class="com.rvelamen.action.ProductAction" scope="prototype">
<property name="productService" ref="productService"/>
</bean>
<!-- 配置业务层的类 -->
<bean id="productService" class="com.rvelamen.service.ProductService">
<!-- 注入DAO的类 -->
<property name="productDao" ref="productDao"/>
</bean>
<!-- 配置DAO的类 -->
<bean id="productDao" class="com.rvelamen.dao.ProductDao">
<!-- 注入HibernateDaoSupport模板,只需要注入sessionFactory,然后通过getHibernateTemplate方法即可 -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务,在Service类上面写上transational注解,注意导对包即可 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
步骤6:
最后的结果:
这样代表成功了!!!
因为在根据文章《利用IntelliJ IDEA 搭建SSH整合》里面的方法进行创建的话,web.xml就会自动对struts2以及spring进行自动配置了,所以无须再次配置(我是新手)。。。你可以先运行起来然后再进行理解。。