1. 首先在classPath下加一个配置文件,hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!--</property name="current_session_context_class">thread</property>-->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--<propertyname="hbm2ddl.auto">update</property> -->
<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
<mapping class="com.bjsxt.hibernate.Teacher"/>
</session-factory>
</hibernate-configuration>
2. .编写实体和表的映射文件,Student.hbm.xml,一般和model放在一个包下。
3. hibernate使用示例:
publicstaticvoid main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("zhangsan");
s.setAge(10);
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
4. 因为sessionFactory在一个项目中创建一个就够了,而且代码比较固定,所以建议写一个创建sessionFactory的工具类:
publicclass HibernateUtil {
privatestaticfinal SessionFactory sessionFactory = buildSessionFactory();
privatestatic SessionFactorybuildSessionFactory() {
try {
returnnewConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
thrownew ExceptionInInitializerError(ex);
}
}
publicstatic SessionFactory getSessionFactory() {
returnsessionFactory;
}
}
5. 用Annotation时有几个不同:
1) 不用再写实体类和表的映射文件了,直接在实体类上注解。
2)hibernate.cfg.xml中的<mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>要改写成
<mapping class="com.bjsxt.hibernate.Teacher"/>
3)创建SessionFactory是,要创建Annotation专门的:
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
6. sessionFactory就是产生session的,可以理解为就是一个数据库连接池,sessionFactory最主要的功能就是维护者一个数据库连接池。
7. sessionFactory创建session时,有两种方法:
1) sessionFactory.openSession():永远是新穿件一个session,用完后必须手工将session关闭,session.close().
2) sessionFactory getCurrentSession():从当前环境拿一个的session,没有才创建一个新的session,不用手工去关闭session,事务提交后自动close。当前环境是什么呢?是在hibernate.cfg.xml里配置的,</propertyname="current_session_context_class">thread</property>,最常用的就是thread,就是说在当前线程中找session找到就拿来用,找不到就建一新的。
8. 对象有三种状态:瞬时的,持久化的,托管的。
9. 增删改查都怎么用:
1) 删除:session.delete(student);
2) 保存:session.save(student);
3) 查询:session.load(Student.class,1)
Session.get(Student.class,1)
Get回马上查询数据库并返回对象,而load时不会直接查数据库,只有在用到对象时才会真正查数据库返回对象。
4) 修改:session.update(student);
5) 保存或修改:session.saveOrUpdate(student)
10. session.clear()是用于清除session缓存的。
11. session.flush()强制将缓存和数据库做同步。Session的flush时机是可以自己调的,
session.setFlushMode(FlushMode.COMMIT)默认是session提交时flush.