【Hibernate】helloworld項目建立

目的:本文只是記錄項目添加hibernate的過程,以便於隨時的使用

前提:爲了運行方便我的這個項目用的數據庫是hsqldb,開發工具eclipse

目標:用hibernate建立用戶表(userTable),並向表中插入一條用戶信息

下載

hibernate ---是一種“數據庫-對象”映射的解決方案,就是你只要寫一句SQL語句,它就自動把SQL語句的結果封裝成對象
        版本:hibernate-release-4.0.1.Final       

        官網地址:​​http://www.hibernate.org/
       下載地址:
​​http://www.hibernate.org/downloads

建立項目

1、建立java project,命名爲HibernateTest

2、 添加的jar文件是解壓後hibernate-release-4.0.1.Final\lib\required目錄下的jar文件

3、添加hsqldb.jar

4、src下建立hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE hibernate-configuration 
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
        <!-- 是否將運行期生成的SQL輸出到日誌以供調試 --> 
        <property name="show_sql">true</property> 
        <!-- SQL方言,這裏設定的是HSQL --> 
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <!-- JDBC驅動程序 --> 
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> 
        <!-- JDBC URL, "?useUnicode=true&characterEncoding=GBK" 表示使用GBK進行編碼 --> 
        <property name="connection.url"> jdbc:hsqldb:file:./db/RWQTable
        </property> 
        <!-- 設置數據庫中的表用hibernate自動建立 -->
   		<property name="hibernate.hbm2ddl.auto">create</property>
        <!-- 數據庫用戶名 --> 
        <property name="connection.username">sa</property> 
        <!-- 數據庫密碼 --> 
        <property name="connection.password"></property> 

        <!-- 指定User的映射文件   -->
        <mapping resource="com/rwq/test/UserTable.hbm.xml"/>         
    </session-factory> 
</hibernate-configuration> 
5、建立UserInfor實體

package com.rwq.test;

public class UserInfor {
	private long id;
	private String name;
	private boolean sex;
	private int age;
	private String address;
	private String pass;
	
	public UserInfor(){
		
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}
6、包com.rwq.test下建立UserInfor對應的建表文件UserTable.hbm.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>
	<!-- 如果上面寫成 hibernate-mapping package="com.rwq.test" 這下面的class中name直接寫類名即可 -->
	<!-- class的name屬性就是要映射的對象類,table數據庫對應的表名,id用於唯一標識該 對象, name屬性爲類裏的屬性名;column是數據庫裏的對應字段。generator子元素用於指定主鍵生成方式 -->
	<class name="com.rwq.test.UserInfor" table="UserTable"><!-- 如果表名跟類名一個樣的話,那麼table屬性可以不寫 -->
		<id name="id" column="id">
			<!-- 定義主鍵生成策略 -->
			<generator class="increment"></generator>
		</id>
		<!-- 映射用戶名屬性 -->
		<property name="name" column="name" length="100" not-null="true"></property>
		<!-- 映射性別屬性 -->
		<property name="sex" column="sex" not-null="true"></property>
		<!-- 映射用戶年齡屬性 -->
		<property name="age" column="age" not-null="true"></property>
		<!-- 映射用戶地址屬性 -->
		<property name="address" column="address"></property>
		<!-- 映射用戶密碼屬性 -->
		<property name="pass" column="pass" not-null="true"></property>
	</class>
</hibernate-mapping>
7、建立service文件進行測試

package com.rwq.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class LoginService {

	public static void main(String[] args) {
		UserInfor user = new UserInfor();
		user.setId(1l);
		user.setName("admin");
		user.setAge(12);
		user.setAddress("xinjanlu");
		user.setPass("123456");
		user.setSex(false);
		try {
			Configuration configuration = new Configuration();
			configuration.configure();
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
					.applySettings(configuration.getProperties())
					.buildServiceRegistry();
			SessionFactory sf = configuration
					.buildSessionFactory(serviceRegistry);
			Session session = sf.openSession();
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
			session.close();
			sf.close();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}

}
8、成功即可

發佈了90 篇原創文章 · 獲贊 107 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章