你的位置:首页 > Java教程

[Java教程]第一节 Hibernate 基本配置

1 新建maven工程

1)打开eclipse,依次点击File---->New---->Maven Project.

2)选择org.apache.maven.archetypes maven-archetype-quickstart选项,点击next.

3)输入Group Id: com.binyulan 输入Artifact Id: persistence.

4)点击Finish.

 

2 打开pom.

1)引入Junit,用于单元测试

<dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>4.10</version>  <scope>test</scope></dependency>

2)引入hibernate,用于持久化

<dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-core</artifactId>  <version>4.3.2.Final</version></dependency>

3)引入mysql,用于存储数据

<dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>5.1.38</version></dependency>

3 创建java端domain对象,并且编写映射文件,将domain对象中的字段与数据库中的字段一一对应

1)新建domain对象com.binyulan.domain.User

package com.binyulan.domain;import java.sql.Date;public class User {  private long id;  private String name;  private Date birthday;  public long getId() {    return id;  }  public void setId(long id) {    this.id = id;  }  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }  public Date getBirthday() {    return birthday;  }  public void setBirthday(Date birthday) {    this.birthday = birthday;  }}

View Code

2)编写映射文件User.hbm.

<??><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2016-6-28 11:38:36 by Hibernate Tools 3.5.0.Final --><hibernate-mapping>  <class name="com.binyulan.domain.User" table="user">    <id name="id" type="long">      <column name="id" />      <generator class="native" />    </id>    <property name="name" type="java.lang.String">      <column name="name" />    </property>    <property name="birthday" type="java.sql.Date">      <column name="birthday" />    </property>  </class></hibernate-mapping>

View Code

 4 创建hibernate配置文件,用于配置连接数据库信息,引入映射文件等。

<??><!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>    <!-- 数据库驱动名称 -->    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>    <!-- 数据库链接地址 -->    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/binyulan</property>    <!-- 数据库用户名称 -->    <property name="hibernate.connection.username">root</property>    <!-- 数据库密码 -->    <property name="connection.password">123456</property>    <!-- 设置数据库SQL语言类型 -->    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>    <!-- 设置是否显示SQL语句-->    <property name="show_sql">true</property>    <!-- 设置是否格式化SQL语句 -->    <property name="format_sql">true</property>    <!-- 自动建表,取值有四种:
create: 根据映射文件生成表, 每次执行都删除之前创建过的表
create-drop: 根据映射文件生成表,每次执行完毕都会删除表
update: 根据映射文件生成表,若以后表结构与映射文件不同,则更新表结构,但不会删除之前的记录,也不会删除映射文件中不存在的列
validate: 不会根据映射文件生成表,只会校验映射文件和表结构是否一致,不一致则抛异常
--> <property name="hbm2ddl.auto">update</property> <!-- 引入映射文件 --> <mapping resource="com/binyulan/domain/User.hbm./> </session-factory></hibernate-configuration>

 5 编写测试类

 

package com.binyulan.persistence1;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.binyulan.domain.User;public class SimpleTest {    private Session session;  private Transaction transaction;  @Before  public void init(){    Configuration cfg = new Configuration().configure();    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();    session = cfg.buildSessionFactory(serviceRegistry).openSession();    transaction = session.beginTransaction();  }  @After  public void destory(){    transaction.commit();    session.close();  }    @Test  public void testSave() {    try {      User user = new User();      user.setBirthday(new java.sql.Date(0));      user.setName("abc");      session.save(user);    } catch (HibernateException e) {      if (transaction != null) {        transaction.rollback();      }      throw e;    }  }}

View Code

 

 转载请标注出处,谢谢。