1.引入Hibernate包
hibernate3.jar 核心包
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-validator.jar
antlr-2.7.6.jar
cglib-2.2.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
javassist-3.9.0.GA.jar
jstl-1.2.jar
jta-1.1.jar
log4j-1.2.14.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
2.编写Hibernate核心配置文件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>
<property name="myeclipse.connection.profile">sql</property><!--别名-->
<property name="connection.url"><!--连接字符串-->
jdbc:sqlserver://127.0.0.1:1433;databaseName=mybookshop
</property>
<property name="connection.username">sa</property><!--用户名-->
<property name="connection.password"></property><!--密码-->
<property name="connection.driver_class"><!--驱动-->
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="dialect"><!--方言(不同的数据库都有一些特殊的命令)-->
org.hibernate.dialect.SQLServerDialect
</property>
<property name="show_sql">true</property><!--在日志中打印SQL语句-->
<property name="format_sql">true</property><!--在日志中打印SQL语句带格式化处理-->
</session-factory>
</hibernate-configuration>
3.编写Java Bean对应数据中的实体(需要实现Serializable(序列化)接口)
package com.haha.bean;
import java.io.Serializable;
public class UserRoles implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.编写ORM中的M(Mapping影射配置文件) UserRoles.bhm.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 schema="dbo">
<class name="com.haha.bean.UserRoles" table="UserRoles">
<id column="Id" name="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property column="Name" name="name" type="string"/>
</class>
</hibernate-mapping>
id:主键
4.1主键策略
1、identity:用于DB2,MySQL, MS SQL Server, Sybase数据库。特点:递增
2、sequence:用于DB2,PostgreSQL, Oracle, SAP DB, McKoi数据库
3、native:跨数据库时使用,由底层方言产生
4、hilo:通过高低位合成id,先建表hi_value,再建列next_value。必须要有初始值。
5、sequencehilo:同过高低位合成id,建一个sequence序列,不用建表。
6、assigned:用户自定义id;
7、foreign:用于一对一关系共享主健时,两id值一样。
<property column="Name" name="name" type="string"/> 其它属性
5.向Hibernate.cfg.xml中添加 影射文件
<mapping resource="com/haha/bean/UserRoles.bhm.xml" />
6.编写测试类
package com.haha.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.haha.bean.UserRoles;
public class UserRolesTest {
/**
* @param args
*/
public static void main(String[] args) {
//1.获取SessionFactory
//通过配置类来读取Hibernate.cfg.xml文件
Configuration config=new Configuration().configure("/Hibernate.cfg.xml");
//获取Session对象
SessionFactory sessionFactory=config.buildSessionFactory();
Session session=sessionFactory.openSession();
//2.通过Session查询数据
//使用对象方式查询数据
//List<UserRoles> list= session.createCriteria(UserRoles.class).list();
//使用HQL查询命令查询
List<UserRoles> list= session.createQuery("from UserRoles").list();
for(UserRoles r :list){
System.out.println(r);
}
//3.关闭Session
session.close();
sessionFactory.close();
}
}