Hibernate配置流程
操作数据库必须要设置数据库的连接属性:
-
driver_class,url,username,password(hibernate.cfg.xml)
2. 编写对象跟表之间的映射关系(类名.hbm.xml)
为什么配置文件用xml?
①xml文件是可以通过规则文件提示的,
xml的规则文件有两种:dtd 和 schema 文件
②任何提供xml文件的框架,基本都提供规则文件
③大部分框架的XML规则文件都是在核心包里面的
④Eclipse可以通过配置XML的规则文件生成配置文件的。而且配置了规则文件,可以实现配置文件的标签提示
hibernate的配置流程图
第一步 、 在SRC目录下新建一个hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "hibernate-configuration-3.0.dtd" > <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sms</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1234</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.formats_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <mapping resource="cn/gzsxt/hibernate/pojo/hbm/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
public class HibernateUtils { public static final SessionFactory ssf = getSessionFactory(); private static SessionFactory getSessionFactory() { //读取配置文件 Configuration config = new Configuration().configure(); //新建会话工厂 SessionFactory sessionFactory = config.buildSessionFactory(); return sessionFactory; } public static Session getSession() { Session session = ssf.openSession(); return session; } }
本文作者:___mouM
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。
版权说明:本文版权归作者和博客园共有,欢迎转载。但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.