用idea使用struts和hibernate来实现CRUB的操作

在公司实习因为对于框架不是很了解,现在所要做的就是对于基本框架的使用,因为公司原有的系统都是基于ssh开发的,都是较为老的系统,而大部分网上的教程都是用eclipse来进行开发,但是要利用好新工具就比如说idea,这个用来开发可能还是快一点吧。所以自己这次就想把刚学会的东西写一下,希望能写的具体详细。也能给别人一点小小的帮助

第一步。new-file 选择好strusts2和hibernate。之后命名好后随便选择

之后的相应文件目录结构

下面主要是写的是自己对于仅仅用struts2和hibernate的理解。

hibernate是用来操作数据库的,我现在学的里面知道session里面有具体的对于数据库进行操作的方法,也有HQL之类的语句进行操作,用了hibernate框架之后能够更简单方便的去对于数据库进行操作

而struts2就我个人而言觉得可能是类似于MVC里面的C,更多的是去配置,从而去具体的实现相应的功能,比如给相应的action配置相应的东西才能得到跳转相应的页面

首先是写具体的pojo类,也就是实体类,就是要把这个类里面的属性给完成,从而去实现你想从数据库里面调用的具体值,再来实现相应的增删改查。

比如我这个例子里面 写的是Product类 里面的是产品的属性,id name price;

package com.xfc.pojo;
public class Product {
    private int id;
    private String name;
    private 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;
    }

}

然后在相应的pojo类下写Product.hbm.xml 这个其实就是hibernate对类里面的属性进行的配置吧。附上相应的代码,要记得修改相应的数据,或者建立好数据库之后使用persistence进行自动化的生成,这个百度上面都有具体的操作

<?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.xfc.pojo">
    <class name="Product" table="product_">
        <id name="id" column="id">
            <generator class="native">
            </generator>
        </id>
        <property name="name" />
        <property name="price" />
    </class>

</hibernate-mapping>

数据库的基本建立
在这里插入图片描述
建立好数据库之后建表赋值在这里插入图片描述

其实在做好建立数据库这一步之后上面直接用自动生成就好 pojo里面的内容其实不需要写,但是这个给自己一个回顾吧

然后就是hibernate.cfg.xml的配置,这个里面的配置就是针对于数据库的连接,这就是hibernate里面的session的具体使用了

<?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/test?characterEncoding=GBK</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="com/xfc/pojo/Product.hbm.xml" />
    </session-factory>
</hibernate-configuration>

然后就我自己来说,我去在DAO层里面写方法 建立一个DAO包

package com.xfc.dao; 
import com.xfc.pojo.Product;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

public class ProductDAO {
    //在dao层里面写入增删改查的方法
    public void add(Product p) {
        List<Product> result = new ArrayList();
//session是hibernate与数据库进行联系的会话 打开,创建,关闭的操作
// session里面定义了增删改查的方法,所以比直接用strusts要方便的多
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();

        s.save(p);

        s.getTransaction().commit();
        s.close();
        sf.close();
    }

    public Product get(int id) {
        Product result = null;

        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();

        result = (Product) s.get(Product.class, id);

        s.close();
        sf.close();
        return result;
    }

    public void delete(int id) {
        List<Product> result = new ArrayList();
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
        Product p = (Product) s.get(Product.class, id);
        s.delete(p);
        s.getTransaction().commit();
        s.close();
        sf.close();
    }

    public void update(Product p) {
        List<Product> result = new ArrayList();
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
        s.update(p);
        s.getTransaction().commit();
        s.close();
        sf.close();
    }

    public List<Product> listProduct() {
        List<Product> result = new ArrayList();
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        Query q = s.createQuery("from Product p");
        result = q.list();
        s.close();
        sf.close();
        return result;
    }
}

dao层里面写的就是CRUB的方法,最后建立相应的action层。这个是用来去真正意义上去实现的 ProductAction.java

package com.xfc.action;

import com.xfc.dao.ProductDAO;
import com.xfc.pojo.Product;

