合理的操作应该是建立实体类来生成关系型数据表,然后根据操作对象来操作数据表,这里的数据表对于开发者而言是绝对的透明的。
Hibernate轻量级框架的使用就是为了提高开发效率,使用操作对象的方式来达到操作关系型数据库,正真的达到万物皆对象。
下面是我采用手工的方式从正面使用Hibernate。
1.创建Hibernate项目工程,即JavaWeb工程或者Java工程
2.创建自定义类库,添加工程中要使用到的Jar包
参见:
3.创建POJO即java实体类
- package xiao.zhang.hbean;
- import java.util.Date;
- public class Person {
- private String perId;
- private String perName;
- private Date creatTime;
- public Person() {
- super();
- }
- public Person(String perId, String perName, Date creatTime) {
- super();
- this.perId = perId;
- this.perName = perName;
- this.creatTime = creatTime;
- }
- //getter() setter()
- }
4.创建实体类Person的配置文件,Person.hbm.xml
放在实体类的目录下; 到Hibernate的实例工程中拷贝模版修改即可.
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.hibernate.auction">
- <class name="xiao.zhang.hbean.Person" table="Person" lazy="true" schema="dbo" catalog="ext">
- <id name="perId">
- <generator class="uuid" />
- </id>
- <property name="perName">
- <column name="perNamee" length="20" ></column>
- </property>
- <property name="creatTime">
- <column name="creatTime"></column>
- </property>
- </class>
- </hibernate-mapping>
5.将Person.hbm.xml文件添加到hibernate.cfg.xml文件中的映射中。
在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">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="dialect">
- org.hibernate.dialect.SQLServerDialect
- </property>
- <property name="hibernate.connection.username">sa</property>
- <property name="hibernate.connection.password">sa</property>
- <property name="hibernate.connection.url">
- jdbc:sqlserver://localhost:1433;databaseName=ext
- </property>
- <property name="hibernate.connection.driver_class">
- com.microsoft.sqlserver.jdbc.SQLServerDriver
- </property>
- <property name="hibernate.show_sql">true</property>
- <mapping resource="xiao/zhang/hbean/Person.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
注意手工配置:添加相应的数据库方言。
并且将Person.hbm.xml文件的资源路径添加到mapping映射中去。
6.编写一个将Person类生成为数据库表的类
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- public class HBCreateTable {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Configuration cfg=new Configuration().configure();
- SchemaExport sexport=new SchemaExport(cfg);
- sexport.create(true, true);
- }
- }
Configuration().configure()默认加载hibernate.cfg.xml;如果有改动则需要在configure方法中传入资源路径字符串
运行后生成在数据库中生成Person表。生成数据库中的表名在Person.hbm.xml文件中定义。
程序执行的SQL语句如下:
- drop table ext.dbo.Person
- create table ext.dbo.Person (perId varchar(255) not null, perNamee varchar(20) null, creatTime datetime null, primary key (perId))
数据库中的情况如下图:
7.编写程序,对对象进行操作。
- import java.util.Date;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.Session;
- public class TestPeson {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Configuration cfg = new Configuration().configure();
- SessionFactory sf = cfg.buildSessionFactory();
- Session session = null;
- try {
- session = sf.openSession();
- session.beginTransaction();
- //**************************//
- //*************************//
- session.getTransaction().commit();
- } catch (Exception e) {
- session.getTransaction().rollback();
- if (session != null) {
- if (session.isOpen()) {
- session.close();
- }
- }
- }
- }
- }
基本的操作框架如上面的代码,下面的操作代码添加到//*******//这里//*******//
向数据库中Person表中添加20个Person对象
- for(int i=0; i<20; i++){
- Person p = new Person();
- p.setPerName("name_"+i);
- p.setCreatTime(new Date());
- System.out.println(p.toString());
- session.save(p);
- }
控制太打印输出的SQL语句为:
- Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_0]
- Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_1]
- Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_2]
- Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_3]
- ..........................................................
- Person [creatTime=Fri Jul 20 03:14:59 CST 2012, perId=null, perName=name_19]
上面是创建的每一个对象的信息
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
- Hibernate: insert into ext.dbo.Person (perNamee, creatTime, perId) values (?, ?, ?)
一共有20条向数据库插入数据的操作。
数据库的显示: