SSH(Spring Struts2 Hibernate)框架整合(xml版)
案例描述:使用SSH整合框架实现部门的添加功能
工程: Maven
数据库:Oracle
案例架构:
1.依赖jar包pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>01MyBatis</artifactId>
<groupId>cn.happy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>10SSHXML</artifactId>
<packaging>war</packaging>
<name>10SSHXML Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!--单测-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
<scope>test</scope>
</dependency>
<!--spring配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--aop使用的jar-->
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency> <!--SpringWeb-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.8.RELEASE</version>
</dependency> <!--JavaEE-->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.5.RELEASE</version>
</dependency> <!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency> <!--hibernate jar包-->
<!--jta的jar包-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency> <!--Spring-ORM-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version> 4.2.2.RELEASE</version>
</dependency> <!--Oracle驱动的jar-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency> <!--struts2-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4.1</version>
</dependency> <dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.4.1 </version>
</dependency> <!-- struts2整合spring -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency> <!-- struts注解核心包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
2.web.xml文件配置
<!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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.resource目录配置文件
3.1 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--1.Datasource-->
<!--1.配置数据源c3p0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
</bean> <!--jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--2.SessionFactory 类:Local-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!--hibernate.xxxxxx必须以hibernate-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!--with current thread bind session和线程绑定的session-->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!--扫描小配置文件 所有的hbm文件-->
<property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>
</bean> <!--3.dao-->
<bean id="deptDAO" class="cn.happy.dao.DeptDAOImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!--4.service-->
<bean id="deptService" class="cn.happy.service.DeptServiceImpl">
<property name="deptDAO" ref="deptDAO"></property>
</bean> <bean id="deptAction" class="cn.happy.action.DeptAction" scope="prototype">
<property name="deptService" ref="deptService"></property>
</bean> <!-- 5.事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!--6.事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <aop:config>
<!–配置了切点Pointcut–>
<aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/>
<!– 顾问–>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor>
</aop:config>-->
</beans>
3.2 jdbc.properties
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=liutao
jdbc.password=liutao
3.3 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"></constant>
<!--将对象工厂的生成全交给Spring-->
<constant name="struts.objectFactory" value="spring"></constant> <package name="default" namespace="/" extends="struts-default">
<action name="add" class="deptAction" method="addDept">
<result>/jsp/success.jsp</result>
</action>
</package>
</struts>
4.分层架构:
entity:
Dept:
/**
* Created by Administrator on 2017/12/24.
* 部门表
*/
public class Dept {
private Integer deptno; //部门编号
private String dname; //部门名称
private String loc; //部门位置 //省略set get方法
}
Dept.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.happy.entity">
<!--实体 name=实体端内容 column=DB端内容-->
<class name="Dept" table="DEPT" dynamic-update="true">
<!--底层数据表对应的主键-->
<id name="deptno" column="DEPTNO">
<!--主键生成策略: assigned 程序员手动给值-->
<generator class="native"/>
</id>
<property name="dname" column="DNAME"></property>
<property name="loc" column="LOC"></property>
</class> </hibernate-mapping>
dao:
IDeptDAO:
public interface IDeptDAO {
/**
* @author: liutao
* @Description: 添加部门
* @param:
* @return:
* @throws:
* @date: 2018/2/26 16:23
*/
public void addDept(Dept dept);
}
DeptDAOImpl:
public class DeptDAOImpl implements IDeptDAO{
//植入sessionFactory
private SessionFactory sessionFactory;
public void addDept(Dept dept) {
Session session = sessionFactory.getCurrentSession();
session.save(dept);
} public SessionFactory getSessionFactory() {
return sessionFactory;
} public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
service:
IDeptService:
public interface IDeptService {
/**
* @author: liutao
* @Description: 添加部门
* @param:
* @return:
* @throws:
* @date: 2018/2/26 16:23
*/
public void addDept(Dept dept);
}
DeptServiceImpl:
public class DeptServiceImpl implements IDeptService {
private IDeptDAO deptDAO;
@Transactional
public void addDept(Dept dept) {
deptDAO.addDept(dept);
} public IDeptDAO getDeptDAO() {
return deptDAO;
} public void setDeptDAO(IDeptDAO deptDAO) {
this.deptDAO = deptDAO;
}
}
Action:
DeptAction:
public class DeptAction extends ActionSupport implements ModelDriven<Dept>{
private Dept dept=new Dept();
private IDeptService deptService; public String addDept(){
deptService.addDept(dept);
return SUCCESS;
} public Dept getDept() {
return dept;
} public void setDept(Dept dept) {
this.dept = dept;
} public IDeptService getDeptService() {
return deptService;
} public void setDeptService(IDeptService deptService) {
this.deptService = deptService;
} public Dept getModel() {
return dept;
}
}
jsp页面:
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加部门</title>
</head>
<body>
<form method="post" action="/add">
部门名称:<input name="dname" type="text"/>
<input type="submit" value="保存"/>
</form>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>添加成功</h1>
</body>
</html>
SSH(Spring Struts2 Hibernate)框架整合(xml版)的更多相关文章
- SSH(Spring SpringMVC Hibernate)框架整合
项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构 1.导入依赖jar包 <!--单测--> <dependency&g ...
- Spring+Struts2+Hibernate框架整合流程
一:基本步骤 新建Maven项目,导入相关依赖(推荐) 在WEB-INF的web.xml中进行配置 ————–Hibernate配置 —————- 创建entity包,创建数据库相关实体类 根据实体类 ...
- ssh (Spring , Struts2 , Hibernate)框架的配置使用
思维导图(基本配置) 1. 需要引入的包 2 .spring-config.xml 的配置 <!-- 链接数据库 外部配置文件扫入 --> <context:property-ove ...
- Spring+Struts2+Hibernate的整合
这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形 ...
- 简单Spring+Struts2+Hibernate框架搭建
使用Maven+Spring+Struts2+Hibernate整合 pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0&q ...
- spring+struts2+hibernate框架搭建(Maven工程)
搭建Spring 1.porm.xml中添加jar包 <!-- spring3 --> <dependency> <groupId>org.springframew ...
- 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 ...
- SSH(Spring4+Struts2+Hibernate4)框架整合
1.加入Spring4 ①. 加入 jar 包
随机推荐
- GWAS分析基本流程及分析思路
数据预处理(DNA genotyping.Quality control.Imputation) QC的工作可以做PLINK上完成Imputation的工作用IMPUTE2完成 2. 表型数据统计分析 ...
- 11.11CTF答案
签到题 flag{0ca175b9c0f726a831d895e269332461 } 下载WIN HEX,用WIN HEX打开“海贼王图片”, ...
- springboot2 pagehelper 使用笔记
作者:cnJun 博客专栏: https://www.cnblogs.com/cnJun/ pom.xml <parent> <groupId>org.springframew ...
- Geometric regularity criterion for NSE: the cross product of velocity and vorticity 3: $u\times \f{\om}{|\om|}\cdot \f{\vLm^\be u}{|\vLm^\be u|}$
在 [Chae, Dongho; Lee, Jihoon. On the geometric regularity conditions for the 3D Navier-Stokes equati ...
- 使用 Google
非原版 Glgoo:http://www.glgoo.com/谷粉搜搜:http://www.gfsoso.net/谷粉搜搜:http://www.gfsswy.com/谷粉搜搜:http://guf ...
- LINQ to SQL 的常见异常及解决办法
Ø 简介 本文主要介绍 LINQ to SQL 中常见的异常,以及对应的解决办法.包括以下内容: 1. 左连接情况下,右表非空类型字段可能抛出异常 1. 左连接情况下,右表非空类型字段可能抛 ...
- Scrapy 入门
Scrapy https://docs.scrapy.org/en/latest/intro/overview.html Scrapy is an application framework for ...
- Vue-cli 模拟数据库
vue-cli2.x 版本开发: 新版在build目录下的webpack.dev.conf.js配置本地数据访问: 1,在const portfinder = require(‘portfinder’ ...
- Contest2161 - 2019-3-21 高一noip基础知识点 测试4 题解版
传送门 预计得分:100+100+100+10=310 实际得分:100+0+82+10=192 你们基础知识不行啊——by wxg T1 一看数据范围就是搜索 但是不能因为数据范围就断送了dp的心 ...
- PHP微信公众号JSAPI网页支付(上)
一.使用场景以及说明 使用场景:商户已有H5商城网站,用户通过消息或扫描二维码在微信内打开网页时,可以调用微信支付完成下单购买的流程. 说明:1.用户打开图文消息或者扫描二维码,在微信内置浏览器打开网 ...