简单Spring+Struts2+Hibernate框架搭建
使用Maven+Spring+Struts2+Hibernate整合
- pom文件
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.javasite</groupId>
<artifactId>JavaSite</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>SpringStruts2Hibernate</artifactId>
<packaging>war</packaging> <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.6.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.10.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/proxool/proxool -->
<dependency>
<groupId>proxool</groupId>
<artifactId>proxool</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.cloudhopper.proxool/proxool-cglib -->
<dependency>
<groupId>com.cloudhopper.proxool</groupId>
<artifactId>proxool-cglib</artifactId>
<version>0.9.1</version>
</dependency> </dependencies> </project> - jdbc.properties
proxool.maxConnCount=10
proxool.minConnCount=5
proxool.statistics=1m,15m,1h,1d
proxool.simultaneousBuildThrottle=30
proxool.trace=false jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456 - applicationContext
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <context:annotation-config />
<!-- 配置扫描包 -->
<context:component-scan base-package="com.ssh.*" /> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource">
<bean class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="driverUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maximumConnectionCount" value="${proxool.maxConnCount}" />
<property name="minimumConnectionCount" value="${proxool.minConnCount}" />
<property name="statistics" value="${proxool.statistics}" />
<property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
<property name="trace" value="${proxool.trace}" />
</bean>
</property>
</bean> <!-- 配置session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/ssh/model/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
</value>
</property>
</bean>
<!-- 事物配置 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> </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> <package name="default" namespace="/" extends="struts-default"> <action name="addEmployeeAction" class="action.EmployeeAction" method="addEmployee">
<result name="success" type="redirect" >findEmployeeAction</result>
</action> <action name="findEmployeeAction" class="action.EmployeeAction" method="findEmployee">
<result name="success">/list.jsp</result>
</action> <action name="deleteEmployeeAction" class="action.EmployeeAction" method="deleteEmployee">
<result name="success" type="redirect" >findEmployeeAction</result>
</action> <action name="editEmployeeAction" class="action.EmployeeAction" method="editEmployee">
<result name="success">/addEmployee.jsp</result>
</action> </package> </struts> - web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置Spring的监听器,用于初始化ApplicationContext对象 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Struts2的主过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app> - employee
package com.ssh.model; import java.io.Serializable; public class Employee implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 5954986571237608774L;
private String id;
private String code;
private String name; public String getId()
{
return id;
} public void setId(String id)
{
this.id = id;
} public String getCode()
{
return code;
} public void setCode(String code)
{
this.code = code;
} public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} } - Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.ssh.model">
<class name="Employee" table="EMPLOYEE">
<id name="id" column="ID">
<generator class="identity"/>
</id>
<property name="code" column="CODE"/>
<property name="name" column="NAME"/> </class>
</hibernate-mapping> - EmployeeDao
package com.ssh.dao; import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.ssh.model.Employee; @Component
public class EmployeeDao { @Autowired
private SessionFactory sessionFactory; public void addEmp(Employee emp){
Session session = sessionFactory.openSession();
session.beginTransaction();
if(emp.getId().equals("")||emp.getId()==null){
session.save(emp);
}else{
session.update(emp);
} session.getTransaction().commit();
session.close();
} /**
* findall
* @return
*/
public List<Employee> findEmp(){
Session session = sessionFactory.openSession();
String hql = "from Employee";
List<Employee> list = session.createQuery(hql).list();
session.close();
return list;
} /**
* delete
* @param id
*/
public void delete(String id){
Session session = sessionFactory.openSession();
String hql = "delete from Employee where id=?";
Query query = session.createQuery(hql);
query.setString(0, id);
query.executeUpdate();
session.close(); }
/**
* ��
* @param id
* @return
*/
public Employee edit(String id){
Session session = sessionFactory.openSession();
session.beginTransaction();
String hql = "from Employee where id=:id";
Query query = session.createQuery(hql);
query.setString("id", id);
Employee emp= (Employee) query.list().get(0);
session.getTransaction().commit();
session.close();
return emp;
} } - EmployeeService
package com.ssh.service; import java.util.List; import com.ssh.model.Employee; public interface EmployeeService { public void addEmployee(Employee emp); public List<Employee> findEmp(); public void delete(String id); public Employee edit(String id); }
EmployeeServiceImpl
package com.ssh.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.ssh.dao.EmployeeDao;
import com.ssh.model.Employee;
import com.ssh.service.EmployeeService;
@Component
public class EmployeeServiceImpl implements EmployeeService{ @Autowired
private EmployeeDao employeeDao; public void addEmployee(Employee emp) {
employeeDao.addEmp(emp);
} public List<Employee> findEmp() {
// TODO Auto-generated method stub
return employeeDao.findEmp();
} public void delete(String id) {
employeeDao.delete(id);
} public Employee edit(String id) {
// TODO Auto-generated method stub
return employeeDao.edit(id);
}
}EmployeeAction
package com.ssh.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport;
import com.ssh.model.Employee;
import com.ssh.service.EmployeeService; @Controller
public class EmployeeAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = -8117314381259294068L; public Employee emp; public String id; public List<Employee> list; @Autowired
private EmployeeService employeeService; public String addEmployee(){
System.out.println(emp.getId());
if(emp.getId()==null ||emp.getId().equals("")){
//add
this.employeeService.addEmployee(emp); }else{
//update
this.employeeService.addEmployee(emp);
}
return SUCCESS; } public String findEmployee(){
list = this.employeeService.findEmp();
return SUCCESS;
} public String deleteEmployee(){
System.out.println(id);
this.employeeService.delete(id); return SUCCESS;
} public String editEmployee(){
System.out.println(id);
emp =this.employeeService.edit(id); return SUCCESS;
} public Employee getEmp() {
return emp;
} public void setEmp(Employee emp) {
this.emp = emp;
} public List<Employee> getList() {
return list;
} public void setList(List<Employee> list) {
this.list = list;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} }- addEmployee.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<s:head/>
</head>
<body>
<form action="addEmployeeAction" method="post">
<table width="300">
<tr>
<td></td>
<td><s:textfield label="CODE" name="emp.code"/></td>
</tr>
<tr>
<td></td>
<td><s:textfield label="NAME" name="emp.name" /></td>
</tr>
<tr>
<td></td>
<td><s:textfield label="id" name="emp.id" />
</tr>
<tr>
<td colspan="2">
<s:submit/>
</td>
</tr>
</table>
</form>
</body>
</html> - list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored="true"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<center>
<a href ="addEmployee.jsp">添加</a>
<table border="1">
<tr>
<td>
id
</td>
<td>
code
</td>
<td>
name
</td>
<td>
操 作
</td>
</tr>
<s:iterator value="list" var="au" status="st">
<tr>
<td>
<s:property value="id" />
</td>
<td>
<s:property value="code" />
</td>
<td>
<s:property value="name" />
</td>
<td>
<a href="editEmployeeAction?id=<s:property value="id" />">edit</a>|<a href="deleteEmployeeAction?id=<s:property value="id" />">delete</a>
</td>
</tr>
</s:iterator> </table>
</center> </body>
</html>
简单Spring+Struts2+Hibernate框架搭建的更多相关文章
- spring+struts2+hibernate框架搭建(Maven工程)
搭建Spring 1.porm.xml中添加jar包 <!-- spring3 --> <dependency> <groupId>org.springframew ...
- Spring+Struts2+Hibernate框架搭建
SSH框架版本:Struts-2.3.30 + Spring-4.2.2 + Hibernate5.2.2 下图是所需要的Jar包: 下面是项目的结构图: 1.web.xml <?xml ...
- Spring+Spring MVC+Hibernate框架搭建实例
前言:这里只是说明整个搭建流程,并不进行原理性的讲解 一 下面所需要用到的数据库配置: 数据库方面,使用mysql创建一个users表,具体代码如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- Spring+Struts2+Hibernate框架整合流程
一:基本步骤 新建Maven项目,导入相关依赖(推荐) 在WEB-INF的web.xml中进行配置 ————–Hibernate配置 —————- 创建entity包,创建数据库相关实体类 根据实体类 ...
- 【Spring】Spring+struts2+Hibernate框架的搭建
1.搭建过程 首先需要引入Spring.Struts2.Hibernate的开发包,已经数据库的驱动包. UserAction.java文件 package cn.shop.action; impor ...
- Spring+Struts2+Mybatis框架搭建时的常见典型问题
搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired ...
- ssh (Spring , Struts2 , Hibernate)框架的配置使用
思维导图(基本配置) 1. 需要引入的包 2 .spring-config.xml 的配置 <!-- 链接数据库 外部配置文件扫入 --> <context:property-ove ...
- spring+springmvc+hibernate 框架搭建
1.新建web项目,将所需jar包放到 lib 目录下 2.配置web.xml 配置文件 <?xml version="1.0" encoding="UTF-8&q ...
- spring+struts2+hibernate框架依赖pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
随机推荐
- oracle pctfree和pctused 详解
一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...
- mongoDB学习手记2--建库、删库、插入、更新
上一篇 讲了在windows系统下的安装和启动,本文主要讲怎么建库.删库.插入.更新 在讲之前我们说一下mongoDB的一些基本概念,我们对比关系型数据库能更直观的理解 SQL术语/概念 Mongo ...
- C++运算符优先级 案例1
问: ... short nReaderCount=10 ++pLock->nReaderCount==? ...++和->同为1级优先级,我想很多也有很多新手弄 ...
- Linux入门之常用命令(14) kill
Linux kill 命令使用详解 功能说明:删除执行中的程序或工作. 语 法:kill [-s <信息名称或编号>][程序] 或 kill [-l <信息编号>] 补充说明: ...
- poj2891非互质同余方程
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8176 ...
- ssh项目访问路径及url请求书写
在ssh项目中配置好Struts后,一般可以采用两种方式进行后台请求: 1.html形式,包括a标签,form表单,ajax等.此时的访问链接必须写全路径,可以是相对路径,也可以是绝对路径 相对路径方 ...
- IE兼容
这个基本知识http://www.cnblogs.com/yoosou/archive/2012/07/27/2612443.html 参考: http://www.cnblogs.com/cocow ...
- ASP.NET没有魔法——ASP.NET MVC & 分层
上一篇文章简要说明了MVC所代表的含义并提供了详细的项目及其控制器.视图等内容的创建步骤,最终完成了一个简单ASP.NET MVC程序. 注:MVC与ASP.NET MVC不相等,MVC是一种开发模式 ...
- linux下rename用法--批量重命名
Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,早期的Linux发行版基本上使用的是C语言版本的,现在已经很难见到C语言版本的了, 由于历史原因,在Perl语言 ...
- MVC使用jQuery从视图向控制器传递Model,数据验证,MVC HTML辅助方法小结
//MVC HTML辅助类常用方法记录 (1)@Html.DisplayNameFor(model => model.Title)是显示列名, (2)@Html.DisplayFor(model ...