您的位置:首页 > 编程语言 > Java开发

Struts + Hibernate实现增删改查实例代码

2018-03-28 16:52 423 查看




1、建库//根据实体字段创建数据库字段
2、实体和xxxxx.hbm.xmlpackage com.entity;

/**
* 产品表实体,对应数据库表product字段,
* Created by lvjun on 2018-03-28.
*/
public class ProductEntity {

int id;
String name;
float price;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}
}
ProductEntity.hbm.xml<?xml version="1.0"?>
<!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.entity">
<!--ProductEntity实体类,对应数据库表product-->
<class name="ProductEntity" table="product">
<!--属性id,映射表里的字段id-->
<id name="id" column="id">
<!--id的自增长方式采用数据库的本地方式-->
<generator class="native"></generator>
</id>
<!--字段属性-->
<property name="name" />
<property name="price" />
</class>

</hibernate-mapping>
3、dao类package com.dao;

import com.entity.ProductEntity;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
* 数据库操作类:增删改查,获取方法
* Created by lvjun on 2018-03-28.
*/
public class ProductDao {

/**
* 查询列表
*/
public List<ProductEntity> listProduct() {
List<ProductEntity> result = new ArrayList();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Query query = session.createQuery("from ProductEntity "); //hql
result = query.list();
session.close();
sessionFactory.close();
return result;
}

/**
* 删除信息
*/
public void deleteProduct(int id){
List<ProductEntity> result = new ArrayList();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
//查询是否存在这条数据
ProductEntity productEntity=(ProductEntity) session.get(Pr
4000
oductEntity.class,id);
session.delete(productEntity);//delete
session.beginTransaction().commit();
session.close();
sessionFactory.close();
}

/**
* 添加信息
*/
public void addProduct(ProductEntity productEntity){
List<ProductEntity> result = new ArrayList();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(productEntity);//save
session.beginTransaction().commit();
session.close();
sessionFactory.close();
}

/**
* 根据id查询明细,用户修改带入参数到修改页面
*/
public ProductEntity getDetail(int id) {
ProductEntity result = null;
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
result = (ProductEntity) session.get(ProductEntity.class, id); //get
session.close();
sessionFactory.close();
return result;
}

/**
* 修改信息
*/
public void updateProduct(ProductEntity productEntity){
List<ProductEntity> result = new ArrayList();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.update(productEntity);//update
session.beginTransaction().commit();
session.close();
sessionFactory.close();
}

}

4、actionpackage com.action;

import com.dao.ProductDao;
import com.entity.ProductEntity;

import java.util.List;

/**
* 分别为增加,删除,修改,查询,获取准备对应的方法.
* Created by lvjun on 2018-03-28.
*/
public class ProductAction {

//声明实例化对象以便于在每一个方法中调用
ProductDao productDao = new ProductDao();
ProductEntity productEntity;
List<ProductEntity> productEntityList;

public ProductDao getProductDao() {
return productDao;
}

public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}

public ProductEntity getProductEntity() {
return productEntity;
}

public void setProductEntity(ProductEntity productEntity) {
this.productEntity = productEntity;
}

public List<ProductEntity> getProductEntityList() {
return productEntityList;
}

public void setProductEntityList(List<ProductEntity> productEntityList) {
this.productEntityList = productEntityList;
}

/**
* 查询列表 方法调用
**/
public String GetList() {
productEntityList = productDao.listProduct();
return "showList";
}

/**
* 删除
**/
public String GetDelete() {
productDao.deleteProduct(productEntity.getId());
return "GetList";
}

/**
* ID明细
* */
public String GetDetail(){
productDao.getDetail(productEntity.getId());
return "showDetail";
}

/**
* 修改
* */
public String GetUpdate(){
productDao.updateProduct(productEntity);
return "GetList";
}

/**
* 添加
* */
public String GetInsert(){
productDao.addProduct(productEntity);
return "GetList";
}

}

5、hibernate.cfg.xml<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<!--驱动-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--地址-->
<property name="connection.url">jdbc:mysql://localhost:3306/school?characterEncoding=GBK</property>
<!--账号-->
<property name="connection.username">root</property>
<!--密码-->
<property name="connection.password">1234</property>

<!-- SQL dialect -->
<!--告诉hibernate数据库类型 MySQL-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--事务管理方式,即每个线程一个事务-->
<property name="current_session_context_class">thread</property>
<!--控制台显示执行的sql语句-->
<property name="show_sql">true</property>
<!--是否会自动更新数据库的表结构,有这句话,是不需要创建表的,因为Hibernate会自动去创建表结构-->
<property name="hbm2ddl.auto">update</property>

<!--配置映射文件,hibernate去识别对应实体类-->
<mapping resource="com/entity/ProductEntity.hbm.xml" />

</session-factory>

</hibernate-configuration>
6、web.xml<!--Struts过滤器-->
<web-app>
<filter>
<filter-name>struts2</filter-name>
<!--所有的请求都被过滤给了这个 Filter-->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>
7、struts.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!--编码格式-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>

<package name="basicstruts" extends="struts-default">

<!--查询-->
<action name="index" class="com.action.ProductAction" method="GetList">
<result name="showList">index.jsp</result>
</action>

<!--删除-->
<action name="delete" class="com.action.ProductAction" method="GetDelete">
<result name="GetList" type="redirect">index</result> <!--type="redirect" 禁重复提交-->
</action>

<!--查单条明细 带入修改页面-->
<action name="detail" class="com.action.ProductAction" method="GetDetail">
<result name="showDetail">edit.jsp</result>
</action>

<!--修改-->
<action name="update" class="com.action.ProductAction" method="GetUpdate">
<result name="GetList" type="redirect">index</result>
</action>

<!--添加-->
<action name="insert" class="com.action.ProductAction" method="GetInsert">
<result name="GetList" type="redirect">index</result>
</action>

</package>

</struts>
8、页面<%--
Created by IntelliJ IDEA.
User: lvjun
Date: 2018-03-28
Time: 9:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>首页</title>
</head>
<body>
<table border="1" align="center" >
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th colspan="2">操作</th>
</tr>

<!--productEntityList是action类中GetList方法返回的结果-->
<s:iterator value="productEntityList" var="p">
<tr>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.price}</td>
<td><a href="detail?productEntity.id=${p.id}">修改</a></td>
<td><a href="delete?productEntity.id=${p.id}">删除</a></td>
</tr>
</s:iterator>

<!--添加--->
<tr>
<td colspan="5" align="center"><a href="add.jsp">添加</a></td>
</tr>

</table>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: lvjun
Date: 2018-03-28
Time: 11:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>修改</title>
</head>
<body>
<form action="update" method="post">
<table align="center" border="1" cellspacing="0">
<tr>
<td>
name:
</td>
<td>
<input type="text" name="productEntity.name" value="${productEntity.name}">
</td>
</tr>
<tr>
<td>
price:
</td>
<td>
<input type="text" name="productEntity.price" value="${productEntity.price}">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="productEntity.id" value="${productEntity.id}">
<input type="submit" value="submit">
</td>
</tr>
</table>
</body>
</html>

<%--
Created by IntelliJ IDEA.
User: lvjun
Date: 2018-03-28
Time: 15:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加</title>
</head>
<body>
<form action="insert" method="post">
<table align="center" border="1" cellspacing="0">
<tr>
<td>
name:
</td>
<td>
<input type="text" name="productEntity.name" value="">
</td>
</tr>
<tr>
<td>
price:
</td>
<td>
<input type="text" name="productEntity.price" value="0">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: