说到SSH也就是Spring,struts2,Hibernate三大框架那么咱们话不多说开始搭建吧
首先我们这个项目的目的是应用三大框架将一个商品的信息保存到数据库中。
第一步就是创建Web项目,引入相应的Jar包:
Struts2框架开发的相应jar包
Spring框架开发的相应jar包
Hibernate框架开发的相应的jar包
本项目整合共用到43个jar包大家可以看自己需求,以下是项目Jar包构图
第二步我们就开始搭建项目
项目架构图:
这里为了单纯实现效果没有定义接口类不过一样可以达成最后实现的效果!大家可以放心去try!
1.1编写实体类Product类
package cn.ssh.domain; public class Product {
private Integer pid; //商品编号
private String pname; //商品名称
private Double price; //商品价格
//get set 访问
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;
} }
1.2编写DAO类ProductDao
package cn.ssh.dao; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.ssh.domain.Product;
import cn.ssh.service.ProductService; /**
* 商品管理的Dao类
* @author DaPeng
*
*/
public class ProductDao extends HibernateDaoSupport { public void save(Product product){
System.out.println("DAO中保存的方法执行了");
this.getHibernateTemplate().save(product); }
}
1.3编写业务类ProductService
package cn.ssh.service; import org.springframework.transaction.annotation.Transactional; import cn.ssh.dao.ProductDao;
import cn.ssh.domain.Product; /**
* 商品管理的业务层类
*
* @author DaPeng
*
*/
@Transactional
public class ProductService { // 业务层注入DAO的类
private ProductDao productDao; //get set 访问
public ProductDao getProductDao() {
return productDao;
}
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
//业务层中的保存方法
public void save(Product product){
System.out.println("Service中的方法执行了!");
productDao.save(product);
}
}
1.4编写action类ProductAction
package cn.ssh.action; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import cn.ssh.domain.Product;
import cn.ssh.service.ProductService; /**
* 商品管理的Action类
*
* @author DaPeng
*
*/
public class ProductAction extends ActionSupport implements
ModelDriven<Product> { // 创建一个Product对象 模型驱动需要使用的类
private Product product = new Product(); @Override
public Product getModel() { return product;
} // Struts和Spring整合过程中按名称自动注入的业务层类
private ProductService productService; public void setProductService(ProductService productService) {
this.productService = productService;
} public Product getProduct() {
return product;
} public void setProduct(Product product) {
this.product = product;
} public ProductService getProductService() {
return productService;
} /**
* 保存商品的方法
*/
public String save() {
System.out.println("Action中保存的方法执行了");
productService.save(product);
return null;
} }
项目的搭建就是这样了然后我们需要进行编写相关的配置文件
2.1配置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:context="http://www.springframework.org/schema/context"
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.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.driver}"></property> <property
name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property> </bean> -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--数据源的配置 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="bdqn"></property>
<property name="password" value="bdqn"></property>
</bean> <!-- 配置 Hibernate 的相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载Hibernate映射文件 -->
<property name="mappingResources">
<list>
<value>cn/ssh/domain/Product.hbm.xml</value>
</list>
</property>
</bean> <!-- 配置Action类 action类交给Spring容易管理 -->
<bean id="productAction" class="cn.ssh.action.ProductAction"
scope="prototype">
<property name="productService" ref="productService"></property>
</bean>
<!-- 配置业务层的类 -->
<bean id="productService" class="cn.ssh.service.ProductService">
<property name="productDao" ref="productDao"></property>
</bean> <!-- 配置DAO的类 -->
<bean id="productDao" class="cn.ssh.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>
2.2配置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="ssh" extends="struts-default" namespace="/">
<action name="product_*" class="productAction"
method="{1}"> </action>
</package>
</struts>
2.3配置Hibernate的Product.hbm.xml
<?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="cn.ssh.domain.Product" table="product">
<id name="pid" column="pid">
<!-- 主键自动生成策略 -->
<generator class="native"/>
</id>
<property name="pname" column="pname" length="32"></property>
<property name="price" column="price"></property>
</class>
</hibernate-mapping>
2.4如果你们想使用C3P0数据源来获取连接通道那就不能少了jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=****
jdbc.password=****
2.5配置log4j.properties
### direct log messages to stdout ###
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 ### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=error, stdout
3.1搭建项目和配置文件这些就差不多完成了接下来我们需要搭建一个添加商品的界面addProduct.jsp
ps:由于框架中涉及到strut2框架所以页面中我采取了<s:from>标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'addProduct.jsp' starting page</title> </head> <body>
<h1>保存商品的页面</h1>
<s:form action="product_save" method="post" namespace="/" theme="simple">
<table border="1" width="400">
<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"><input type="submit" value="添加"></td>
</tr>
</table>
</s:form>
</body>
</html>
这样一个页面就画好了我们就可以开启Tomcat服务进行添加了
我们点击添加前往数据库中查看即可:
PS:其实三大框架整合并不难,反而为我们的程序省了很多的事情。项目素材及jar包需要的同学可以联系本人QQ1097503654备注博客园即可获取~