SSH(Spring+Struts2+hibernate)整合基于注解开发的详解

Spring+Struts2+hibernate整合开发项目除了我们常规的方法,还可以使用全注解使开发更加方便快捷。

一:引入支持全注解整合相关的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>

<!--Struts2注解支持jar包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>

二:构建项目结构(以添加学生为例)

beans层

@Entity
@Table //数据库的表名 如果不写就默认为实体名字
public class StudentSSH {
@Id
@GeneratedValue //id的生成策略
private int id;
@Column
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;
}
}

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

}

@Repository("StudentDAO")
public class StudentDAOImpl implements IStudentDAO {
//注入sessionFactory工厂
@Resource
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);

}

@Service("StudentService")
public class StudentServiceImpl implements IStudentService {
@Resource(name = "StudentDAO")
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层
@Controller("StudentAction")
@ParentPackage("struts-default")
@Namespace("/")
@Scope("prototype") //struts为多例 spring为单例
public class StudentAction extends ActionSupport {
private StudentSSH studentSSH;
@Resource(name = "StudentService")
private IStudentService studentService;
/**
* 新增
* @return
*/
@Action(value = "add",results = {@Result(name = "success",location = "/jsp/index.jsp")})
public String Add(){
studentService.add(studentSSH);
return SUCCESS;
}
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
">

<!--识别注解-->
<context:component-scan base-package="cn.sjl"></context:component-scan>

<!--数据源-->
<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>-->
<property name="packagesToScan" value="cn.sjl"></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>

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>
<h2>ssh注解版整合</h2>
<s:debug></s:debug>
<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>

六:部署Tomcat并启动




原文地址:https://www.cnblogs.com/sujulin/p/8508179.html