Hibernate学习笔记1
学习内容:
部署第一个Hibernate程序 |
配置Hibernate图
详细内容:
1.准备工作
点击"Windows" - "preferences" - "Java" - "Build Path" -
"User Libraries" - "New",建立一个用户库 "hibernate",导入包:
Hibernate_Home/hibernate3.jar |
Hibernate_Home/lib/*.jar |
再建立一个user libraries ,名字为"sqlServer JDBC",导入包:
SQLServer JDBC/lib/*.jar |
打开SQL Server 2000,建立数据库HibernateTest
2.建立项目 "Hibernate_01",导入上面配置的用户库
3.建立实体类,名字为"User.java",代码如下:
package entity;
import java.util.Date;
public class User { 字段值以及各个字段的set和get方法 private String id; private String name; private int age; private Date createTime; private Date expireTime;
public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getExpireTime() { return expireTime; } public void setExpireTime(Date expireTime) { this.expireTime = expireTime; } public String getId() { return id; } public void setId(String id) { this.id = id; } }
|
4.定义User类的映射文件
User.hbm.xml 映射文件与实体类放于同一个文件夹内
示例文件存放目录: Hibernate_Home/eg/org/hibernate/auction/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> <class name="entity.User" table="user_message"> <!-- 默认存放的表与实体类的名字一致 --> <id name="id" column="name_id"><!--此处定义的是主键 --> <!-- 将实体类中的"name"字段值,存放到User表中"username"字段中 --> <generator class="uuid"></generator> <!-- 生成策略唯一性 --> </id> <property name="name"></property> <property name="age"></property> <property name="createTime"></property> <property name="expireTime"></property> </class> </hibernate-mapping> |
5.创建Hibernate配置文件 hibernate.cfg.xml
为了便于调试同时加入log4j.properties配置文件(该文件不用修改),代码如下:
示例文件存放目录Hibernate_Home/etc/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驱动程序 --> <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property> <!-- 连接数据库URL --> <property name="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=HibernateTest</property> <!-- 连接数据库用户名 --> <property name="hibernate.connection.username">sa</property> <!-- 连接数据库密码 --> <property name="hibernate.connection.password">xingxing</property> <!-- 方言 --> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="entity/User.hbm.xml"/> </session-factory> </hibernate-configuration> |
6.生成一个工具类 testExport.java
将对象模型转换成关系模型
Configuration读取hibernate.cfg.xml文件 SchemaExport将对象类导成表 export.create(true,true); |
package test;
import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport;
public class testExport {
public static void main(String[] args) { //Configuration读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); //SchemaExport将对象类导成表 SchemaExport export = new SchemaExport( cfg ); //创建表 /* * Run the schema creation script. * * @param script print the DDL to the console 脚本打印DDL控制台 * @param export export the script to the database 出口的脚本的数据库 * public void create(boolean script, boolean export) { execute( script, export, false, false ); } */
export.create( true , true ); }
}
|
7.建立Client
向数据库中写入数据
创建SessionFactory: 一个SessionFactory对应于一个数据库 cfg.buildSessionFactory() 声明Session(位于org.hibernate.Session) |
代码: package test;
import java.util.Date;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
import entity.User;
public class Client { public static void main(String[] args) { Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = null; try{ session = factory.openSession(); //Auto Commit 默认值为false,所以需要手动提交事务 //开启事务 session.beginTransaction(); User user = new User(); user.setName( "第一条数据" ); user.setAge( 20 ); user.setCreateTime( new Date() ); user.setExpireTime( new Date() ); //存储数据 session.save( user ); //提交事务 //需要先得到原先开启事务的上下文,使用getTransaction(),再提交时事务 session.getTransaction().commit(); }catch( Exception e ){ e.printStackTrace(); //出现问题,回滚事务 session.getTransaction().rollback(); } finally{ if ( session != null ){ if ( session.isOpen()){ session.close(); } session = null; } if ( factory != null){ factory.close(); } } } }
|
8.查询数据,判断是否插入成功
打开SQL,在HibernateTest数据库,查询select * from user_message
9.完毕
注:user在数据库中为关键字,不能使用该单词作为表名