框架 Hibernate

Hibernate

在test01右键新建其他找到hibernate文件夹下的Hibernate Configuration File(cfg.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">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  再点最后一个文件就全选了。

posted @ 2016-11-07 20:35  hanruyue  阅读(215)  评论(0编辑  收藏  举报