2021-5-13讲课内容hibernate主键id映射_XML方式

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 概述项目结构Student类hibernate.cfg.xmllog4j.propertiesStudent.hbm.xmlStudentTest类理论常用的五种方式1. increment:2. identity主键自增3.sequence 序列4. native5. uuid

概述


代码和博客 略有不同,但是大体上是一样的


项目结构


20210513140134550.png


Student类


package cn.edu.ldu.entity;
public class Student {
    // private int id;
    private String id;
    private String name;
    //必须要有一个无参的构造方法
    //如果写了一个有参构造,必须要有一个午餐的构造方法写出来
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}


hibernate.cfg.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="url">
      jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC
    </property>
<!--    ?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC-->
    <property name="driverClassName"> com.mysql.cj.jdbc.Driver </property>
    <property name="username">root</property><!--connection.-->
    <property name="password">123456</property>
    <!-- DB schema will be updated if needed -->
    <property name="connection.provider_class">
      com.alibaba.druid.support.hibernate.DruidConnectionProvider </property>
    <property name="filters">stat</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <property name="hibernate.hibernate.hbm2ddl.auto">update</property>
    <!--    进行修改-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!--    加入映射-->
    <mapping resource="Student.hbm.xml"></mapping>
  </session-factory>
</hibernate-configuration>


log4j.properties


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="url">
      jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC
    </property>
<!--    ?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC-->
    <property name="driverClassName"> com.mysql.cj.jdbc.Driver </property>
    <property name="username">root</property><!--connection.-->
    <property name="password">123456</property>
    <!-- DB schema will be updated if needed -->
    <property name="connection.provider_class">
      com.alibaba.druid.support.hibernate.DruidConnectionProvider </property>
    <property name="filters">stat</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <property name="hibernate.hibernate.hbm2ddl.auto">update</property>
    <!--    进行修改-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!--    加入映射-->
    <mapping resource="Student.hbm.xml"></mapping>
  </session-factory>
</hibernate-configuration>


Student.hbm.xml


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="url">
      jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=UTC
    </property>
<!--    ?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC-->
    <property name="driverClassName"> com.mysql.cj.jdbc.Driver </property>
    <property name="username">root</property><!--connection.-->
    <property name="password">123456</property>
    <!-- DB schema will be updated if needed -->
    <property name="connection.provider_class">
      com.alibaba.druid.support.hibernate.DruidConnectionProvider </property>
    <property name="filters">stat</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <property name="hibernate.hibernate.hbm2ddl.auto">update</property>
    <!--    进行修改-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!--    加入映射-->
    <mapping resource="Student.hbm.xml"></mapping>
  </session-factory>
</hibernate-configuration>


StudentTest类


package cn.edu.ldu.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
public class StudentTest {
    public static void main(String[] args) {
        Configuration config = new Configuration().configure("/hibernate.cfg.xml");
        SessionFactory sessionFactory = config.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Student student = new Student();
        student.setName("PushyTao");
        session.beginTransaction(); //完整性一致性
        session.save(student);
        session.getTransaction().commit(); //提交
        session.close();
        sessionFactory.close();
    }
    /*@Before
    public void testJunit(){
        System.out.println("before");
    }
    @Test
    public void testInsert(){
        System.out.println("test the insert");
    }
    @After
    public void testUpdate(){
        System.out.println("after");
    }*/
}


理论


常用的五种方式


20210513140703560.png


1. increment:


20210513140847161.png20210513140914815.png


2. identity主键自增


20210513140940610.png


3.sequence 序列


20210513141005850.png


4. native


20210513141023105.png


5. uuid


20210513141043760.png

20210513141054876.png


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3天前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
hibernate正向生成数据库表以及配置——Teacher.hbm.xml
17 1
|
3天前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
11 0
|
3天前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
12 0
|
3天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
18 1
|
3天前
|
Java 数据库连接
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
Hibernate中使用Criteria查询及注解——(Emp.hbm.xml)
12 2
|
3天前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
8 0
|
3天前
|
数据库
最全三大框架整合(使用映射)——struts.xml和web.xml配置
最全三大框架整合(使用映射)——数据库资源文件jdbc.properties
11 0
|
3天前
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
最全三大框架整合(使用映射)——applicationContext.xml里面的配置
9 0
|
3天前
|
XML Java 数据库连接
【Mybatis】XML映射文件
【Mybatis】XML映射文件
30 0
|
3天前
|
XML Java 数据库连接
Mybatis之简介、使用操作(安装、XML、SqlSession、映射的SQL语句、命名空间、作用域和生命周期)
【1月更文挑战第2天】 MyBatis 是一款优秀的持久层框架 MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的过程 MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 实体类 【Plain Old Java Objects,普通的 Java对象】映射成数据库中的记录。
116 2
Mybatis之简介、使用操作(安装、XML、SqlSession、映射的SQL语句、命名空间、作用域和生命周期)