SSH(Spring+struts2+hibernate)整合版详解

Posted sujulin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH(Spring+struts2+hibernate)整合版详解相关的知识,希望对你有一定的参考价值。

Spring+Struts2+hibernate框架整合的步骤:

开发工具:idea+Oracle数据库

一:引入相关的依赖jar包

<!--oracle驱动-->
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
<!--hibernate 核心依赖-->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>

<!-- jta依赖 主要作用是事务处理-->
<!-- https://mvnrepository.com/artifact/javax.transaction/jta -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

<!--spring配置jar-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<!--aop使用的jar-->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>

<!--web编程jar-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

<!--jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>

<!--c3p0连接池-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1</version>
</dependency>

<!--spring整合hibernate-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>

<!--struts2 核心包-->
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4</version>
</dependency>

<!--xwork 核心包-->
<!-- https://mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core -->
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.4</version>
</dependency>

<!--Struts整合Spring的jar包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>

二:搭建整个项目结构

技术分享图片

 

beans层
public class StudentSSH {
private int id;
private String name;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

<?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.sjl.beans">
<!--表名称-->
<class name="StudentSSH" table="STUDENTSSH">
<!--列名-->
<id name="id" column="ID">
<!--主键生成的策略 native:自动生成主键字段-->
<generator class="native"></generator>
</id>
<property name="name" column="NAME"></property>
</class>
</hibernate-mapping>

dao层
public interface IStudentDAO {
/**
* 添加学生
* @param student
* @return
*/
public int add(StudentSSH student);
}

public class StudentDAOImpl implements IStudentDAO {
//注入SessionFactory工厂
private SessionFactory sessionFactory;
//添加
public int add(StudentSSH student) {
Serializable count = sessionFactory.getCurrentSession().save(student);
return (Integer)count;
}

public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

}

service层
public interface IStudentService {
/**
* 添加学生
* @param student
* @return
*/
public int add(StudentSSH student);
}

public class StudentServiceImpl implements IStudentService {
//注入dao层的接口
private IStudentDAO dao;
public IStudentDAO getDao() {
return dao;
}
public void setDao(IStudentDAO dao) {
this.dao = dao;
}

/**
* 添加
* @param student
* @return
*/
@Transactional
public int add(StudentSSH student) {
int result = dao.add(student);
return result;
}
}

action层
public class StudentAction extends ActionSupport {
private StudentSSH studentSSH;
private IStudentService studentService;
/**
* 新增
* @return
*/
public String Add(){
int count= studentService.add(studentSSH);
if (count>0){
return SUCCESS;
}else {
return LOGIN;
}

}

public StudentSSH getStudentSSH() {
return studentSSH;
}
public void setStudentSSH(StudentSSH studentSSH) {
this.studentSSH = studentSSH;
}

public IStudentService getStudentService() {
return studentService;
}
public void setStudentService(IStudentService studentService) {
this.studentService = studentService;
}
}

三:书写配置文件
applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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
">
<!--数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!--识别jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

<!--sessionfactory工厂-->
<bean id="factory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!--控制台打印SQL语句-->
<prop key="hibernate.show_sql">true</prop>
<!--格式化sql-->
<prop key="hibernate.format_sql">true</prop>
<!--方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!--绑定线程-->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!--扫描小配置文件-->
<property name="mappingDirectoryLocations" value="classpath:cn/sjl/beans"></property>
</bean>

<!--dao层-->
<bean id="studentDAO" class="cn.sjl.dao.StudentDAOImpl">
<property name="sessionFactory" ref="factory"></property>
</bean>

<!--service层-->
<bean id="studentService" class="cn.sjl.service.StudentServiceImpl">
<property name="dao" ref="studentDAO"></property>
</bean>

<!--创建struts层 struts默认为多例-->
<bean id="StudentAction" class="cn.sjl.action.StudentAction" scope="prototype">
<property name="studentService" ref="studentService"></property>
</bean>

<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="factory"></property>
</bean>

<!--事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

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.objectFactory" value="spring"></constant>
<package name="result" namespace="/" extends="struts-default">
<action name="add" class="StudentAction" method="Add">
<result name="success">/jsp/index.jsp</result>
<result name="login">/jsp/add.jsp</result>
</action>
</package>
</struts>

jdbc.properties文件
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.user=root
jdbc.password=root

四:web.xml文件(这个不能少)
<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>

五:jsp页面
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加</title>
</head>
<body>
<s:debug></s:debug>
<h2>sshxml整合</h2>
<form method="post" action="/add">
姓名:<input type="text" name="studentSSH.name">
<input type="submit" value="添加">
</form>
</body>
</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加成功</title>
</head>
<body>
<h2>add ok</h2>
</body>
</html>

六:部署及运行


以上是关于SSH(Spring+struts2+hibernate)整合版详解的主要内容,如果未能解决你的问题,请参考以下文章

SSH(struts2+spring+hibernate)三大框架整合

SSH框架的搭建和测试(Spring + Struts2 + Hibernate)

SSH(Struts2+Spring+Hibernate)框架搭建流程

SSH框架简化(struts2+spring+hibernate)

SSH(Spring+struts2+hibernate)整合版详解

Maven搭建SSH(Struts2+Spring+Hibernate)框架入门教程_1