美文网首页
01_hibernate入门和基本操作无标题文章

01_hibernate入门和基本操作无标题文章

作者: AndroidCat | 来源:发表于2017-07-10 00:05 被阅读0次
web内容回顾
j2EE三层结构用到框架
  • web层: struts2框架
  • service层: spring框架
  • dao层:hibernate框架
    • 对数据库进行curd操作
MVC思想
  • M:模型
  • v:视图
  • c:控制
  • MVC不是我们java独有的,在其他语言中也有这种思想
hibernate概述
什么是框架
  1. 框架是用来提高开发效率的
  2. 封装了好了一些功能.我们需要使用这些功能时,调用即可.不需要再手动实现.
  3. 所以框架可以理解成是一个半成品的项目.只要懂得如何驾驭这些功能即可.
  • 写程序,使用框架之后,帮我们实现一部分的功能,少些一部分代码
什么是hibernate(冬眠)
  • hibernate框架应用在javaEE三层架构中的dao层
  • 在dao层中对数据库进行curd操作,hibernate底层代码就是jdbc,hibernate是对jdbc进行了封装,不需要写复杂的jdbc代码了,也不需要写sql了
  • 代替了我们以前的jdbc和dbutils,用于帮助我们完成数据的操作的
  • hibernate是开源的轻量级框架
  • hibernate版本:hibernate-release-5.0.7.Final.zip
    • lib:hibernate相关jar包
什么是orm思想---hibernate就是通过orm思想对数据库进行操作的
  • web阶段学习中,javaBean更正确的叫法:实体类
  • orm: object relational mapping: 对象关系映射
  • 让实体类和数据库进行一一对应关系
    • 类名和表名对应
    • 类中的属性和表的字段对应
  • 不需要直接操作数据库表,而操作表对应的实体类对象
  • orm分4级
    • hibernate属于4级:完全面向对象操作数据库
    • mybatis属于2级
    • dbutils属于1级--对ResultSet进行封装
JDBC操作数据库
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement pstmp = conn.prepareStatement(sql);
ResultSet resultSet = pstmp.executeQuery();
while(resultSet.next()) {
    ....
}
resultSet.close();
pstmp.close();
conn.close();
hibernate操作数据库
实体类的创建
public class User {
    private int uid;
    private String username;
    private String password;
    //set and get 方法
}

数据库建表
create table t_user (
    uid int primary key
    username varchar(50)
    password varchar(50)
)

让实体类和数据库表进行一一对应关系
使用配置文件的方式完成

不直接操作表,直接操作表对应的实体类对象完成对数据库的操作
hibernate封装的session对象进行操作

User user = new User();
user.setxxx...
session.save(user);
hibernate入门案例
开发环境搭建
  • hibernate用在dao层中,建java项目和web项目都可以
  1. 导入hibernate的jar包
    • 解压hibernate-release-5.0.7.Final.zip
    • 在lib目录中把require目录中的jar包方法工程
    • jpa目录中的jar包也要放进去
    • 使用hibernate时候有一些日志信息的输出,hibernate本身没有支持日志输出的jar包,我们导入一些其他的日志的jar包来支持输出log4j.jar,slf4j-api.jar,slf4j-log4j.jar
    • 数据库驱动jar包
    • 解压后再project\etc文件夹下有hibernate所有的配置文件
  2. 创建实体类
public class User {
    hibernate要求实体类有一个属性唯一的值
    private int uid;
    private String username;
    private String password;
    
    get and set method........
}
  1. 创建表(数据库要自己建)

    • 使用hibernate时候,不需要我们手动建表,hibernate会帮忙建表
  2. 配置实体类和数据库表的一一对应关系(映射关系)

    • 使用配置文件来实现映射关系
    • 创建一个xml格式的配置文件
      • 映射配置文件名称和位置没有固定要求
      • 建议:在实体类所在的包里面创建,实体类名称.Hbm.xml
      • 引入约束DTD(mapping.dtd)
      • 重点写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!--配置哪个类和哪个表进行映射关系-->
    <class name="实体类全路径" table="数据库表名称,没有的话hibernate会帮你建">
        <!--配置实体类属性和表字段一一对应-->
        <!--
            hibernate要求类中和表中都有一个位置的值
            表中的id和类中的id
        -->
        <id name="实体类中id属性的名称:uid" column="生成表里面字段名称:uid">
            <!--
                设置数据库表id增长策略
                native:生成表的id值就会是主键和自增长
            -->
            <generator class="native" >
            </generator>
        </id>
        <!--配置其他属性-->
        <!--
            not-null默认值为false(可选)
            length=""(可选)配置数据库中列的长度,默认值为数据库数据类型最大
        -->
        <property name="实体类中的字段" cloumn="表的字段名字" not-null="true"></property>
        <property name="username" cloumn="username"></property>
        <property name="password" cloumn="password"></property>
    </class>
