hibernate简单应用

来源:互联网 发布:8090端口是干嘛的 编辑:程序博客网 时间:2024/05/20 15:37

jar包:
jre jar包
mysql-connector-java;
hibernate必需的jar包

hibernate配置文件:
Product.hbm.xml
hibernate.cfg.xml

[程序目录结构]这里写图片描述

Product.java:

package com.ly.pojo;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;    }    @Override    public String toString() {        return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";    }}

TestHibernate.java

package com.ly.pojo;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class TestHibernate {    public static void main(String[] args) {        // TODO Auto-generated method stub        //获取SessionFactory        SessionFactory sf = new Configuration().configure().buildSessionFactory();        //通过SessionFactory获取一个session        Session s = sf.openSession();        //开启一个事务        s.beginTransaction();        Product p =new Product();        p.setName("iphone");        p.setPrice(7000);        s.save(p);        //  提交事务        s.getTransaction().commit();        //关闭session        s.close();        //关闭SessionFactory        sf.close();    }}

Product.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.ly.pojo">    <class name="Product" table="product">        <id name="id" column="id">            <generator class="native"><!-- 意味着id的自增长方式采用数据库的本地方式 -->            </generator>        </id>    <property name="name" column="name" />    <property name="price" column="price"/>    </class> </hibernate-mapping>

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>        <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>        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="show_sql">true</property>         <property name="current_session_context_class">thread</property>        <property name="hbm2dll.auto">update</property>        <mapping resource="com/ly/pojo/Product.hbm.xml"/>    </session-factory></hibernate-configuration>