2_Hibernate_HelloWorld 代码实现部分
2017-04-11 16:31
246 查看
准备工作:
1.导入hibernate需要的jar包:改目录下的
hibernate-release-4.2.4.Final\lib\required
2.导入数据库驱动包
Hibernate开发步骤
1. 创建 Hibernate 配置文件()
2. 创建持久化类(pojo)
3. 创建对象-关系映射文件
4. 通过 Hibernate API 编写访问数据库的代码
hibernate.cfg.xml
Person
Person.hbm.xml
HibernateTest
1.导入hibernate需要的jar包:改目录下的
hibernate-release-4.2.4.Final\lib\required
2.导入数据库驱动包
Hibernate开发步骤
1. 创建 Hibernate 配置文件()
2. 创建持久化类(pojo)
3. 创建对象-关系映射文件
4. 通过 Hibernate API 编写访问数据库的代码
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 配置链接数据库的基本信息 jdbc\:mysql\://127.0.0.1\:3306/itrusportal--> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate1</property> <!-- 配置 hibernate 的基本信息 --> <!-- hibernate 所使用的数据库方言 用了org.hibernate.dialect.MySQLInnoDBDialect之后,出现无法执行的问题: org.hibernate.exception.SQLGrammarException: could not execute statement ,改用org.hibernate.dialect.MySQLDialect之后正常了--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 执行操作时是否在控制台打印 SQL --> <property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定关联的 .hbm.xml 文件 :注意取的是路径--> <mapping resource="com/hgh/hibernate/helloworld/Person.hbm.xml"/> </session-factory> </hibernate-configuration>
Person
package com.hgh.hibernate.helloworld; import java.sql.Date; public class Person { private Integer id; private String name; private Date birthDate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public Person(String name, Date birthDate) { super(); this.name = name; this.birthDate = birthDate; } public Person() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", birthDate=" + birthDate + "]"; } }
Person.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-4-11 15:06:03 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.hgh.hibernate.helloworld.Person" table="PERSON" dynamic-insert="true"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <!-- 指定主键的生成方式, native: 使用数据库本地方式 --> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="birthDate" type="java.sql.Date"> <column name="BIRTHDATE" /> </property> </class> </hibernate-mapping>
HibernateTest
package com.hgh.hibernate.helloworld; import java.sql.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.Test; public class HibernateTest { @Test public void test() { System.out.println("test..."); //1. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = null; //1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息,使用了默认值,会去加载默认路径下的默认名称的配置文件 Configuration configuration = new Configuration().configure(); //4.0 之前这样创建 // sessionFactory = configuration.buildSessionFactory(); //2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象 //hibernate 的任何配置和服务都需要在该对象中注册后才能有效. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); //3). sessionFactory = configuration.buildSessionFactory(serviceRegistry); //2. 创建一个 Session 对象 Session session = sessionFactory.openSession(); //3. 开启事务 Transaction transaction = session.beginTransaction(); //4. 执行保存操作 Person person = new Person("hgh", new Date(new java.util.Date().getTime())); session.save(person); //5. 提交事务 transaction.commit(); //6. 关闭 Session session.close(); //7. 关闭 SessionFactory 对象 sessionFactory.close(); } }
相关文章推荐
- Hibernate中HelloWorld代码实现部分
- hibernate中的增删改查实现代码
- asp下tag的实现,简单介绍与部分代码
- hibernate如何实现部分字段更新
- c# ACCESS/SQL 部分实现代码
- Java SSH Hibernate通过代码实现从数据库中随机取出指定条数的随机数据
- 单线程和多线程访问数据库性能比较二实现代码部分(oracle 数据库)
- 代码开源(1)——数据库部分操作封装C#实现
- jQuery的实现原理的模拟代码 -2 数据部分
- [原创]《柔性字符串匹配》部分代码C实现
- gsm发短信代码实现部分
- 今天看了ACE 中Proactor实现部分的一些代码,做一些总结和分析
- hibernate中的增删改查实现代码
- 收藏,对比功能的JS部分实现代码
- jQuery的实现原理的模拟代码 -1 核心部分
- asp下tag的实现,简单介绍与部分代码
- mini-pascal部分实现的语法分析器,代码only
- 【转】用winpcap实现局域网DNS欺骗之三(代码部分及深入研究)
- string.h中库函数的实现代码(部分)
- 在hibernate中实现查找部分字段