zoukankan      html  css  js  c++  java
  • 2021-5-13讲课内容hibernate主键id映射_XML方式

    概述

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

    项目结构

    在这里插入图片描述

    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");
        }*/
    
    }
    
    

    理论

    常用的五种方式

    在这里插入图片描述

    1. increment:

    在这里插入图片描述
    在这里插入图片描述

    2. identity主键自增

    在这里插入图片描述

    3.sequence 序列

    在这里插入图片描述

    4. native

    在这里插入图片描述

    5. uuid

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    10th blog:箭头函数
    10th blog:Event Flow
    12th:MAP.API
    10th blog:For each···in / For···in / For···of
    10th blog:Object
    Web第九周作业:History of Program(1950--2020)
    Web作业:Regular Expression
    Web作业:specific word count (index of )
    Web第七周作业:DOM&BOM
    MAP
  • 原文地址:https://www.cnblogs.com/PushyTao/p/15101054.html
Copyright © 2011-2022 走看看