import java.util.List;

public class ProducAction {
    ProductDAO pdao = new ProductDAO();
    Product product;
    List<Product> products;

    public ProductDAO getPdao() {
        return pdao;
    }

    public void setPdao(ProductDAO pdao) {
        this.pdao = pdao;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    //构建完get/set方法之后在action里面写入增删改查的实际方法
    public String add() {
        pdao.add(product);
        return "list";
    }
    public String edit() {
        product =pdao.get(product.getId());
        return "edit";
    }
    public String delete() {
        pdao.delete(product.getId());
        return "list";
    }
    public String update() {
        pdao.update(product);
        return "list";
    }
    public String list() {
        products = pdao.listProduct();
        return "listJsp";
    }
}

对了,list是展示,把数据表现出来

然后配置相应的struts.xml了

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <package name="basicstruts" extends="struts-default">
        <action name="addProduct" class="com.xfc.action.ProducAction" method="add">
            <result name="list" type="redirect">listProduct.action</result>
        </action>
        <action name="deleteProduct" class="com.xfc.action.ProducAction" method="delete">
            <result name="list" type="redirect">listProduct.action</result>
        </action>
        <action name="updateProduct" class="com.xfc.action.ProducAction" method="update">
            <result name="list" type="redirect">listProduct.action</result>
        </action>
        <action name="editProduct" class="com.xfc.action.ProducAction" method="edit">
            <result name="edit">/WEB-INF/jsp/edit.jsp</result>
        </action>
        <action name="listProduct" class="com.xfc.action.ProducAction" method="list">
            <result name="listJsp">/WEB-INF/jsp/list.jsp</result>
        </action>
    </package>

里面的action之类的在我上篇里面有一个百度搜来的具体讲解,我自己也懵懵懂懂,不过还是要继续学习。大概的意思可能就是action配置好相应的具体文件目录,然后有相应的返回值从而去进行实在的跳转
这个后面就是页面的实现了 这个比较弱智,所以就直接附上相应的代码了
index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <jsp:forward page="/WEB-INF/jsp/list.jsp"/>
  </body>
</html>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<table align="center" border="1" cellspacing="0" width="500px">
    <tr>
        <td>id</td>
        <td>name</td>
        <td>price</td>
        <td>edit</td>
        <td>delete</td>
    </tr>
    <s:iterator value="products" var="p">
        <tr>
            <td>${p.id}</td>
            <td>${p.name}</td>
            <td>${p.price}</td>
            <td><a href="editProduct?product.id=${p.id}">edit</a></td>
            <td><a href="deleteProduct?product.id=${p.id}">delete</a></td>
        </tr>
    </s:iterator>
</table>
<br/>
<form action="addProduct" method="post">
    <table align="center" border="1" cellspacing="0">
        <tr>
            <td>
                name:
            </td>
            <td>
                <input type="text" name="product.name" value="">
            </td>
        </tr>
        <tr>
            <td>
                price:
            </td>
            <td>
                <input type="text" name="product.price" value="">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="submit">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

edit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>

<%@ taglib prefix="s" uri="/struts-tags"%>

<html>

<body>

<form action="updateProduct" method="post">
    <table align="center" border="1" cellspacing="0">
        <tr>
            <td>
                name:
            </td>
            <td>
                <input type="text" name="product.name" value="${product.name}">
            </td>
        </tr>
        <tr>
            <td>
                price:
            </td>
            <td>
                <input type="text" name="product.price" value="${product.price}">
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="hidden" name="product.id" value="${product.id}">
                <input type="submit" value="submit">
            </td>
        </tr>
    </table>

</form>

</body>
</html>

这样下来就是具体的实现了 注意lib千万不要缺失,反正就是缺失了之后会调试起来会非常麻烦

具体的实现页面
在这里插入图片描述在这里插入图片描述
就附上这两张了 别的都能实现。
然后就是希望自己能好好学习,真的人还是要能担当的起一点责任的。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值