Hibernate的初次使用

时间:2021-12-31 15:29:56
使用hibernate的四个步骤:
第一:创建一个hibernate.cfg.xml。
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--声明Hibernate配置文件的开始-->
<hibernate-configuration>
<session-factory>
<!--配置链接数据库的基本属性-->
<property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=Book</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <!--输出sql语句-->
<property name="show_sql">true</property> <!--数据库使用的sql方言-->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <!--是否格式化sql语句-->
<property name="format_sql">true</property> <!--指定自动生成数据表的策略-->
<!--<property name="hbm2ddl.auto">update</property>--> <!--指定需要关联的映射文件-->//注意在最后要加上这个关联的文件,*.hbm.xml文件。
<mapping resource="NewSpringDAO/Book2.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration> 第二:写一个持久化类
1:必须有一个无参的构造器。
2:非final类
3:set,get方法。
public class Book2 { private int id;
private String name;
private double price;
private String author;
private int bookCount; public Book2(){} public Book2(int id,String name,double price,String author,int bookCount){
this.id=id;
this.name=name;
this.price=price;
this.author=author;
this.bookCount=bookCount;
} public void setId(int id){this.id=id;}
public void setName(String name){this.name=name;}
public void setPrice(double price){this.price=price;}
public void setAuthor(String author){this.author=author;}
public void setBookCount(int bookCount){this.bookCount=bookCount;} public int getId(){return id;}
public String getName(){return name;}
public double getPrice(){return price;}
public String getAuthor(){return author;}
public int getBookCount(){return bookCount;} @Override
public String toString() {
return " "+this.id+" "+this.name+" "+this.price+" "+this.author+" "+this.bookCount;
}
} 第三:在类路径下创建对应的*.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="NewSpringDAO.Book2" table="Bookimformation">
<!--主键的方式用id标签。-->
<id name="id" column="id" type="java.lang.Integer">
<!--<generator class="native"></generator>--><!-- 这个标签用来定义主键生成策略。
有这个标签的时候插入会抛出异常,因为表时我自己建的,并不是由Hibernate自动生成的!-->
</id> <property name="name" column="name" type="java.lang.String" not-null="true">
</property> <!--name属性为javaBean相对应的字段,column属性为数据库中的数据表对应的列名,type为数据库中的字段的类型-->
<property name="author" column="author" type="java.lang.String" not-null="true">
</property> <property name="price" column="price" type="java.lang.Double" not-null="true">
</property> <property name="bookCount" column="bookCount" type="int" not-null="true">
</property>
</class>
</hibernate-mapping> 第四步:
在我的创建的一个HibernateUtil的类的前提下,使用hibernate的API。
public class HibernateUtil {
private static final ThreadLocal<Session>threadLocal=new ThreadLocal<Session>(); private static SessionFactory sessionFactory=null; static {
try {
Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();//你麻痹你就不能写个类把这些方法包装起来!!! sessionFactory=configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) {
System.out.println("创建会话工厂失败!");
e.printStackTrace();
}
} public static Session getSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if (session==null||!session.isOpen()) {
if (sessionFactory == null)
rebuildSessionFactory();
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
} public static void rebuildSessionFactory(){
try {
Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();//你麻痹你就不能写个类把这些方法包装起来!!! sessionFactory=configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) {
System.out.println("创建会话工厂失败!");
e.printStackTrace();
}
} public static SessionFactory getSessionFactory(){
return sessionFactory;
} public static void closeSession() throws HibernateException{
Session session=(Session)threadLocal.get();
threadLocal.set(null);
if (session!=null)
session.close();
}
}
具体使用hibernate的API,参照包Hibernate