10.8 hibernate configuration SessionFactory
Configuration
配置管理类:主要管理配置文件的一个类
它拥有一个子类AnnotationConfiguration,也就是说:我们可以使用注解来代替XML配置文件来配置相对应的信息
configure方法
configure()方法用于加载配置文件
- 加载主配置文件的方法
- 如果指定参数,那么加载参数的路径配置文件
- 如果不指定参数,默认加载src/目录下的hibernate.cfg.xml
buildSessionFactory方法
buildSessionFactory()用于创建Session工厂
SessionFactory
SessionFactory-->Session的工厂,也可以说代表了hibernate.cfg.xml这个文件...hibernate.cfg.xml的就有<session-factory>
这么一个节点
openSession方法
创建一个Session对象
getCurrentSession方法
创建Session对象或取出Session对象
Session
Session是Hibernate最重要的对象,Session维护了一个连接(Connection),只要使用Hibernate操作数据库,都需要用到Session对象
通常我们在DAO层中都会有以下的方法,Session也为我们提供了对应的方法来实现!
public interface IEmployeeDao {
voidsave(Employee emp);
voidupdate(Employee emp);
Employee findById(Serializable id);
List<Employee> getAll();
List<Employee> getAll(String employeeName);
List<Employee> getAll(int index, int count);
voiddelete(Serializable id);
}