zoukankan      html  css  js  c++  java
  • hibernate学习笔记

    
    

    Hibernate是ORM(Object Relationship Mapping)对象映射关系的持久层框架

    
    

    开发环境IDEA2018.3 + MySQL5.6. + JDK1.8+hibernate5

    什么是hibernate框架?

    它是应用在javaee 三层架构中的dao层 它的底层就是JDBC 它对JDBC进行了封装,好处就是不用写jdbc的代码,和sql语句,它是一个开源的轻量级框架,现在使用hibernate5.x版本

    
    

    1.准备工作:创建测试用的数据库

    
    

    
    

    2.在eclipse中新建项目一个项目

    
    

    导入相关jar包:

    
    

    a.数据库驱动包:

    
    

    b.hibernate/lib/required/下的jar包:

    
    

    c.日志记录包:

    
    

    
    

    3.创建实体类User.java:

    
    
    复制代码
    public class User {
        private int uid;
        private String username;
        private String userAddress;
        private String userPassword;
        public Integer getUid() {
            return uid;
        }
        public void setUid(int uid) {
            this.uid = uid;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getUserAddress() {
            return userAddress;
        }
        public void setUserAddress(String userAddress) {
            this.userAddress = userAddress;
        }
        public String getUserPassword() {
            return userPassword;
        }
        public void setUserPassword(String userPassword) {
            this.userPassword = userPassword;
        }
        
        
    
    }
    复制代码
    
    

    4.在实体类同目录下创建映射配置文件User.bhm.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>
    <!-- 建立类和标的映射关系 -->
    <!-- 
        class标签:用来建立类和表的映射
        name:类的全路径
        table:数据库表名
        catalog:数据库名
     -->
    <class name="com.hxzy.hibernate_demo.entity.User" table="t_user">
    <!-- 
        id标签:用来建立类中的属性与标的主键字段的对应
        name:类中的属性名
        column:表中对应的字段名 (若与属性名相同)可省略
        length:字段的长度
        type:类型
     -->
    <id name="uid" column="uid">
        <!-- 主键生成策略 -->
        <generator class="native" />
    </id>
    <!-- 建立类中普通属性与表中字段的映射 -->
     <!-- 
        property标签:用来建立类中普通属性与表中字段的映射
        name:类中的属性名
        column:表中字段名,若与类名一致可省略
        length:字段长度
        type:类型,Java类型,hibernate类型,SQL类型
      -->
      <property name="username" column="username"/>
      <property name="userAddress" column="userAddress"/>
      <property name="userPassword" column="userPassword"/>
      
    </class>
    </hibernate-mapping>
    复制代码
    
    

    5.创建Hibernate核心配置文件,一般放在src目录下命名为hibernate.cfg.xml,文件内容如下:

    
    
    复制代码
    <?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>

      <!-- 数据库地址,若不加?useUnicode=true&amp;characterEncoding=utf8可能出现连接失败,或汉字存储乱码问题 --> <property name="hibernate.connection.url"> jdbc:mysql:///hibernate_demo?useUnicode=true&amp;characterEncoding=utf8 </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">111111</property> <!-- hibernate属性 --> <!-- 方言:设定链接数据库类型 --> <!-- 显示SQL语句 --> <property name="hibernate.show_sql">true</property> <!-- 格式化SQL语句 --> <property name="hibernate.format_sql">true</property> <!-- hbm2ddl.auto的取值 none:不用hibernate自动生成 create:每次都会创建新表 create:drop:每次创建信表,执行结束后删掉 update:如果库中有表,使用原来的表,没有则自动生成,可以更新表结构? validate: 只使用原有的表结构,对映射关系进行校验 --> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 加载映射 --> <mapping resource="com/hxzy/hibernate_demo/entity/User.hbm.xml" /> </session-factory> </hibernate-configuration>
    复制代码
    
    

    6.创建测试类

    
    
    package bin.superme.test;

    import bin.superme.pojo.User;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;

    public class User_Test {
    public static void main(String[] args) {
    //加载hibernate 的核心配置文件
    Configuration cfg = new Configuration();
    cfg.configure();
    //创建 SessionFactory 对象
    SessionFactory sessionFactory = cfg.buildSessionFactory();
    //获取SessionFactory的链接
    Session session = sessionFactory.openSession();
    //开启事务
    Transaction tran = session.beginTransaction();
    //写业务逻辑
    User user = new User();
    user.setUsername("张6");
    user.setPassword("qwe123");
    session.save(user);
    //提交事务
    tran.commit();
    //关闭
    session.close();
    sessionFactory.close();
    }
    }
     
  • 相关阅读:
    centos7.6 使用yum安装mysql5.7
    解决hadoop本地库问题
    docker-compose 启动警告
    docker 安装zabbix5.0 界面乱码问题解决
    docker 部署zabbix问题
    zookeeper 超时问题
    hbase regionserver异常宕机
    (转载)hadoop 滚动升级
    hadoop Requested data length 86483783 is longer than maximum configured RPC length
    zkfc 异常退出问题,报错Received stat error from Zookeeper. code:CONNECTIONLOSS
  • 原文地址:https://www.cnblogs.com/gu-bin/p/10414905.html
Copyright © 2011-2022 走看看