框架 Hibernate

Hibernatejava

在test01右鍵新建其餘找到hibernate文件夾下的Hibernate Configuration File(cfg.xml)sql

 

<?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">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.password">123456</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
  <property name="hibernate.connection.username">test0816</property>
 <!--數據庫方案  -->
  <property name="hibernate.default_schema">TEST0816</property>
 <!--數據庫方言 -->
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
 <!--調試--> 
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
 <!--自動建表方式  --> 
  <property name="hibernate.hbm2ddl.auto">update</property>
 <!--映射文件  -->     
      <mapping resource="com/hanqi/entity/User.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

 

package com.hanqi.test;

import static org.junit.Assert.*;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;

public class Test01 {
    
    //測試Hibernate鏈接
    
    @Test
    public void test() {
        //1獲取配置文件
        Configuration cfg=new Configuration().configure();
        //2註冊配置
        ServiceRegistry sr=new StandardServiceRegistryBuilder()
                .applySettings(cfg.getProperties()).build();
        //3獲取SessionFactory  (至關於jdbc的鏈接connection) 
        SessionFactory sf=cfg.buildSessionFactory(sr);
        System.out.println(sf);
        sf.close();
        
    }

}
package com.hanqi.entity;

import java.util.Date;

//持久化類         實體化類
//不須要使用final終態
public class User {
    
    private int userID;
    private String userName;
    private Date birthday;
    private double money;
    private String password;
    
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getUserID() {
        return userID;
    }
    public void setUserID(int userID) {
        this.userID = userID;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    
    public User(int userID, String userName, Date birthday, double money, String password) {
        super();
        this.userID = userID;
        this.userName = userName;
        this.birthday = birthday;
        this.money = money;
        this.password = password;
    }

    
    //必須包含無參的構造方法
    //由於須要用到反射        反射要求是構造無參實例
    public User() {
        super();
    }

    
    
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-11-7 14:38:39 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.hanqi.entity.User" table="T_USER">
        <id name="userID" type="int">
            <column name="USERID" />
            <generator class="native" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="USERNAME" length="20" not-null="true" unique="true"/>
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" sql-type="DATE" />
        </property>
        <property name="money" type="java.lang.Double">
            <column name="MONEY" length="8" scale="2" default="0"/>
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="10"/>
        </property>
    </class>
</hibernate-mapping>

 

lib下面的.jar文件  點擊第一個文件  再按shift  再點最後一個文件就全選了。數據庫

相關文章
相關標籤/搜索