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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现