1 简介
Hsqldb是一个开放源代码的JAVA数据库,其具有标准的SQL语法和JAVA接口,它可以自由使用和分发,非常简洁和快速的。
HSqlDB非常适合在用于快速的测试和演示的Java程序中。
2 配置maven的pom.xml文件
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-search-orm</artifactId> <version>4.4.2.Final</version> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>2.3.0</version> </dependency> |
3 启动数据库的mvn命令
Mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/tutorial" |
4 启动数据库管理控制台的mvn命令
Mvn exec:java -Dexec.mainClass="org.hsqldb.util.DatabaseManager" |
5 Hibernate配置文件
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="connection.url">jdbc:hsqldb:hsql://localhost</property> <property name="connection.username">sa</property> <property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property>
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property>
<mapping class="fire.entity.Author" /> <mapping class="fire.entity.Book" /> </session-factory> </hibernate-configuration> |
6 测试下是否可以使用
hibernate工具类:
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
public static SessionFactory getSessionFactory() { return sessionFactory; }
} |
Hibernate测试方法(java bean自己创建吧):
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction(); // SchemaExport schemaExport = new SchemaExport(configuration); // schemaExport.create(true, true); // Author author = new Author(); // author.setName("是的"); // session.save(author); Query query = session.createQuery("from Author"); List<Author> list = query.list(); System.out.println(list.size());
session.getTransaction().commit();
} |