使用Maven+Spring+Struts2+Hibernate整合

  1. 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> 
  2. 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
  3. 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>
  4. 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>
  5. 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>
  6. 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;
    } }
  7. 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>
  8. 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;
    } }
  9. 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);
    
    }
    
  10. 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);
    }
    }

      

  11. 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;
    } }

      

  12. 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>

      

  13. 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框架搭建的更多相关文章

  1. spring+struts2+hibernate框架搭建(Maven工程)

    搭建Spring 1.porm.xml中添加jar包 <!-- spring3 --> <dependency> <groupId>org.springframew ...

  2. Spring+Struts2+Hibernate框架搭建

    SSH框架版本:Struts-2.3.30  +  Spring-4.2.2  +  Hibernate5.2.2 下图是所需要的Jar包: 下面是项目的结构图: 1.web.xml <?xml ...

  3. Spring+Spring MVC+Hibernate框架搭建实例

    前言:这里只是说明整个搭建流程,并不进行原理性的讲解 一 下面所需要用到的数据库配置: 数据库方面,使用mysql创建一个users表,具体代码如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  4. Spring+Struts2+Hibernate框架整合流程

    一:基本步骤 新建Maven项目,导入相关依赖(推荐) 在WEB-INF的web.xml中进行配置 ————–Hibernate配置 —————- 创建entity包,创建数据库相关实体类 根据实体类 ...

  5. 【Spring】Spring+struts2+Hibernate框架的搭建

    1.搭建过程 首先需要引入Spring.Struts2.Hibernate的开发包,已经数据库的驱动包. UserAction.java文件 package cn.shop.action; impor ...

  6. Spring+Struts2+Mybatis框架搭建时的常见典型问题

    搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired ...

  7. ssh (Spring , Struts2 , Hibernate)框架的配置使用

    思维导图(基本配置) 1. 需要引入的包 2 .spring-config.xml 的配置 <!-- 链接数据库 外部配置文件扫入 --> <context:property-ove ...

  8. spring+springmvc+hibernate 框架搭建

    1.新建web项目,将所需jar包放到 lib 目录下 2.配置web.xml 配置文件 <?xml version="1.0" encoding="UTF-8&q ...

  9. spring+struts2+hibernate框架依赖pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

随机推荐

  1. Java IO学习笔记(一)

    一.概念 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.在两设备间的传输的数据称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,以进行数据操作. 二.流分类 数据类型 ...

  2. js中set和get的用法

    get 语句作为函数绑定在对象的属性上,当访问该属性时调用该函数. set 语法可以将一个函数绑定在当前对象的指定属性上,当那个属性被赋值时,你所绑定的函数就会被调用. eg: var log = [ ...

  3. Qt 打开文件的默认路径 QFileDialog::getOpenFileName()

    为了说明QFileDialog::getOpenFileName()函数的用法,还是先把函数签名放在这里:   QString QFileDialog::getOpenFileName (       ...

  4. Linux系统管理命令(1)accton的使用

    安装: apt install acct accton accton命令是Linux系统进程管理命令之一,它的作用是打开进程统计,如果不带任何参数,即关闭进程统计.         具体用法为:acc ...

  5. Python文件读写模式

    r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须存在.可读,可写,可追加. w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失.若文件不存在则建立该文件. w+ 打 ...

  6. PowerBI开发 第七篇:数据集和数据刷新

    PowerBI报表是基于数据分析的引擎,数据真正的来源(Data Source)是数据库,文件等数据存储媒介,PowerBI支持的数据源类型多种多样.PowerBI Service(云端)有时不直接访 ...

  7. MongoDB数据库的安装、配置和使用

    1.下载安装包   wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.7.tgz 2.解压安装包   tar -zxf mo ...

  8. Oracle之 any 、some、all解析

    oracle之 any.some.all 解析 因为很少用到, 所以几乎忘记了这几个函数, 不过它们还是很有用的使用它们可以大大简化一些SQL文的语法, 至于效率问题, 如CCW所说它们和EXISTS ...

  9. c# 反射得到实体类的字段名称和值,DataTable转List<T>

    /// <summary> /// 反射得到实体类的字段名称和值 /// var dict = GetProperties(model); /// </summary> /// ...

  10. winPcap编程之不用回调方法捕获数据包(五 转)

    这一次要分析的实例程序跟上一讲非常类似(“打开适配器并捕获数据包”),略微不同的一点是本次将pcap_loop()函数替换成了pcap_next_ex()函数.本节的重点也就是说一下这两个函数之间的差 ...