2021-5-13讲课内容hibernate主键id映射_XML方式
Posted PushyTao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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&useSSL=false&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&useSSL=false&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&useSSL=false&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
以上是关于2021-5-13讲课内容hibernate主键id映射_XML方式的主要内容,如果未能解决你的问题,请参考以下文章
hibernate 主键是String类型利用createQuery查询全表内容出错。
使用 PostgreSQL 的 Hibernate import.sql 重复主键