已经好久没有使用Hibernate了,最近看了下Hibernate的官网,已经有5.0的开发版了,大体看了下目录结构,很多还是引用的上个版本的,也就是最新的稳定版本4.3.8.
这里螃蟹就以 Hibernate 4.3.8为例来介绍第一个例子。
首先我们创建一个项目,类似结构如下:
![](https://i-blog.csdnimg.cn/blog_migrate/86096a3e09eb5c57b2aa7676dad8fd68.png)
1、引用jar包
相关jar包在官方下载的hibernate中的lib下的文件夹中,可根据需要来添加,下图是螃蟹暂时用到的部分
![](https://i-blog.csdnimg.cn/blog_migrate/bfe4d89f3ac0ab12f51e5c136ab545d9.png)
2、创建一个javabean
3、创建 Hibernate映射文件( User.hbm.xml )
这里有几个简单的说明:
a、由于这是User的映射文件,那么package需要指定User所在的目录
b、class中name指的是类名,table对应的是数据库的表名
c、class的子标签中,name对应javabean中的类属性,column对应数据库的字段
4、Hibernate核心配置文件(hibernate.cfg.xml)
这个文件按本篇中创建的项目的目录结构来说,放在src目录下。
配置文件中主要使一些数据库配置,以及还有第3步的映射文件,注释看起来 也比较简单,就不用多说了。
5、创建一个Hibernate辅助类
通过这个辅助类我们就可以加载第4步的核心配置文件了,至于为什么,只要看一下源码就可以了。
下图是上述代码31行 Configuration cfg = new Configuration().configure(); 的源码解读:
![](https://i-blog.csdnimg.cn/blog_migrate/fe7c56534e40f6cfc4b5223575b7efb6.png)
到这里就可以看到hibernate已经实现了加载该文件的默认方法实现了。
6、编写测试类
执行后,以下是打印日志:
![](https://i-blog.csdnimg.cn/blog_migrate/c6da1d401cf7187e6584504f4421c13e.png)
再次查看数据库数据:
![](https://i-blog.csdnimg.cn/blog_migrate/3fb227189180cd73cebfd8e3e3c825c4.png)
这样,我们的第一个例子就运行成功了。
这里螃蟹就以 Hibernate 4.3.8为例来介绍第一个例子。
首先我们创建一个项目,类似结构如下:
![](https://i-blog.csdnimg.cn/blog_migrate/86096a3e09eb5c57b2aa7676dad8fd68.png)
1、引用jar包
相关jar包在官方下载的hibernate中的lib下的文件夹中,可根据需要来添加,下图是螃蟹暂时用到的部分
![](https://i-blog.csdnimg.cn/blog_migrate/bfe4d89f3ac0ab12f51e5c136ab545d9.png)
2、创建一个javabean
- package com.itxxz.manager.user.entity;
- /**
- *
- * @author: IT学习者-螃蟹
- *
- * @官网 www.itxxz.com
- *
- * @version: 2015年4月14日 下午10:04:36
- */
- public class User {
- private Integer id;
- private String username;
- private String password;
- private String email;
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public java.lang.Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public java.lang.String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- }
3、创建 Hibernate映射文件( 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 package="com.itxxz.manager.user.entity">
- <class name="User" table="user">
- <id name="id" column="id" type="integer">
- <generator class="native"/>
- </id>
- <property name="username" column="username" type="string" />
- <property name="password" column="password" type="string" />
- <property name="email" column="email" type="string" />
- </class>
- </hibernate-mapping>
这里有几个简单的说明:
a、由于这是User的映射文件,那么package需要指定User所在的目录
b、class中name指的是类名,table对应的是数据库的表名
c、class的子标签中,name对应javabean中的类属性,column对应数据库的字段
4、Hibernate核心配置文件(hibernate.cfg.xml)
- <?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">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://127.0.0.1:3306/itxxz</property>
- <property name="connection.username">root</property>
- <property name="connection.password">root</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>
- <mapping resource="com/itxxz/manager/user/dao/User.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
这个文件按本篇中创建的项目的目录结构来说,放在src目录下。
配置文件中主要使一些数据库配置,以及还有第3步的映射文件,注释看起来 也比较简单,就不用多说了。
5、创建一个Hibernate辅助类
- /**
- * 文件名:HibernateUtil.java
- *
- * 日期:2015年4月14日
- *
- */
- package com.itxxz.util;
- import org.hibernate.SessionFactory;
- import org.hibernate.boot.registry.StandardServiceRegistry;
- import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
- import org.hibernate.cfg.Configuration;
- /**
- *
- * @author: IT学习者-螃蟹
- *
- * @官网 www.itxxz.com
- *
- * @version: 2015年4月14日 下午10:48:41
- */
- public class HibernateUtil {
- private static final SessionFactory sessionFactory = buildSessionFactory();
- private static SessionFactory buildSessionFactory() {
- try {
- // Create the SessionFactory from hibernate.cfg.xml
- Configuration cfg = new Configuration().configure();
- StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
- .applySettings(cfg.getProperties()).build();
- return cfg.buildSessionFactory(serviceRegistry);
- } 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;
- }
- }
通过这个辅助类我们就可以加载第4步的核心配置文件了,至于为什么,只要看一下源码就可以了。
下图是上述代码31行 Configuration cfg = new Configuration().configure(); 的源码解读:
![](https://i-blog.csdnimg.cn/blog_migrate/fe7c56534e40f6cfc4b5223575b7efb6.png)
到这里就可以看到hibernate已经实现了加载该文件的默认方法实现了。
6、编写测试类
- package com.itxxz.test;
- import java.util.Date;
- import org.hibernate.Session;
- import com.itxxz.manager.user.entity.User;
- import com.itxxz.util.HibernateUtil;
- /**
- *
- * @author: IT学习者-螃蟹
- *
- * @官网 www.itxxz.com
- *
- * @version: 2015年4月14日 下午10:51:30
- */
- public class DemoTest {
- public static void main(String[] args) {
- DemoTest test = new DemoTest();
- test.addUser();
- HibernateUtil.getSessionFactory().close();
- }
- private void addUser() {
- Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- session.beginTransaction();
- User user = new User();
- user.setId(2);
- user.setUsername("IT学习者-螃蟹");
- user.setPassword("123");
- user.setEmail("test@itxxz.com");
- session.save(user);
- session.getTransaction().commit();
- }
- }
执行后,以下是打印日志:
![](https://i-blog.csdnimg.cn/blog_migrate/c6da1d401cf7187e6584504f4421c13e.png)
再次查看数据库数据:
![](https://i-blog.csdnimg.cn/blog_migrate/3fb227189180cd73cebfd8e3e3c825c4.png)
这样,我们的第一个例子就运行成功了。