SSH(Spring Struts2 Hibernate)框架整合(注解版)
案例描述:使用SSH整合框架实现部门的添加功能
工程: Maven
数据库:Oracle
框架:Spring Struts2 Hibernate
案例架构:
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>11SSHAnnotation</artifactId>
<packaging>war</packaging>
<name>11SSHAnnotation 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>
<finalName>11SSHAnnotation</finalName>
</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.1applicationContext.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
">
<!--包扫描器-->
<context:component-scan base-package="cn.happy"></context:component-scan>
<!--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>--> <property name="packagesToScan" value="cn.happy.entity"></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>
jdbc.properties:
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=liutao
jdbc.password=liutao
分层架构:
entity:
Dept
@Entity
@Table(name = "Dept")
public class Dept {
@Id
@GeneratedValue
private Integer deptno; //部门编号
@Column
private String dname; //部门名称
@Column
private String loc; //部门位置 //set get方法省略
}
dao:
IDeptDAO:
public interface IDeptDAO {
/**
* @author: liutao
* @Description: 添加部门
* @param:
* @return:
* @throws:
* @date: 2018/2/26 16:23
*/
public void addDept(Dept dept);
}
DeptDAOImpl:
@Repository("deptDAO")
public class DeptDAOImpl implements IDeptDAO{
//植入sessionFactory
@Resource(name = "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:
@Service("deptService")
public class DeptServiceImpl implements IDeptService {
@Resource(name = "deptDAO")
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:
@Controller("deptAction")
@ParentPackage("struts-default")
@Namespace("/")
@Scope("prototype")
public class DeptAction extends ActionSupport implements ModelDriven<Dept>{
private Dept dept=new Dept();
@Resource(name = "deptService")
private IDeptService deptService; @Action(value = "add",results = {@Result(name = "success",location = "/jsp/success.jsp")})
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;
}
}
SSH(Spring Struts2 Hibernate)框架整合(注解版)的更多相关文章
- 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+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 ...
- JAVAWEB 一一 框架整合(SSH,Spring+Struts2+Hibernate IOC/DI AOP声明式事务处理 定时任务)
package org.springframework.orm.hibernate3; import java.io.Serializable; import java.util.List; impo ...
随机推荐
- 计算pi的位数
from random import random from math import sqrt from time import clock DARTS=1000000 hits=0.0 clock( ...
- Python之路(第十七篇)logging模块
一.logging模块 (一).日志相关概念 日志是一种可以追踪某些软件运行时所发生事件的方法.软件开发人员可以向他们的代码中调用日志记录相关的方法来表明发生了某些事情.一个事件可以用一个可包含可选变 ...
- Kubernetes之存储
存储卷概述 容器磁盘上的文件的生命周期是短暂的,这就使得在容器中运行重要应用时会出现一些问题.首先,当容器崩溃时,kubelet 会重启它,但是容器中的文件将丢失——容器以干净的状态(镜像最初的状态) ...
- Java多线程:向线程传递参数的三种方法
在传统的同步开发模式下,当我们调用一个函数时,通过这个函数的参数将数据传入,并通过这个函数的返回值来返回最终的计算结果.但在多线程的异步开发模式下,数据的传递和返回和同步开发模式有很大的区别.由于线程 ...
- C# this扩展方法
本文导读:扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀. 扩展方法当然不能破坏面向对象封装的概念,所以 ...
- Vim使用技巧:特定文件类型关联缩进
Vim如何打开特定文件类型关联自动缩进呢?答案:将filetype indent on写入你的.vimrc文件中
- 【codeforces 765F】Souvenirs
Description Artsem is on vacation and wants to buy souvenirs for his two teammates. There are n souv ...
- error while loading shared libraries: libg2o_core.so: cannot open shared object file: No such file or directory解决方法
在build文件夹目录环境下输入: sudo ldconfig 然后编译就可以了.因为g2o刚装,没生效.
- a标签跳页传参,以及截取URL参数
<a href="dd.index?aa=1&&bb=2"></a> //截取URL参数 // console.log(window.loc ...
- 405 css样式的研究 list-style-type 属性研究
CSS 列表的样式 list-style-type.list-style-position和list-style-image 属性 在CSS中,列表元素是一个块框,列表中的每个表项也是一个块框,只是在 ...