搭建简单的Habernate环境(一)
一、开篇
下载Habernate所需要的jar包和mysql驱动。
二、建立java项目并且导入所需要的包
三、建立实体类和配置映射文件
user实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package testPackage; public class user { public String id; public 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; } } |
user配置文件
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> < class name= "testPackage.user" table= "user" > <id name= "id" > <generator class = "uuid" ></generator> </id> <property name= "name" /> </ class > </hibernate-mapping> |
habernate.cfg.cml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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.connection.driver_class" >com.mysql.jdbc.Driver</property> <property name= "hibernate.connection.url" >jdbc:mysql: //120.78.155.**:3306/jwttoken</property> <property name= "hibernate.connection.username" >root</property> <property name= "connection.password" >root</property> <property name= "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property> <property name= "hibernate.hbm2ddl.auto" >update</property> <property name= "hibernate.show_sql" > true </property> <property name= "hibernate.format_sql" > true </property> <mapping resource= "testPackage/user.xml" /> </session-factory> </hibernate-configuration> |
四、测试
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 | package testPackage; import java.util.UUID; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class demo { public static void main(String[] args) { user d= new user(); d.id=UUID.randomUUID().toString().replaceAll( "-" , "" ); d.name= "dirk" ; Configuration configure = new Configuration().configure(); SessionFactory factory = configure.buildSessionFactory(); Session session = factory.openSession(); Transaction transaction= session.beginTransaction(); session.save(d); transaction.commit(); System.out.print( "ok" ); } } |
五、结果
六、遇到的问题
本文作者:WangJunZzz
本文链接:https://www.cnblogs.com/WangJunZzz/p/7845000.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义