zoukankan      html  css  js  c++  java
  • Hibernate--(一)

    项目结构:

    表:

     

    1.实体类:

    public class Product {
        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;
        }
    }

    2.Product.hbm.xml

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

    3.hibernate.cfg.xml

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD//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=UTF-8</property>
            <property name="connection.username">root</property>
            <property name="connection.password">123456</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/h/pojo/Product.hbm.xml" />
        </session-factory>
    </hibernate-configuration>

    4.Test.java

    public class Test {
    
        public static void main(String[] args) {
            /*
            hibernate的基本步骤是:
            1. 获取SessionFactory
            2. 通过SessionFactory 获取一个Session
            3. 在Session基础上开启一个事务
            4. 通过调用Session的save方法把对象保存到数据库
            5. 提交事务
            6. 关闭Session
            7. 关闭SessionFactory
             */
            SessionFactory sf = new Configuration().configure().buildSessionFactory();
    
            Session s = sf.openSession();
            s.beginTransaction();
    
            Product p = new Product();
            p.setName("iphone7");
            p.setPrice(7000);
            s.save(p);
    
            s.getTransaction().commit();
            s.close();
            sf.close();
        }

    5.成功

     

  • 相关阅读:
    oc调用rest api
    EF Attach时已存在的处理方式
    设置XtraForm标题居中
    读取DBF文件数据
    GP 环境参数名称列表
    MapWinGIS.ocx 注册
    ArcEngine :The XY domain on the spatial reference is not set or invalid错误
    批量修改sql server 2008的架构
    net不安装Oracle11g客户端直接使用ODAC
    Ninject使用介绍
  • 原文地址:https://www.cnblogs.com/crazy-lc/p/12157361.html
Copyright © 2011-2022 走看看