Hibernate学习之Hibernate开发环境搭建
程序员文章站
2022-07-05 23:21:13
Hibernate学习之Hibernate开发环境搭建
一、下载Hibernate包的下载
官网地址:http://hibernate.org/orm/
下载版本:hibernate-r...
Hibernate学习之Hibernate开发环境搭建
一、下载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开发环境
新建javaproject工程。新建libs文件夹,将hibernate中required的所有文件复制到java工程的libs文件夹中,将所连接的数据库驱动包也复制到文件夹下。将hibernate中project下etc下的hibernate.cfg.xml和log4j.properties复制到SRC根目录下。?
五、测试代码
1、 创建学生表
hibernate.cfg.xml配置文件(hibernate的核心配置文件,hibernate只加载这个配置文件)
com.mysql.jdbc.Driver jdbc:mysql://127.0.0.1:3306/test root root
org.hibernate.dialect.MySQL5Dialect
?
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、实体映射文件
?
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(); } } }
上一篇: 从三层架构到MVC