Hibernate5.x Eclipse搭建

今天写一个简单的hibernate框架搭建流程

首先准备好我们的jar包,这里附上一个jar包链接

hibernate下载

下载完jar后新建一个简单的java工程,结构如下

9988457-e6a1addd3b67fac1.jpg
项目结构

接下来编写我们的hibernate的配置文件hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> <!-- 该声明可以到org/hibernate/hibernate-configuration-3.0.dtd查看--> <!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:///hibernate</property> <!-- 链接数据库的用户名 --> <property name="hibernate.connection.user">root</property> <!-- 密码 --> <property name="hibernate.connection.password">root</property> <!-- 是否显示SQL语句 --> <property name="hibernate.show_sql">true</property> <!-- 链接的编码 --> <property name="connection.characterEncoding">utf8</property> <!-- hibernate使用的方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 映射文件 --> <mapping resource="com/education/bean/User.hbm.xml" /> </session-factory> </hibernate-configuration>

下面列举一些常用的属性以及一些常用的方言设置

属性名作用
hibernate.dialect为所选数据库使用其SQL
hibernate.connection.driver_classJDBC驱动
hibernate.connection.url链接地址
hibernate.connection.pool_size连接池的数量
hibernate.connection.autocommint自动提交模式

方言设置(可以在org/hibernate/dialect中找到)

数据库属性
SQL Server 2000org.hibernate.dialect.SQLServerDialect
SQL Server 2008org.hibernate.dialect.SQLServer2008Dialect
MySQLorg.hibernate.dialect.MySQLDialect
Oracleorg.hibernate.dialect.OracleDialect

创建一个简单的User类,定义了三个属性以及他们的get/set方法

package com.education.bean; public class User { private int id; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } }

我们要做的是将数据库的表与实体类相连接,所以需要创建一个映射文件,映射文件的命名方式为 className.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> <!-- 可以在org/hibernate/hibernate-mapping-3.0.dtd中找到 --> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 指明为哪个包下的,如果此处的package没有指明,则下面的class中需要全名 --> <hibernate-mapping package="com.eduask.bean"> <!-- 规定该类对应哪张表 --> <class name="User" table="user"> <!-- 主键使用id标签,name为属性名与column列名相对应 --> <id name="id" type="int" column="id"> <!-- 主键增长设置 --> <generator class="native"></generator> </id> <!-- 其余属性映射 type是java类型与数据库类型的对应设置 --> <property name="name" type="string" column="username"/> <property name="password" type="string" column="passwd"></property> </class> </hibernate-mapping>

附上自增长类型表与对应类型表

生产器说明
increment从数据库取出主键的最大值,然后递增1
identity使用数据库的自增长策略
sequence只能在支持序列的数据中使用,如oracle
native由hibernate根据数据库自动选择identity/hilo/sequence中的任意一种
assigned在外部生成,在save前必须指定一个
java类型映射类型sql类型
int/Integerint/Integerinteger
long/Longlongbigint
short/Shortshortsamllint
byte/Bytebytetinyint
float/Floatfloatfloat
double/Doubledoubledouble
Stringstringvarchar
boolean/Booleantrue/falsechar(1)('T'/'F')
Datedatedate
Date/Timetimetime
Date/Timestamptimestamptimestamp
Clobclobclob
Blobblobblob

最后编写一个测试类来测试一下我们的hibernate框架是否搭建成功

package com.education.controller; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.education.bean.User; public class IndexController { public static void main(String[] args) { //configuration 主配置类 负责读取hibernate运行的底层信息 //sessionFactory 会话工厂 充当数据源,负责创建session对象 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //session 会话 应用程序与数据库的一次交互,包含了一般的CURD方法 Session session = sessionFactory.openSession(); //实例化出一个对象 User user = new User(); user.setName("隔壁老王"); user.setPassword("123123"); //使用事务管理 Transaction transcation = session.getTransaction(); //开始事务 transcation.begin(); //此时数据不会真的保存到数据库中 session.save(user); //提交事务后,数据才会真正的保存到数据库中 transcation.commit(); } }

控制台已经将sql语句答应出来了

9988457-ff41f1a2747c2531.jpg
sql语句

最后看一下数据库中是否添加成功

9988457-f8fe2b58b1d91296.jpg
数据库.jpg

到此为止,hibernate的框架搭建完毕。


__EOF__

本文作者Gabriel
本文链接https://www.cnblogs.com/youarephoenix/p/15972940.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   gabriel丶  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示