首页 > 代码库 > Hibernate学习笔记

Hibernate学习笔记

 

Hibenate开发流程:

  1:添加相关jar包;

  2:src下增加hibernate.cfg.xml(文件内容如下);

 1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5  6 <!-- Generated by MyEclipse Hibernate Tools.                   --> 7 <hibernate-configuration> 8  9     <session-factory>10     <!-- dialect 制定数据库的方言(不同数据库的SQL不同)11         Oracle org.hibenate.dialect.Oracle9Dialect12         MySql  org.hibenate.dialect.MySQLDialect13         SQLServer org.hibenate.dialect.SQLServerDialect14     15      -->16         <property name="dialect">17             org.hibernate.dialect.MySQLDialect18         </property>19         20     <!--  指定链接数据库的URL -->21         <property name="connection.url">22             jdbc:mysql://localhost:3306/testhib23         </property>24     <!-- 链接书库的用户名和密码 -->25         26         <property name="connection.username">root</property>27         <property name="connection.password">root</property>28         29     <!--  指定数据库的JDBC驱动类-->30         <property name="connection.driver_class">31             com.mysql.jdbc.Driver32         </property>33 34         35     <!--  hbm2ddl的值有:36         create-drop:运行时创建,运行完删除37         create:每次运行都会删除已有的,在创建。测试时,使用create38         update:映射文件和表。只会更新表中的数据39         validate:查看映射文件和表是不是对应,如果不对应,会报错40         41     -->42         <property name="hbm2ddl.auto">update</property>43         44     <!-- show_sql hibenate以日志的形式输出语句 -->45         <property name="show_sql">true</property>46     47     <!--  告之对象的映射关系-->48         <mapping resource="User.hbm.xml" />49 50     </session-factory>51 52 </hibernate-configuration>
View Code

 

  3:增加pojo和对应的映射文件;

  4:增加log4j配置(可选,设置调试信息和打印级别);

  5:开发程序。

    a:UibUtil

 1 import org.hibernate.Session; 2 import org.hibernate.SessionFactory; 3 import org.hibernate.cfg.Configuration; 4  5  6 public class HibUtil { 7     private static SessionFactory factory; 8      9     private HibUtil(){10         11     }12     13     static {14         Configuration configuration = new Configuration();15         //configuration.configure("hibernate.cfg.xml");16         configuration.configure();17         factory = configuration.buildSessionFactory();18         19 20     }21     22     public static SessionFactory getFactory(){23         return factory;24     }25     26     public static Session getSession(){27         return factory.openSession();28     }29 }
View Code

    b:Test.java

 1 import java.util.Date; 2  3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7  8  9 public class Test {10 11     /**12      * @param args13      */14     public static void main(String[] args) {15         // TODO Auto-generated method stub16         /*17         Configuration conf = new Configuration().configure();18         SessionFactory factory = conf.buildSessionFactory();19         Session session = factory.openSession();20         Transaction transaction = session.beginTransaction();21         User user = new User("xiao", new Date());22         session.save(user);23         transaction.commit();24         session.close();25         */26         27         Session session = null;28         Transaction tx = null;29         try{30             session = HibUtil.getSession();31             tx = session.beginTransaction();32             User u = (User) session.get(User.class, 5);//取得对象33             u.setName("习大大");//修改属性34             //session.delete(u);删除对象35             session.update(u);//写入数据库36             tx.commit();                                                            37         }finally{38             if(session != null){39                 session.close();40             }41         }42     }43     44 45 }
View Code