zoukankan      html  css  js  c++  java
  • Hibernate的基本知识

    1.框架介绍

    2.ORM框架

    2.1 数据库回顾

     

    2.2 ORM框架介绍

    orm(Object/Relation Mapping):对象-关系映射,java中的对象与数据库中的表,之间的映射关系

    ORM功能:提供api操作java对象,ORM自动生成sql,并执行

    ORM开发需要完成的事情:编写对象、建立映射关系

    3 hibernate

    3.1介绍

    Hibernate是轻量级JavaEE应用的持久层解决方案,是一个关系数据库ORM框架

    持久层:java中的数据需要保存到数据库

    轻量级:使用的依赖hibernate内容较少。提供简单的api和通过配置就能完成很强大的功能

    Hibernate提供了对关系型数据库的增删改查操作。

    流行的数据库框架:

    1)JPA(Java Persistence API.)只有接口范式

    2)Hibernate通过对象-关系映射配置,可以完全脱离底层SQL

    3)MyBatis本是一个apache的开源项目iBatis,支出普通SQL查询,存储过程和高级映射的优秀持久层框架

    4)Apache DBUtils

    5)Spring JDBCTemplate

    3.2分析

    3.3Hibernate优点

    3.4 日志框架调用过渡jar,执行目标jar

    相当于jdbc调用驱动,执行数据库

    4 Hello Word(重要)

    4.1 编写流程

    1)编写javabean

    package cn.hibernate.bean;

    public class User {
    private Integer userId; //类型必须,整形,名称不建议使用id
    private String userName;
    private String userPassword;
    public User() {
    super();
    }
    public User(Integer userId, String userName, String userPassword) {
    super();
    this.userId = userId;
    this.userName = userName;
    this.userPassword = userPassword;
    }
    public Integer getUserId() {
    return userId;
    }
    public void setUserId(Integer userId) {
    this.userId = userId;
    }
    public String getUserName() {
    return userName;
    }
    public void setUserName(String userName) {
    this.userName = userName;
    }
    public String getUserPassword() {
    return userPassword;
    }
    public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
    }
    @Override
    public String toString() {
    return "User [userId=" + userId + ", userName=" + userName
    + ", userPassword=" + userPassword + "]";
    }

    }

    2)核心配置文件:hibernate.cfg.xml

       基本项:4项

       映射

    <?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>
    <!-- #1基本4项,必须手动的创建数据库 -->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/minemysql</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <!-- #2配置方言
    通过配置文件获得:%h%project/etc/hibernate.properties
    -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

    <!-- #3自动创建表,固定值 -->
    <property name="hibernate.hbm2ddl.auto">update</property>

    <!-- 配置映射文件 -->
    <mapping class="cn/hibernate/bean/User.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>

    3)映射英文:*.hbm.xml  与bean的类名相同

    位置:

    下面是User.hbm.xml文件

    <?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 package="cn.hibernate.bean">
    <class name="User" table="user">
    <!-- 主键 -->
    <id name="userId">
    <!-- 固定值 -->
    <generator class="native"></generator>
    </id>
    <property name="userName" ></property>
    <property name="userPassword"></property>
    </class>
    </hibernate-mapping>

    4)使用api

    package cn.hibernate.test;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;

    import cn.hibernate.bean.User;

    public class UserTest {

    public static void main(String[] args) {

    User user = new User();
    //不设置userId
    user.setUserName("嘿嘿");
    user.setUserPassword("ds1233");

    //1.加载配置文件,注意:必须执行configure()方法
    Configuration config = new Configuration().configure();

    //2.获得session工厂,相当于连接池
    SessionFactory factory = config.buildSessionFactory();

    //3.获得session,相当于jdbc Connection
    Session session = factory.openSession();

    //4.必须开启事物
    Transaction transaction = session.beginTransaction();

    //操作
    session.save(user);

    //5.提交|回滚
    transaction.commit();

    //6.释放资源
    session.close();
    factory.close(); //可以不关

    }
    }

    4.2 导入jar包

    版本:hibernate-distribution-3.6.10.Final

    核心jar:hibernate3.jar

    必须jar:lib\required\*

    jpa jar:lib\jpa\*

    驱动:mysql

     4.3 jar包总结

    jar包位置

  • 相关阅读:
    2.vue插件总结——总有你能用上的插件
    1.前端数据可视化插件:Highcharts、Echarts和D3(区别)
    git学习地址
    node vue 微信公众号(四)配置环境 本地测试
    解决element 分页组件,搜索过后current-page 绑定的数据变了,但是页面当前页码并没有变的问题
    导入excel并进行数据提取
    后端返回字符串中带换行符,前端需转换
    正则表达式tab表示
    FormData
    interval
  • 原文地址:https://www.cnblogs.com/MisterLiu/p/6931265.html
Copyright © 2011-2022 走看看