简单Spring+Struts2+Hibernate框架搭建

使用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>
    

      

时间: 2024-10-13 05:00:58

简单Spring+Struts2+Hibernate框架搭建的相关文章

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

搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired自动注入Bean(运行时bean总是为null) 常见原因: A.Spring的配置文件中组件扫描路径错误(即<context:component-scan base-package="xx.xx" />配置错误). B.@Autowired声明的beanName错误,bean

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

前言:这里只是说明整个搭建流程,并不进行原理性的讲解 一 下面所需要用到的数据库配置: 数据库方面,使用mysql创建一个users表,具体代码如下: DROP TABLE IF EXISTS `users`; CREATE TABLE `users` (   `UserID` int(4) NOT NULL AUTO_INCREMENT,   `UserName` varchar(16) NOT NULL,   `Password` varchar(16) NOT NULL,   `Telep

Spring+struts2+Hibernate框架的搭建

1.搭建过程 首先需要引入Spring.Struts2.Hibernate的开发包,已经数据库的驱动包. UserAction.java文件 package cn.shop.action; import java.io.IOException; import java.util.List; import javax.annotation.Resource; import org.apache.struts2.convention.annotation.Action; import org.apa

spring+springmvc+hibernate 框架搭建

1.新建web项目,将所需jar包放到 lib 目录下 2.配置web.xml 配置文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLo

Maven搭建Spring+Struts2+Hibernate项目详解

前言 这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架,但是Spring也提供和其他框架的无缝整合,采用组件形式对个框架进行管理,项目实例是按照真实企业里面的开发搭建,也是web的最后一片了.数据库使用mysql,连接池使用的是Druid数据源(这些都无关紧要,可以随时的替换),下面就将详细的介绍一下Maven搭建Spring,Struts2,和hibernation的步奏. 1.数据库设计 数据库库表

搭建基于全注解的Spring+Spring MVC+Hibernate框架

以实例讲解Spring+Spring MVC+Hibernate框架搭建步骤: 一.配置web.xml 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/XML

Spring+Struts2+Hibernate的整合

这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形式对个框架进行管理,项目实例是按照真实企业里面的开发搭建,也是web的最后一片了.数据库使 用mysql,连接池使用的是Druid数据源(这些都无关紧要,可以随时的替换),下面就将详细的介绍一下Maven搭建 Spring,Struts2,和hibernation的步奏. 1.数据库设计 数据库库表

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</artifactI

SSH(Struts2+Spring+Hibernate)框架搭建流程&lt;注解的方式创建Bean&gt;

此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblogs.com/wkrbky/p/5912810.html 一.Hibernate(数据层)的搭建: 实现流程 二.Spring(注入实例)的使用: 实现流程 三.Struts2(MVC)的搭建: 实现流程 这里注意一点问题: Struts2与Hibernate在一起搭建,antlr包,有冲突.MyE