zoukankan      html  css  js  c++  java
  • Struts+Hibernate系列教材 (二)- hiberante 部分

    步骤1:Hibernate准备工作
    步骤2:创建数据库how2java
    步骤3:pojo
    步骤4:Product.hbm.xml
    步骤5:hibernate.cfg.xml
    步骤6:dao

    步骤 1 : Hibernate准备工作

    这部分准备Hibernate方面的事情

    步骤 2 : 创建数据库how2java

    create database  how2java

    步骤 3 : pojo

    Product有id,name,price3个字段

    package com.how2java.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;

        }

         

    }

    步骤 4 : Product.hbm.xml

    映射Product类对应product_表

    <?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.how2java.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>

    步骤 5 : hibernate.cfg.xml

    hibernate基本配置,指定数据库,账号,密码,方言等等

    <?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/how2java?characterEncoding=GBK</property>

            <property name="connection.username">root</property>

            <property name="connection.password">admin</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/how2java/pojo/Product.hbm.xml" />

             

        </session-factory>

    </hibernate-configuration>

    步骤 6 : dao

    为ProductDAO准备增,删,修改,查询,获取方法

    package com.how2java.dao;

    import java.util.ArrayList;

    import java.util.Iterator;

    import java.util.List;

    import org.hibernate.Query;

    import org.hibernate.Session;

    import org.hibernate.SessionFactory;

    import org.hibernate.cfg.Configuration;

    import com.how2java.pojo.Product;

    public class ProductDAO {

        public void add(Product p) {

            List<Product> result = new ArrayList();

            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;

        }

    }


    更多内容,点击了解: https://how2j.cn/k/struts-hibernate/struts-hibernate-hibernate/82.html

  • 相关阅读:
    lambda 和 iterable
    使用Jenkins部署Python项目
    python下selenium自动化测试自我实践
    【其它】数学学科编码
    【其它】音阶中的数学
    【数理统计基础】 06
    【数理统计基础】 05
    【数理统计基础】 04
    【数理统计基础】 03
    【数理统计基础】 02
  • 原文地址:https://www.cnblogs.com/Lanht/p/12789363.html
Copyright © 2011-2022 走看看