操作数据库必须要设置数据库的连接属性:
  1. 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;
}
}

最新文章

  1. mysql的分区和分表
  2. 24. Swap Nodes in Pairs
  3. POJ 2151 Check the difficulty of problems
  4. If A wants to use B
  5. 原创:分享asp.net伪静态成目录形式iis如何设置
  6. 从AsyncTask中获取数据的问题
  7. 专为物联网开发的开源操作系统Contiki(转)
  8. 百度之星复赛Astar Round3
  9. android获取sdk更新
  10. css如此强大你知道吗
  11. Codeforces Round #257 (Div. 2) 题解
  12. 自己写的一个jQuery轮播插件
  13. [Direct2D1.1教程] Direct2D特效概览
  14. udp和tcp
  15. 通过自动回复机器人学Mybatis 笔记:接口式编程
  16. mysql 查询优化 ~ 优化基础补充
  17. 【C++】C++中typedef、auto与decltype的作用
  18. storage封装
  19. js 中格式化时间
  20. EF6+Sqlite连接字符串的动态设置

热门文章

  1. [转] Filezilla server设置指南及中文乱码、登录欢迎语问题解决
  2. RecyclerView 实现快速滚动 (转)
  3. 扩展和嵌入 Python 解释器 用 C 或 C++ 编写模块以使用新模块来扩展 Python 解释器的功能 定义新的函数\对象类型\方法。 将 Python 解释器嵌入到另一个应用程序中
  4. mybatis pagehelper 分页 失效
  5. Quartz Configuration Reference
  6. 写了一个具有future接口的rust测试代码
  7. darknet 的python接口使用
  8. expandablelistView 可展开的列表
  9. 【转】Python读取PDF文档,输出内容
  10. Spring cloud微服务安全实战-6-4权限控制改造