</hibernate-mapping>
  1. 创建hibernate核心配置文件
    • 核心配置文件格式:xml,但是名称和位子是固定的
      • hibernate.cfg.xml
    • 位置必须是在src下面
    • 名称必须是hibernate.cfg.xml
    • hibernate操作过程中,只会加载核心配置文件,其他配置文件不会加载
      1. 配置数据库信息(必须有)
      2. 配置hibernate信息(可选的)
      3. 映射文件是不会加载的,把映射文件放到核心配置文件中(必须的)
        • 映射文件不会加载,我们在核心配置文件中加载映射配置文件,引入映射文件
  • ctrl + shift + t快速找到某个类
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--配置数据库信息(必要)-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/数据库名</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!--
            配置数据库的方言(必要)
            在mysql中实现分页:关键字limit,只能使用在mysql中
            在oracle数据库中实现分页:rownum
            让hibernate框架识别不同数据库的语句
        -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        
        <!--配置hibernate信息(可选)-->
        <!--有sql语句的输出-->
        <property name="hibernate.show_sql">true</property>
        <!--对语句进行格式化-->
        <property name="hibernate.format_sql">true</property>
        <!--
            hibernate会帮我们自动创建表
            update:如果有这个表,会帮我们更新,如果没有
        -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        
        <!--映射文件是不会加载的,把映射文件放到核心配置文件中-->
        <mapping resource="cn/itcast/entity/User.hbm.xml" />
    </session-factory>
</hibernate-configuration>
添加功能实现(测试环境功能)
  1. 加载hibernate核心配置文件
  2. 创建SessionFactory对象
  3. 使用SessionFactor创建Session对象
  4. 开启事务
  5. 写具体的crud操作
  6. 提交事务
  7. 关闭资源
public class HibernateDemo {
    @Test
    public void testAdd() {
        hibernate中封装了一个类用来加载配置文件
        Configuration config = new Configuration();
        会到src目录下去加载hibernate.cfg.xml
        config.configure();
        
        根据配置信息创建SessionFactory对象
        在这个过程中,会根据映射关系,在配置的数据库中把表创建出来
        SessionFactory factory = config.buildSessionFactory();
        
        创建session,类似于Connection
        Session session = factory.openSession();
        
        开启事务
        Transation transation = session.beginTransation();
        
        添加数据
        User user = new User(uid,username,password);
        session.save(user);
        
        Customer customer = session.get(Customer.class, 1l);
        session.update(customer);
        session.delete(customer);
        
        
        提交事务
        transation.commit();
        关闭资源
        session.close();
        factory.close();
    }
}
  • 会自动生成表
  • 新增一条记录
hibernate配置文件详解
映射配置文件
  1. 名称和位置没有固定的要求
  2. 映射文件中,标签name属性值写的是实体类相关的内容
    • class标签中name属性值为实体类的全路径
    • id和property标签的name属性为实体类中的属性名称
  3. id标签和property标签中的column属性可以不写,不写生成表的字段跟name中的属性是一样的
  4. property标签中还有一个属性:type=""设置生成表的字段的类型,一般不用
核心配置文件
  • 如上面的代码
    • 数据库部分是必须的
    • hibernate部分是可选的
    • 映射的文件是必须的
  • 名称和位置是固定的
    • 名称: hibernate.cgf.xml
    • 位置: src目录下
hibernate核心api
Configuration
Configuration config = new Configuration();
config.configure();
到src目录下找到hibernate.cfg.xml配置文件,创建对象,把配置文件放到对象里(加载核心配置文件)
SessionFactory(重要)
SessionFactory factory = config.buildSessionFactory();
根据核心配置文件中,有数据库配置,映射文件配置,到数据库中根据映射关系把表创建
创建sessionFactory过程中,是非常耗资源的
sessionFactory是线程安全的
在hibernate操作中,一个项目一般创建一个sessionFactory对象

写工具类,写静态代码块实现

public class HibernateUtils {
    private static final SessionFactory fatory = null;
    private static final Configuration config = null;
    static {
        config = new Configuration();
        config.configure();
        
        factory = config.buildSessionFactory();
    }
    
    public static SessionFactory getSessionFactory() {
        return factory;
    }
    
    public static Session getSession() {
        return factory.getOpenSession();
    }
    public static Session getCurrentSession() {
        return factory.getCurrentSession();
    }
}
Session(重要)
  • session类似与jdbc中的connection
  • hibernate与数据库之间的会话连接
  • 调用session不同方法实现curd操作
    • 添加: save
    • 修改: update
    • 删除: delete
    • 根据id查询: get
  • session是单线程的对象
    • session对象不能共用,只能自己用
Transation
Transation transation = session.beginTransation();
transation.rollback();
transation.commit();
配置文件没有提示问题
  1. 可以上网的电脑都可以解决
  2. 有一些公司不能够上网时,把约束文件引入到eclipse中.
    • window-->preferences-->xml catalog
    • 在配置文件中复制地址粘贴到key type = uri中
    • location选择约束文件本地地址
CRM联系
  • CRM:customer relation manager-->客户关系管理系统
  1. 创建一个新的web项目
  2. 导包
    • hibernate包
    • 数据库驱动包
    • 标签库包
    • BeanUtils
  3. 引入静态页面
  4. 搭建hibernate框架
  5. 思路分析
  6. 开发
    • 通过filter解决乱码问题
  7. 测试

相关文章

  • 01_hibernate入门和基本操作无标题文章

    web内容回顾 j2EE三层结构用到框架 web层: struts2框架 service层: spring框架 d...

  • 无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章 无标题文章无标题文章无标题文章无...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • fasfsdfdf

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标...

  • 无标题文章

    无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章无标题文章

网友评论

      本文标题:01_hibernate入门和基本操作无标题文章

      本文链接:https://www.haomeiwen.com/subject/qgoehxtx.html