hibernate_demo
参考:ORM----hibernate入门Demo(无敌详细版) - Old-凯 - 博客园 (cnblogs.com)
Hibernate - 基础入门详解_51CTO博客_hibernate入门
hmb.xml:Hibernate框架之hbm.xml映射文件(详解)_hibernate映射文件详解_hestyle的博客-CSDN博客
新建testdb数据库,创建tb_users表:
模块整体目录结构:
pom.xml最初只添加了hibernate、mysql的依赖,其他jar包依赖是根据运行报错,一点点加上的。mysql依赖的版本要对应安装的mysql版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>org.example</groupId> <artifactId>hibernate_demo</artifactId> <version> 1.0 -SNAPSHOT</version> <properties> <maven.compiler.source> 19 </maven.compiler.source> <maven.compiler.target> 19 </maven.compiler.target> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <hibernate.version> 5.2 . 10 .Final</hibernate.version> <hibernate.validator.version> 5.4 . 0 .Final</hibernate.validator.version> <mysql.driver.version> 8.0 . 21 </mysql.driver.version> </properties> <dependencies> <!-- Hibernate包 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>${hibernate.validator.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.driver.version}</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version> 2.3 . 0 </version> </dependency> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version> 2.3 . 0 </version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version> 1.1 . 1 </version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version> 4.0 . 1 </version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version> 4.0 . 1 </version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version> 2.3 . 0 </version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>** /*.xml</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/ *.xml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build> </project> |
User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package com.hmb; public class User { public User() { } public User(Integer id, String name, String pwd) { this .id = id; this .name = name; this .pwd = pwd; } private Integer id; private String name; private String pwd; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this .pwd = pwd; } } |
User.hmb.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?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= "com.hmb.User" table= "tb_users" > <id name= "id" column= "userid" > <generator class = "native" ></generator> </id> <property name= "name" column= "name" type= "string" /> <property name= "pwd" column= "pwd" type= "string" /> </ class > </hibernate-mapping> |
hibernate.cfg.xml要放在resources目录下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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.dialect" >org.hibernate.dialect.MySQLDialect</property> <property name= "hibernate.connect.driver_class" >com.mysql.jdbc.Driver</property> <!--testdb为数据库名--> <property name= "hibernate.connection.url" >jdbc:mysql: //localhost:3306/testdb</property> <!-- 填登录数据库的账号、密码--> <property name= "hibernate.connection.username" >root</property> <property name= "hibernate.connection.password" >******</property> <property name= "format_sql" > true </property> <mapping resource= "com/hmb/User.hmb.xml" /> </session-factory> </hibernate-configuration> |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.hmb; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; public class Main { public static void main(String[] args) { Configuration config = new Configuration().configure( "hibernate.cfg.xml" ); SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession(); Transaction transaction = session.beginTransaction(); User user = new User( 3 , "wangwu" , "123456" ); session.save(user); transaction.commit(); session.clear(); factory.close(); } } |
运行效果:
查看数据表成功新增了数据
其他:
运行时如果报错”java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String“,可添加jvm启动参数”--add-opens java.base/java.lang=ALL-UNNAMED“:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
2018-08-21 线段树模板
2018-08-21 HDU1166 敌兵布阵 线段树