Struts2+Hibernate实现增、删、查、改的简单网页(连接数据库)

本文介绍了如何利用Struts2和Hibernate框架在Myeclipse环境中搭建一个简单的CRUD应用。首先介绍了开发环境,包括Myeclipse、Struts2、Hibernate、Tomcat和MySQL驱动。接着详细阐述了搭建Struts2运行环境,配置web.xml和struts.xml的过程。然后讲解了使用Hibernate编写DAO层,包括javabean、hbm.xml、hibernate.cfg.xml和BaseDao接口及其实现。接下来,文章描述了Action及对应的JSP页面编写,包括显示、编辑页面和主页。最后,展示了运行结果和应用总结。
摘要由CSDN通过智能技术生成



1、基本的环境
1.1、开发工具:Myeclipse 2017 CI
1.2、struts2:struts-2.3.34

1.3、hibernate:hibernate-release-5.2.10.Final

1.4、tomcat: apache-tomcat-8.5.15

1.5、mysql驱动:mysql-connector-java-5.1.42-bin
1.7、C3P0数据源

1.8、数据库MYSQL5.1以上

2、搭建基本strust2运行环境
2.1、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
	<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>
2.2、在src目录下创建strust.xml文件(这个是最终的strust.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>

	<bean name="employeeDao" type="com.splend.crud.dao.EmployeeDao"
		class="com.splend.crud.dao.EmployeeDaoImpl"></bean>
	<bean name="employeeService" type="com.splend.crud.service.EmployeeService"
		class="com.splend.crud.service.EmployeeServiceImpl"></bean>

	<!-- Overwrite Convention -->
	<package name="default" extends="struts-default" namespace="/">

		<interceptors>
			<interceptor-stack name="splend">
				<interceptor-ref name="paramsPrepareParamsStack">
					<param name="prepare.alwaysInvokePrepare">false</param>
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>

		<default-interceptor-ref name="splend"></default-interceptor-ref>
		<action name="emp-*" class="com.splend.crud.handler.EmployeeAction"
			method="{1}">
			<result name="success" type="redirectAction">emp-list</result>
			<result name="{1}">/emp-{1}.jsp</result>
		</action>

		<action name="test" class="com.splend.crud.handler.HelloWorld">
			<result name="success">/success.jsp</result>
		</action>

	</package>
</struts>
3、用hibernate编写dao层
3.1、写一个Employee的javabean

package com.splend.crud.entity;

public class Employee {

	private Integer employeeId;
	private String firstName;
	private String lastName;
	private String email;

	public Employee() {
	}

	public Employee(Integer employeeId, String firstName, String lastName, String email) {
		super();
		this.employeeId = employeeId;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
	}

	public Integer getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Integer employeeId) {
		this.employeeId = employeeId;
	
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值