Hibernate学习笔记:IDEA下Maven工程使用Hibe
开发步骤
1、使用IDEA创建Maven工程;
2、添加MySQL和Hibernate依赖,并通过IDEA生成Hibernate配置文件hibernate.cfg.xml;
3、通过IDEA生成持久化类和对象-关系映射文件*.hbm.xml;
4、通过Hibernate API编写访问MySQL数据库的代码。
创建Maven工程
创建步骤如图所示:





添加依赖,并生成hibernate.cfg.xml配置文件
在pom.xml文件中添加Hibernate、MySQL、Junit三个依赖:
<dependencies>
<!-- Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.1.Final</version>
</dependency>
<!-- MySQL数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
<!-- Junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
通过IDEA生成Hibernate.cfg.xml配置文件步骤如图:






此时要在hibernate.cfg.xml配置文件中写入配置信息:

代码如下,其中connection.username
是MySQL的登录账户名,connection.password
是密码,我这里账户名是root,密码是123。
<session-factory>
<!-- MySQL登录账户名 -->
<property name="connection.username">root</property>
<!-- 账户密码 -->
<property name="connection.password">123</property>
<!-- 一些属性信息 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 指定在程序运行时在控制台输出SQL语句 -->
<property name="show_sql">true</property>
<!-- 输出的SQL语句格式化 -->
<property name="format_sql">true</property>
<!-- 运行时在数据库自动生成数据表,这里选择update -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
使用IDEA生成持久化类及*.hbm.xml文件
在MySQL数据库中建立students数据库并新建student表,如图:

将id列的自增勾选上:

在IDEA中生成持久化类、*.hbm.xml文件:




进入Import Database Schema界面,Choose Data Source项选择@localhost后可以看到MySQL中的数据库以及其中的表信息,勾选student、age、id、lastName。Package是存放持久化类StudentEntity和映射文件StudentEntity.hbm.xml的包,需要提前建好,我这里在java下建的包名是test。勾选Add to Session Factory和Generate Separ...后单击OK即可生成持久化类和映射文件。


此时需要:
- 将StudentEntity.hbm.xml文件移至resources文件夹下,否则配置文件hibernate.cfg.xml会找不到它,运行时会报错;
- 更改StudentEntity.hbm.xml文件中id的generator,填写class为native。

再回到hibernate.cfg.xml文件中可以看到IDEA已经帮我们填写好了映射文件的地址:

通过Hibernate API编写访问数据库代码
新建HibernateTest类进行测试:
public class HibernateTest {
@Test
public void Test(){
//SessionFactory是生成Session的工厂
SessionFactory sessionFactory=null;
//Configuration类负责管理Hibernate的配置信息
Configuration configuration=new Configuration().configure();
//Hibernate4之后新增ServiceRegistry接口,所有基于Hibernate 的配置都必须统一向这个ServiceRegistry注册后才能生效
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
//生成SessionFactory类
sessionFactory=configuration.buildSessionFactory(serviceRegistry);
//生成Session类
Session session=sessionFactory.openSession();
//事务
Transaction transaction=session.beginTransaction();
//新建一个StudentEntity实例,将它插入数据库
StudentEntity studentEntity=new StudentEntity();
studentEntity.setId(1);
studentEntity.setAge(20);
studentEntity.setLastName("乔峰");
//Session的save操作将这个实例从临时状态变为持久化状态
session.save(studentEntity);
//提交事务
transaction.commit();
//关闭操作
session.close();
sessionFactory.close();
}
}
运行结果后如图,输出了一条SQL的插入语句。

查看数据库,已经将刚才的实例成功插入了数据库。
