一、下载Hibernate包的下载
官网地址:http://hibernate.org/orm/
下载版本:hibernate-release-4.3.11.Final
二、Hibernate jar包介绍
包名 | 用途 |
hibernate-core-4.3.11.Final.jar | Hibernate核心包 |
antlr-2.7.7.jar | Hibernate利用它实现从HQL到SQL的转换 |
dom4j-1.6.1.jar | 解析XML配置文件和XML映射文件 |
hibernate-commons-annotations-4.0.5.Final.jar | Hibernate注解包 |
hibernate-jpa-2.1-api-1.0.0.Final.jar | JPA2.1接口库 |
jandex-1.1.0.Final.jar | 用来索引Anotation的 |
javassist-3.18.1-GA.jar | Hibernate用它来实现PO字节码的动态生成 |
jboss-logging-3.1.3.GA.jar | 日志服务通用库 |
jboss-logging-annotations-1.2.0.Beta1.jar | 实现带注释的借口的具体实现类 |
jboss-transaction-api_1.2_spec-1.0.0.Final.jar | JTA规范包 |
三、Hibernate核心配置文件
hibernate.cfg.xml:是Hibernate的核心配置文件,主要功能有:
四、搭建Hibernate开发环境
五、测试代码
1、 创建学生表
hibernate.cfg.xml配置文件
<!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> <!-- 配置数据库连接 --> <!-- Hibernate的方言:生成SQL语句的规则 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </property> <!-- 数据库驱动包 --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- 数据库URL地址 --> <property name="hibernate.connection.url"> jdbc:mysql://127.0.0.1:3306/test </property> <!-- 用户名 --> <property name="hibernate.connection.username">root</property> <!-- 密码 --> <property name="hibernate.connection.password">root</property> <!-- 是否输出SQL语句 --> <property name="show_sql">true</property> <!-- 是否格式化SQL语句 --> <property name="format_sql">true</property> <!-- 如果对象有更新则重新生成SQL表 create:重新生成 update:更新 --> <property name="hbm2ddl.auto">update</property> <!-- 注册实体映射文件 --> <mapping resource="com/lxit/entity/Student.hbm.xml" /> </session-factory> </hibernate-configuration>
2、学生类
package com.lxit.entity; import java.io.Serializable; public class Student implements Serializable{ public Student(){} /** * */ private static final long serialVersionUID = 1L; public Student(String loginId, String loginPwd, String realName, String phone, String address, String email) { super(); this.loginId = loginId; this.loginPwd = loginPwd; this.realName = realName; this.phone = phone; this.address = address; this.email = email; } private String loginId; private String loginPwd; private String realName; private String phone; private String address; private String email; public String getLoginId() { return loginId; } public void setLoginId(String loginId) { this.loginId = loginId; } public String getLoginPwd() { return loginPwd; } public void setLoginPwd(String loginPwd) { this.loginPwd = loginPwd; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public static long getSerialversionuid() { return serialVersionUID; } }
3、实体映射文件
<?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.lxit.entity.Student" table="Student"> <id name="loginId" column="loginId"> <generator class="assigned"></generator> </id> <property name="loginPwd" column="loginPwd"></property> <property name="realName" column="realName"></property> <property name="phone" column="phone"></property> <property name="address" column="address"></property> <property name="email" column="email"></property> </class> </hibernate-mapping>
4、测试类
package com.lxit.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test; import com.lxit.entity.Student; public class StudentTest { //@Test public void createTable(){ Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); export.create(true, true); //创建表,在控制台输出SQL语句 } @Test public void add(){ Configuration cfg = new Configuration().configure(); //hibernate3.0版本创建sessionFactory的方法 //SessionFactory factory = cfg.buildSessionFactory(); //hibernate4.0版本创建sessionFactory方法 StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()); ServiceRegistry sr = ssrb.build(); SessionFactory factory = cfg.buildSessionFactory(sr); Session session = factory.openSession(); Transaction tran = session.beginTransaction(); Student stu = new Student("zhangsan", "123123", "张三", "12345678901", "广东深圳", "zhangsan@163.com"); try { session.save(stu); tran.commit(); System.out.println("保存成功!"); } catch (Exception e) { tran.rollback(); }finally{ session.close(); factory.close(); } } }