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); } } }
通过查询,我们可以看到插入的值。