Spring+Struts2+Hibernate框架整合流程
一:基本步骤
- 新建Maven项目,导入相关依赖(推荐)
- 在WEB-INF的web.xml中进行配置
————–Hibernate配置 —————-- 创建entity包,创建数据库相关实体类
- 根据实体类创建**.hbm.xml映射文件(移动到resources/mapper下)或者直接使用hibernate注解
——–Spring整合Hibernate配置 ———-- 在resources下创建spring文件夹,新建spring-dao.xml,添加二者整合配置。
————Spring Service层配置———–- 在spring文件夹下新建spring-service.xml,配置service层的相关bean。
————–Spring整合Struts2—————-- 创建.action包, 新建Action类
- 在spring文件夹下创建spring-web.xml配置struts2的Action类
- 在resources下新建struts.xml并配置action等
————–—日志记录—————-- 在resources下创建log4j.properties文件即可。
二:详细配置
1.新建Maven项目,导入相关依赖(推荐)
若不使用maven:请前往Maven官网依次下载jar包导入)
<properties>
<!-- 统一源码的编码方式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 统一各个框架版本 -->
<spring.version>4.3.7.RELEASE</spring.version>
<hibernate.version>4.3.10.Final</hibernate.version>
<struts2.version>2.5.5</struts2.version>
</properties>
<dependencies>
<!-- junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--补全项目依赖 -->
<!--1.日志 java日志有:slf4j,log4j,logback,common-logging
slf4j:是规范/接口
日志实现:log4j,logback,common-logging
使用:slf4j+ -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!--2.数据库相关依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<!--2.dao框架:hibernate-lib-required依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--1) hibernate-lib-jpa/包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--3.Servlet web相关依赖 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--4.spring依赖 -->
<!--1)IOC依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!--2)spring AOP依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aopalliance</groupId>
<artifactId>com.springsource.org.aopalliance</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<!--3)spring dao层依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--4)spring 事务管理 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--5)spring web相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!--6)spring test相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--7)spring log4j 日志记录 -->
<dependency>
<groupId>org.apache.log4j</groupId>
<artifactId>com.springsource.org.apache.log4j</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>com.springsource.org.apache.commons.logging</artifactId>
<version>1.1.1</version>
</dependency>
<!--5.struts2 依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<!--1)Struts2整合Spring -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!--2)Struts2注解开发 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
</dependencies>
2.在WEB-INF的web.xml中进行配置
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>ssh</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Struts2过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring配置文件所在路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
</web-app>
3.创建entity包,创建数据库相关实体类
package cn.zyzpp.domain;
public class Product {
private Integer pid;
private String pname;
private Double price;
public Product() {
super();
}
public Product(Integer pid, String pname, Double price) {
super();
this.pid = pid;
this.pname = pname;
this.price = 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;
}
@Override
public String toString() {
return "pname:" + getPname() + ";price:" + getPrice();
}
}
4.根据实体类创建**.hbm.xml映射文件(移动到resources/mapper下)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2018-2-21 14:13:05 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="cn.zyzpp.domain.Product" table="product">
<id name="pid" type="java.lang.Integer"> <!-- 主键 -->
<column name="pid" /> <!-- 数据库字段 -->
<generator class="increment" /> <!-- 主键策略:自动; assigned:手动-->
</id>
<property name="pname" type="java.lang.String">
<column name="pname" />
</property>
<property name="price" type="java.lang.Double">
<column name="price" />
</property>
</class>
</hibernate-mapping>
或者直接在实体类中使用注释
package cn.zyzpp.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* hibernate注释演示
*/
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id //定义主键
@GeneratedValue(strategy=GenerationType.IDENTITY) //主键生成策略:自动选择; mysql会自增
@Column(name = "id") //对应数据库字段
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "salary")
private int salary;
public Employee() {
//@Entity规定必须有的构造方法
}
public Employee(String firstName, String lastName, int salary) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first_name) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last_name) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
5.在resources下创建spring文件夹,新建spring-dao.xml,添加二者整合配置。
/*推荐在resources下新建jdbc.properties而不是直接使用*/
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/domain?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=
<?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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- Spring配置整合hibernate过程 -->
<!-- 1:配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 2:数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="3000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<!-- 3:配置hibernate4的相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 方言 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 更新表结构:有表使用无表创建 -->
</props>
</property>
<!-- 加载Hibernate4中的映射文件 -->
<property name="mappingLocations">
<list>
<value>classpath:mapper/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置DAO的类 -->
<bean id="productDao" class="cn.zyzpp.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
6.在spring文件夹下新建spring-service.xml,配置service层的相关bean。
package cn.zyzpp.service;
import org.springframework.transaction.annotation.Transactional;
import cn.zyzpp.dao.ProductDao;
import cn.zyzpp.domain.Product;
@Transactional
public class ProductService {
//业务层注入Dao的类
private ProductDao productDao;
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
/**
* 业务层保存方法
* @param product
*/
public void save(Product product){
System.out.println("service中的save方法执行了");
productDao.saveProduct(product);
}
}
<?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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- 配置业务类 -->
<bean id="productService" class="cn.zyzpp.service.ProductService">
<property name="productDao" ref="productDao"></property>
</bean>
<!--配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!--注入数据库连接池 -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
7.创建.action包, 新建Action类
package cn.zyzpp.action;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import cn.zyzpp.domain.Product;
import cn.zyzpp.service.ProductService;
public class ProductAction extends ActionSupport implements ModelDriven<Product> {
// 模型驱动使用的类
private Product product = new Product();
@Override
public Product getModel() {
return product;
}
// struts2和spring整合过程按名称自动注入的业务类
private ProductService productService;
public void setProductService(ProductService productService) {
this.productService = productService;
}
/**
* Action响应
*
* @param product
* @return
* @throws IOException
*/
public String save() throws IOException {
System.out.println("action中的save方法执行了:" + product.toString());
productService.save(product);
return NONE;
}
}
8.在spring文件夹下创建spring-web.xml配置struts2的Action类
<?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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- Spring配置Struts2的Action类 -->
<bean id="productAction" class="cn.zyzpp.action.ProductAction" scope="prototype">
<property name="productService" ref="productService"></property>
</bean>
</beans>
9.在resources下新建struts.xml并配置action等
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- struts2整合spring第一种方式 :还是由struts2.xml配置action
<struts>
<package name="ssh" extends="struts-default" namespace="/">
<action name="product_*" class="cn.zyzpp.action.ProductAction" method="{1}">
</action>
</package>
</struts>-->
<!-- struts2整合spring第二种方式 :由spring容器管理action类 :类路径改为id值即可-->
<struts>
<package name="ssh" extends="struts-default" namespace="/">
<action name="product_*" class="productAction" method="{1}">
</action>
</package>
<!-- 改变action 后戳 -->
<constant name="struts.action.extension" value=",html"></constant>
</struts>
10.在resources下创建log4j.properties文件即可。
log4j.rootLogger=debug, stdout
#log4j.logger.org.mybatis.example.BlogMapper=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%t] %-5p [%c] - %m%n
#DEBUG < INFO < WARN < ERROR <FATAL
Spring+Struts2+Hibernate框架整合流程的更多相关文章
- Spring+Struts2+Hibernate的整合
这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形 ...
- SSH(Spring SpringMVC Hibernate)框架整合
项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构 1.导入依赖jar包 <!--单测--> <dependency&g ...
- 简单Spring+Struts2+Hibernate框架搭建
使用Maven+Spring+Struts2+Hibernate整合 pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- spring+struts2+ibatis 框架整合以及解析
一. spring+struts2+ibatis 框架 搭建教程 参考:http://biancheng.dnbcw.net/linux/394565.html 二.分层 1.dao: 数据访问层(增 ...
- Spring + Spring MVC+Hibernate框架整合详细配置
来源于:http://www.jianshu.com/p/8e2f92d0838c 具体配置参数: Spring: spring-framework-4.2.2Hibernate: hibernate ...
- spring+struts2+hibernate框架搭建(Maven工程)
搭建Spring 1.porm.xml中添加jar包 <!-- spring3 --> <dependency> <groupId>org.springframew ...
- ssh (Spring , Struts2 , Hibernate)框架的配置使用
思维导图(基本配置) 1. 需要引入的包 2 .spring-config.xml 的配置 <!-- 链接数据库 外部配置文件扫入 --> <context:property-ove ...
- Spring+Struts2+Hibernate框架搭建
SSH框架版本:Struts-2.3.30 + Spring-4.2.2 + Hibernate5.2.2 下图是所需要的Jar包: 下面是项目的结构图: 1.web.xml <?xml ...
- Spring+SpringMVC+Mybatis框架整合流程
一:基本步骤 新建Maven项目,导入相关依赖.(推荐) ————–Mybatis配置 —————- 新建entity包,并根据数据库(表)新建相关实体类. 新建dao包,并根据业务创建必要的mapp ...
随机推荐
- php json_encode中提示的中文总是返回"\u767b\u5f55\u6210\u529f\uff01"的解决办法
最近在练习使用 php 写一些简单的接口,但是在返回的消息中,如果有中文,在测试后总是返回: {"resultCode":200,"message":" ...
- SQL中常用字符串函数
--CHARINDEX 返回指定字符的位置--2个参数,第一个是要查找的字符串 第二个参数:要搜索的字符串 参数3:开始查找的位置--查找位置从1开始,返回结果为0时表示为结果为空 SELECT CH ...
- ORA-1652: unable to extend temp segment by 128 in tablespace xxx Troubleshootin
当收到告警信息ORA-01652: unable to extend temp segment by 128 in tablespace xxxx 时,如何Troubleshooting ORA-16 ...
- 分组统计SQL
Itpub上遇到一个求助写SQL的帖子,感觉很有意思,于是写出来看看,要求如下: 有个计划表1, 记录物料的年度计划量 有个实际使用情况表2,记录实际使用情况. 最后要出个统计表,把计划和实际的数据结 ...
- monkey_recorder录制monkeyrunner脚本
转载:monkey_recorder录制monkeyrunner脚本 1. 你必须有android sdk, sdk的tools文件家里有一个monkeyrunner.bat.2. 将如下内容拷贝 ...
- 4.11Python数据处理篇之Matplotlib系列(十一)---图例,网格,背景的设置
目录 目录 前言 (一)图例legend 1.默认不带参数的图例 2.添加参数的图例 3.将图例移动到框外 (二)网格grid 1.说明 2.源代码: 3.输出效果 (三)背景axses 1.设置全局 ...
- [福大软工] Z班——个人技术博客评分
个人技术博客 作业地址 https://edu.cnblogs.com/campus/fzu/SoftwareEngineering2015/homework/1070 作业要求 个人技术博客单次作业 ...
- 【Teradata】配置PE和AMP(congfig和reconfig工具、vprocmanager)
The Reconfiguration and Configuration utilities are used to define the AMPs and PEs that operate tog ...
- HashMap源码调试——认识"put"操作
前言:通常大家都知道HashMap的底层数据结构为数组加链表的形式,但其put操作具体是怎样执行的呢,本文通过调试HashMap的源码来阐述这一问题. 注:jdk版本:jdk1.7.0_51 1.pu ...
- python 列表 元祖
# # 1,写代码,有如下列列表,按照要求实现每⼀一个功能li = ["alex", "WuSir", "ritian", "ba ...