Hibernate 框架的详细搭建与简单使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33642117/article/details/51879996

今天我就写一下怎么搭建Hibernate的框架的~

步骤如下:

1. 获取hibernate发布包

可以在github或者官网找找~

2.将hibernate依赖的jar集成到项目中

一共要9个jar包~

到这个目录hibernate\hibernate-distribution-3.6.10.Final\lib\required找,有6个jar包,如图:


有这些包还不够,还需要到hibernate\hibernate-distribution-3.6.10.Final\lib\jpa这个目录下,看图:


还需要俩个jar包,一个是连接数据库的包,一个是Hibernate包,如图:


到这里jar包全部找到,可以导入到项目里面了~

在项目中新建一个jar文件,把要依赖的jar包放进去,如图所示:


jar包包括驱动包和hibernate3.jar和一些别的包,都是一些必须要的包~

3.将hibernate依赖的配置文件和映射文件加入到项目中

hibernate.cfg.xml 配置文件

***.hbm.xml     映射文件

这时候就要把dtd文件放入到项目中了~

把hibernate3.jar包解压开来,进入到hibernate\hibernate-distribution-3.6.10.Final\hibernate3\org\hibernate,可以找到俩个dtd文件,如图:


在项目中新建一个dtd文件,把必要的dtd放入到dtd文件中,如图:


hibernate.cfg.xml配置文件的写法:


 
 
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- Database connection settings -->
  8. <property name= "connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name= "connection.url">jdbc:mysql: //127.0.0.1:3306/ssh</property>
  10. <property name= "connection.username">root</property>
  11. <property name= "connection.password">root</property>
  12. <!-- SQL dialect -->
  13. <property name= "dialect">org.hibernate.dialect.MySQLDialect</property>
  14. <!-- Echo all executed SQL to stdout -->
  15. <property name= "show_sql"> true</property>
  16. <!-- Drop and re-create the database schema on startup -->
  17. <property name= "hbm2ddl.auto">update</property>
  18. </session-factory>
  19. </hibernate-configuration>

 
 
  1. <!DOCTYPE hibernate-configuration PUBLIC
  2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  4. 这几句可以从dtd文件中hibernate-configuration- 3.0.dtd找~,这几句是必须要的

<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property>中的ssh是在Native MySql中创建的一个数据库的名字~

<hibernate-configuration>这行代码出不现的话,就要配置了,在Window的Preferences做如图操作:



上面的location就是这个项目中dtd文件的所在目录,而key就是http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd这个链接~

这样配置文件就配置好了
***.hbm.xml映射文件的写法:


 
 
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package= "com.xu.day4.basic">
  6. </hibernate-mapping>

<hibernate-mapping package="com.xu.day4.basic">是指要映射到哪个包下的哪个类~

同样


 
 
  1. <!DOCTYPE hibernate-mapping PUBLIC
  2. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  4. 这几行代码也是必须的~可以从hibernate-mapping- 3.0.dtd文件中找~
<hibernate-mapping>如果这行代码也出不现的话,和配置文件的配置步骤是一样的~

4.将映射文件集成到配置文件中

看下图:



只需要这样集成就行~

那框架就搭配完成了~

现在就可以写写一个简单的例子:

要做一个用户的存储~

User类:


 
 
  1. public class User implements Serializable{
  2. private static final long serialVersionUID = 1L;
  3. Long id;
  4. String name;
  5. Double account;
  6. Date birthday;
  7. public User() {
  8. super();
  9. // TODO Auto-generated constructor stub
  10. }
  11. public User(Long id, String name, Double account, Date birthday) {
  12. super();
  13. this.id = id;
  14. this.name = name;
  15. this.account = account;
  16. this.birthday = birthday;
  17. }
  18. public Long getId() {
  19. return id;
  20. }
  21. public void setId(Long id) {
  22. this.id = id;
  23. }
  24. public String getName() {
  25. return name;
  26. }
  27. public void setName(String name) {
  28. this.name = name;
  29. }
  30. public Double getAccount() {
  31. return account;
  32. }
  33. public void setAccount(Double account) {
  34. this.account = account;
  35. }
  36. public Date getBirthday() {
  37. return birthday;
  38. }
  39. public void setBirthday(Date birthday) {
  40. this.birthday = birthday;
  41. }
  42. @Override
  43. public String toString() {
  44. return "User [id=" + id + ", name=" + name + ", account=" + account + ", birthday=" + birthday + "]";
  45. }
  46. }

hibernate.cfg.xml:


 
 
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- Database connection settings -->
  8. <property name= "connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property name= "connection.url">jdbc:mysql: //127.0.0.1:3306/ssh</property>
  10. <property name= "connection.username">root</property>
  11. <property name= "connection.password">root</property>
  12. <!-- SQL dialect -->
  13. <property name= "dialect">org.hibernate.dialect.MySQLDialect</property>
  14. <!-- Echo all executed SQL to stdout -->
  15. <property name= "show_sql"> true</property>
  16. <!-- Drop and re-create the database schema on startup -->
  17. <property name= "hbm2ddl.auto">update</property>
  18. <mapping resource= "com/xu/day4/basic/user.hbm.xml" />
  19. </session-factory>
  20. </hibernate-configuration>
映射文件user.hbm.xml:


 
 
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package= "com.xu.day4.basic">
  6. < class name= "User" table= "tb_user">
  7. <id name= "id" column= "id">
  8. <generator class= "increment" />
  9. </id>
  10. <property name= "name" />
  11. <property name= "account" />
  12. <property name= "birthday" />
  13. </ class>
  14. </hibernate-mapping>

在Test类中代码运行入口:


 
 
  1. public class Test {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. //1.创建配置对象
  5. Configuration configuration= new Configuration();
  6. //2.读取配置对象
  7. configuration.configure();
  8. //3.根据配置文件信息创建一级缓存,SessionFactory
  9. /*
  10. * Connection
  11. * 映射文件,映射对象
  12. * HttpSession 用户与服务器之间的会话
  13. * Session 代码与数据库之间的会话
  14. */
  15. SessionFactory ss=configuration.buildSessionFactory();
  16. //4.创建Session,打开会话
  17. Session session=ss.openSession();
  18. //5.开启事务
  19. Transaction tran=session.beginTransaction();
  20. session.save( new User( null, "张二", 100.0, new Date()));
  21. //6.事务的提交
  22. tran.commit();
  23. //7.session关闭
  24. session.close();
  25. System.out.println( "1111111111111111111");
  26. }
  27. }
注释已经写的很清楚,我就不一一解释了~

下一篇我也讲个例子,可以把部分代码封装起来~

效果图我就不贴了,有什么需要的可以和我交流啊~吐舌头

源码:下载
































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值