hibernate总结

JDBC+DBUtils

hibernate:持久化层的对象关系映射的orm框架,自动生成SQL语句,自动执行,使用对象编程的思维操作数据库
操作对象,通过映射的关联操作,操作数据库的表结构
核心类:session,sessionFactory,Transaction,Query,Criteria,Configuration
session:表示的是一级缓存,用完就关闭
sessionFactory:不是轻量级的,一个数据库对应一个SF
Transaction,
Query,
Criteria,
Configuration
ORM
O:面向对象领域的Object
R:关系
M:映射Mapping
优点:对JDBC数据库的代码做了封装,简化了数据访问层的繁琐的重复代码
轻量级的框架

学习路线:
1.完成增删改查
2.学习一级缓存,事务管理,基本的查询
3.一对多,多对多的操作
4.基本查询和查询优化

    
    org.hibernate.dialect.MySQLDialect
    
    true
    
    update

properties文件配置:

数据库使用的驱动类

hibernate.connection.driver_class=com.mysql.jdbc.Driver

数据库连接串

hibernate.connection.url=jdbc:mysql://localhost:3306/db

数据库连接的用户名

hibernate.connection.username=user

数据库连接的密码

hibernate.connection.password=password

数据库使用的方言

hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect

是否打印SQL语句

hibernate.show_sql=true
javax.persistence.validation.mode=none

Configuration:默认加载SRC的hibernate.cfg.xml配置文件
propertier需手动加载

技术分析之Hibernate框架的快速入门


第一步:下载Hibernate5的运行环境

1. 下载相应的jar包等
    * http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/hibernate-release-5.0.7.Final.zip/download  

2. 解压后对目录结构有一定的了解

第二步:创建表结构

1. 建表语句如下
    Create database hibernate_day01;
    Use hibernate_day01;
    CREATE TABLE `cst_customer` (
      `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
      `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',
      `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
      `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',
      `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',
      `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',
      `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',
      `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',
      `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
      `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
      PRIMARY KEY (`cust_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;

第三步:搭建Hibernate的开发环境

1. 创建WEB工程,引入Hibernate开发所需要的jar包
    * MySQL的驱动jar包
    * Hibernate开发需要的jar包(资料/hibernate-release-5.0.7.Final/lib/required/所有jar包)
    * 日志jar包(资料/jar包/log4j/所有jar包)

第四步:编写JavaBean实体类

1. Customer类的代码如下:
    public class Customer {
        private Long cust_id;
        private String cust_name;
        private Long cust_user_id;
        private Long cust_create_id;
        private String cust_source;
        private String cust_industry;
        private String cust_level;
        private String cust_linkman;
        private String cust_phone;
        private String cust_mobile;
        // 省略get和set方法
    }

第五步:创建类与表结构的映射

1. 在JavaBean所在的包下创建映射的配置文件
    * 默认的命名规则为:实体类名.hbm.xml
    * 在xml配置文件中引入约束(引入的是hibernate3.0的dtd约束,不要引入4的约束)
        

2. 如果不能上网,编写配置文件是没有提示的,需要自己来配置
    * 先复制http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd --> window --> preferences --> 搜索xml --> 选择xml catalog --> 点击add --> 现在URI --> 粘贴复制的地址 --> 选择location,选择本地的DTD的路径

3. 编写映射的配置文件
    
    
    
    
        
            
                
            
            
            
            
            
            
            
            
            
            
        
    

第六步:编写Hibernate核心的配置文件

1. 在src目录下,创建名称为hibernate.cfg.xml的配置文件
2. 在XML中引入DTD约束
    

3. 打开:资料/hibernate-release-5.0.7.Final/project/etc/hibernate.properties,可以查看具体的配置信息 
    * 必须配置的4大参数                 
        #hibernate.connection.driver_class com.mysql.jdbc.Driver
        #hibernate.connection.url jdbc:mysql:///test
        #hibernate.connection.username gavin
        #hibernate.connection.password
    
    * 数据库的方言(必须配置的)
        #hibernate.dialect org.hibernate.dialect.MySQLDialect
    
    * 可选的配置
        #hibernate.show_sql true
        #hibernate.format_sql true
        #hibernate.hbm2ddl.auto update
    
    * 引入映射配置文件(一定要注意,要引入映射文件,框架需要加载映射文件)
        *              

4. 具体的配置如下
    
    
    
    
        
            com.mysql.jdbc.Driver
            jdbc:mysql:///hibernate_day01
            root
            root
            org.hibernate.dialect.MySQLDialect
            
            
        
    

第七步:编写Hibernate入门代码

1. 具体的代码如下
    /**
     * 测试保存客户
     */
    @Test
    public void testSave(){
        // 先加载配置文件
        Configuration config = new Configuration();
        // 默认加载src目录下的配置文件
        config.configure();
        // 创建SessionFactory对象
        SessionFactory factory = config.buildSessionFactory();
        // 创建session对象
        Session session = factory.openSession();
        // 开启事务
        Transaction tr = session.beginTransaction();
        // 编写保存代码
        Customer c = new Customer();
        // c.setCust_id(cust_id);   已经自动递增
        c.setCust_name("测试名称");
        c.setCust_mobile("110");
        // 保存客户
        session.save(c);
        // 提交事务
        tr.commit();
        // 释放资源
        session.close();
        factory.close();
    }

你可能感兴趣的:(hibernate总结)