Hibernate是用來操作數據庫的,當然要結合數據庫來使用。但是公司的電腦是不能隨便安裝軟件的,即使是mysql的免安裝版,也要把驅動文件放到windows下,沒有權限是做不到的。因此推薦一個hsqldb數據庫,是java語言寫的。不需要安裝。下面介紹如何結合hibernate和hsqldb。
1、 下載hsqldb
將下載下來的文件解壓,文件結構如圖
打開demo文件夾
注意里面的幾個文件runManager.bat,runServer.bat,my_server.bat,runManagerSwing.bat
runServer.bat是來開啟數據庫服務,runManager.bat和runManagerSwing.bat是數據庫管理界面,可以在里面輸入sql語句來執行。注意my_server.bat是自己加進去的,內容是:
cd ../data
java -cp ../lib/hsqldb.jar org.hsqldb.Server -database.0 file:mydb -dbname.0 hibernate
hibernate是數據庫名,自己隨便寫,也可以寫成mydb等等
2、 啟動hsqldb
先執行my_server.bat,然后執行runServer.bat,最后執行runManager.bat或者runManagerSwing.bat,本例中以runManager.bat為例,啟動界面,配置如圖
注意url中不要忘記hibernate這個數據庫名。
點擊ok,如圖,里面有個名字為user的表
Hsqldb就配置到這里。
3、 下面hibernate的配置,首先自己建一個user library,把hibernate所有的必須的jar包都添加進去,在hibernate文件夾中有,自己添加一下。
然后新建一個java項目hibernateDemo,將剛才自己定義的庫加到工程里。配置到此結束。
4、 我們現在src文件下定義hibernate的配置文件hibernate.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="myeclipse.connection.profile">hsql</property>
- <property name="connection.url">jdbc:hsqldb:hsql://localhost/hibernate</property>
- <property name="connection.username">sa</property>
- <property name="connection.password"></property>
- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
- <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
- <property name="show_sql">true</property>
- <property name="connection.autocommit">true</property>
- <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
- <mapping resource="com/zhoubo/hibernate/User.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
5、 在文件夾src下定義一個package com.zhoubo.hibernate 在里面新建一個User類,如下
- package com.zhoubo.hibernate;
- public class User {
- private String id;
- private String password;
- private char sex;
- private int age;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public char getSex() {
- return sex;
- }
- public void setSex(char sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
在里面定義一個User類的配置文件User.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>
- <class name="com.zhoubo.hibernate.User" table="User">
- <id name="id" column="User_id">
- <generator class="uuid"/>
- </id>
- <property name="password"/>
- <property name="sex"/>
- <property name="age"/>
- </class>
- </hibernate-mapping>
并且在hibernate.cfg.xml中來注冊這個user.hbm.xml,也就是我們在hibernate.cfg.xml中看到的
<mapping resource="com/zhoubo/hibernate/User.hbm.xml" />
下面我們就來通過hibernate來在數據庫生成user表,定義一個DBExport類,如下
- package com.zhoubo.hibernate;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- public class DBExport {
- public static void main(String ...arg){
- Configuration cfg = new Configuration().configure();
- SchemaExport export = new SchemaExport(cfg);
- export.create(true, true);
- }
- }
可以通過desc user來查詢表的屬性,可以看到表已經生成了。
下面我們來在表中插入數據,定義一個hibernateUtil類和Client類,如下
- package com.zhoubo.hibernate;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtils {
- private static SessionFactory factory ;
- static {
- try{
- Configuration cfg = new Configuration().configure();
- factory = cfg.buildSessionFactory();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- public static Session getSession(){
- return factory.openSession();
- }
- public static SessionFactory getSessionFactory(){
- return factory;
- }
- public static void closeSession(Session session){
- session.close();
- }
- }
- package com.zhoubo.hibernate;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- public class Client {
- public static void main(String ...args){
- User user = new User();
- Session session = null;
- Transaction tx = null;
- try{
- session = HibernateUtils.getSession();
- tx = session.beginTransaction();
- user.setAge(18);
- user.setPassword("hello");
- user.setSex('m');
- session.save(user);
- tx.commit();
- }catch(Exception e){
- e.printStackTrace();
- tx.rollback();
- }finally{
- HibernateUtils.closeSession(session);
- }
- }
- }
通過查詢,我們可以看到插入的值。