Java Hibernate Mysql

Java Hibernate Mysql

demo:http://download.csdn.net/download/keen_zuxwang/9898205

添加lib庫文件夾,包括hibnernate庫以及連接Mysql的驅動的jar

hibernate-core-5.0.0.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
mysql-connector-java-5.1.40-bin.jar //mysql java 連接驅動jar

1、hibernate.cfg.xml – hibernate配置xml,包括驅動、登陸賬戶、密碼、方言、實體類的映射文件設置等

<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE hibernate-configuration PUBLIC    
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

    <hibernate-configuration> 
        <session-factory> 
            <property name="hibernate.connection.driver_class"> 
                com.mysql.jdbc.Driver    
            </property> 
            <property name="hibernate.connection.url"> 
                jdbc:mysql://localhost:3306/test
            </property> 
            <!--  數據庫連接設置 --> 

            <property name="hibernate.connection.username">user-name</property> 
            <property name="hibernate.connection.password">user-password</property> 

            <!-- show_sql 生成SQL語句輸出到日誌以供調式 --> 
            <property name="hibernate.show_sql">true</property> 

            <!-- SQL dialect 方言 --> 
            <property name="hibernate.dialect"> 
                org.hibernate.dialect.MySQLDialect  
            </property> 

             <!-- 指定session通過當前執行的線程來跟蹤和界定 -->
            <property name="hibernate.current_session_contecxt_class" >
                thread
            </property>

            <!-- 添加實體類的映射文件--> 
            <mapping resource="com/myhibernate/city.hbm.xml" /> 

        </session-factory> 
    </hibernate-configuration> 

2、city.hbm.xml — hibernate mapping xml文檔實體類與數據庫表的映射設置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.Net/hibernate-mapping-3.0.dtd">
<!--<hibernate-mapping package="com.myhibernate"> -->

<hibernate-mapping> 
   <class name="com.myhibernate.City" table="city"> 
     <id name="id" type="java.lang.Integer"></id> 
     <property name="name" type="java.lang.String"  /> 
     <property name="level" type="java.lang.Integer" /> 
   </class> 
</hibernate-mapping> 

3、POJO實體類

public class City {
    private int id;
    private String name;
    private int level;

    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 int getLevel() {
        return level;
    }
    public void setLevel(int level) {
        this.level = level;
    }
}

4、

    public static void main(String a[])
    {
        City l = new City();
        l.setId(10);
        l.setName("foshan");
        //l.setLevel(1);
        l.setLevel(1);

        City l1 = new City();
        l1.setId(11);
        l1.setName("dongguang");
        l1.setLevel(1);

        City l0 = new City();
        l0.setId(12);
        l0.setName("zhongshan");
        l0.setLevel(1);

        //Transaction tx = session.beginTransaction();
        //tx.commit();

        Configuration cfg = new Configuration();//配置
        SessionFactory sf = cfg.configure().buildSessionFactory();//會話工廠
        Session session = sf.openSession();//具體會話,包括一次事務的操作
        session.beginTransaction(); //事務
        session.save(l0); //add element
        session.save(l1);
        session.save(l);

        //session.update(l1);
        //session.update(l);
        session.getTransaction().commit();//提交執行事務
        session.close();
        sf.close();
        System.out.print("success!");
    }

5、運行工程,Run As => Java Application
Eclipse Console 運行輸出信息:
這裏寫圖片描述

Mysql數據庫運行、更新:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章