简单Spring+Struts2+Hibernate框架搭建
Posted fliay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单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框架搭建的主要内容,如果未能解决你的问题,请参考以下文章
Java--SSH,SSM和Spring Boot框架区别优缺点
简单Spring+Struts2+Hibernate框架搭建