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

Struts + Hibernate实现增删改查实例代码_第1张图片Struts + Hibernate实现增删改查实例代码_第2张图片

1、建库

//根据实体字段创建数据库字段

2、实体和xxxxx.hbm.xml

package 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






    
    
        
        
            
            
        
        
        
        
    


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 listProduct() {
        List 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 result = new ArrayList();
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        //查询是否存在这条数据
        ProductEntity productEntity=(ProductEntity) session.get(ProductEntity.class,id);
        session.delete(productEntity);//delete
        session.beginTransaction().commit();
        session.close();
        sessionFactory.close();
    }


    /**
     * 添加信息
     */
    public  void addProduct(ProductEntity productEntity){
        List 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 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、action

package 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 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 getProductEntityList() {
        return productEntityList;
    }

    public void setProductEntityList(List 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






    

        
        
        com.mysql.jdbc.Driver
        
        jdbc:mysql://localhost:3306/school?characterEncoding=GBK
        
        root
        
        1234

        
        
        org.hibernate.dialect.MySQLDialect
        
        thread
        
        true
        
        update

        
        

    


6、web.xml



    
        struts2
        
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    

    
        struts2
        FORWARD
        REQUEST
        /*
    


7、struts.xml





    
    

    

        
        
            index.jsp
        

        
        
            index 
        

        
        
            edit.jsp
        

        
        
            index
        

        
        
            index
        


    


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"%>


    首页



        
编号 名称 价格 操作
${p.id} ${p.name} ${p.price} 修改 删除
添加
<%--
  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"%>

    
        修改
    
    
    
name:
price:

<%--
  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" %>


    添加


    
        
name:
price:


你可能感兴趣的:(Java)