当前位置: 移动技术网 > IT编程>开发语言>Java > Java的Hibernate框架结合MySQL的入门学习教程

Java的Hibernate框架结合MySQL的入门学习教程

2019年07月22日  | 移动技术网IT编程  | 我要评论
零、关于hibernate hibernate是冬眠的意思,它是指动物的冬眠,但是本文讨论的hibernate却与冬眠毫无关系,而是接下来要讨论的ssh2框架中的一员。h

零、关于hibernate
hibernate是冬眠的意思,它是指动物的冬眠,但是本文讨论的hibernate却与冬眠毫无关系,而是接下来要讨论的ssh2框架中的一员。hibernate是一个开源的项目,它是一个对象关系模型的框架,并且对jdbc进行了非常轻量级的封装,程序员在开发时可以使用对象编程思维进行开发。
下载地址:http://hibernate.org/orm/downloads/
note:轻量级和重量级的区别,轻量级的框架包较小,并且使用较简单,而且测试容易,开发效率高;重量级框架则包较大,内部封装的业务过程较复杂,测试困难,如struts。

201676182320036.png (558×411)

对象关系模型:

201676182344905.png (620×398)

 hibernate实现了对象--关系模型的映射,在编程时程序员能够直接使用对象模型对数据库进行操作,它对jdbc进行了轻量级的封装,另外还封装了对数据库操作的sql语句,使用简单。虽然它有很多优点,但是使用数据库特性的语句,将很难调优,如:存储过程等就比较困难。
 hibernate优缺点:
(1)优点
a、提高生产力;
b、使开发更加对象化(阻抗不匹配);
c、可移植性;
d、没有侵入性,支持透明持久化。
(2)缺点
a、使用数据库特性的语句,将很难调优;
b、对大批量数据更新存在问题;
c、系统中存在大量的统计查询功能。


二、hibernate实例
上文对hibernate做了一些初步的解读,有了理论当然更要有实践,没有使用过hibernate是不懂得它的便利的,这正如一个喜欢喝酒的人第一次品尝到茅台一样,使用后才能更深刻的理解。
下面的实例采用了mysql数据库,在mysql中创建了一个名为hibernate_first的数据库,并通过hibernate的映射文件采用对象化编程的方法创建了一个user表,并向user表中添加信息。
具体步骤:
(1)创建一个普通的java application;
(2)添加hibernate的jar包,添加jar包时需要将hibernate.jar、hibernate引用的第三方jar包以及hibernate和mysql连接的jar包一同引入其中;
(3)添加数据库连接配置文件hibernate.cfg.xml。

<?xml version="1.0" encoding="utf-8"?> 
<!doctype hibernate-configuration public 
    "-//hibernate/hibernate configuration dtd 3.0//en" 
    "http://hibernate.sourceforge.net/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://localhost:3306/hibernate_first</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">ab12</property> 
    <!-- dialect:方言,封装的底层api,类似于runtime,将数据库转换为配置中的相应的语言 --> 
    <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property> 
     
    <!-- 设置数据显示对数据库的操作 --> 
    <property name="hibernate.show_sql">true</property> 
     
    <property name="hibernate.format_sql">true</property> 
    <mapping resource="com/hibernate/user.hbm.xml"/> 
  </session-factory> 
</hibernate-configuration> 

(4)建立实体类名称为user.java

package com.hibernate; 
 
import java.util.date; 
 
public class user { 
  private string id; 
  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; 
  } 
  public string getpassword() { 
    return password; 
  } 
  public void setpassword(string password) { 
    this.password = password; 
  } 
  public date getcreatetime() { 
    return createtime; 
  } 
  public void setcreatetime(date createtime) { 
    this.createtime = createtime; 
  } 
  public date getexpiretime() { 
    return expiretime; 
  } 
  public void setexpiretime(date expiretime) { 
    this.expiretime = expiretime; 
  } 
  private string name; 
  private string password; 
  private date createtime; 
  private date expiretime; 
} 

(5)创建user实体类的映射文件user.hbm.xml,完成实体类的映射,并将该文件加入到hibernate.cfg.xml文件中。

<?xml version="1.0"?> 
<!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- generated 2014-4-30 15:39:33 by hibernate tools 3.4.0.cr1 --> 
<hibernate-mapping> 
  <class name="com.hibernate.user"> 
    <id name="id"> 
      <generator class="uuid"/> 
    </id> 
    <property name="name"/> 
    <property name="password"/> 
    <property name="createtime"/> 
    <property name="expiretime"/> 
  </class> 
</hibernate-mapping> 

(6)编写exportdb.java,将映射文件转化为相应的ddl。

package com.hibernate; 
 
import org.hibernate.cfg.configuration; 
import org.hibernate.tool.hbm2ddl.schemaexport; 
public class exportdb { 
  public static void main(string[] args){ 
    //首先声明获取配置文件 
    //默认读取hibernate.cfg.xml文件 
    configuration cfg=new configuration().configure();  
     
    //将读取到的xml文件导出到ddl 
    schemaexport export=new schemaexport(cfg); 
    export.create(true, true); 
  } 
} 

运行exportdb类即可完成数据库表的创建工作,在cmd中查看具体操作后的视图如下:

201676182756720.png (508×189)

上面的例子只是完成了连接数据库及在数据库中创建表的操作,创建完表后要向表中添加数据,建立客户端类client,向user表中添加新的用户信息,具体代码如下:

package com.hibernate; 
 
import java.util.date; 
 
import org.hibernate.session; 
import org.hibernate.sessionfactory; 
import org.hibernate.cfg.configuration; 
 
public class client { 
  public static void main(string[] args){ 
     
    //读取hibernate.cfg.xml文件 
    configuration cfg=new configuration().configure(); 
     
    //创建sessionfactory,相当于数据库镜像,sessionfactory因为是镜像所以就一份,最好创建一次 
    //通常是线程安全的。 
    sessionfactory factory=cfg.buildsessionfactory(); 
     
    //取的session 
    session session=null; 
     
    try{ 
      session=factory.opensession(); 
      //开启事务 
      session.begintransaction(); 
      user user=new user(); 
      user.setname("张三"); 
      user.setpassword("123"); 
      user.setcreatetime(new date()); 
       
      //保存user对象 
      session.save(user); 
       
      //提交事务 
      session.gettransaction().commit(); 
    }catch(exception e){ 
      e.printstacktrace();//打印错误信息 
      //回滚事务 
      session.gettransaction().rollback(); 
    }finally{ 
      if(session != null){ 
        if(session.isopen()){ 
          //关闭session 
          session.close(); 
        } 
      } 
    } 
  } 
} 

在mysql中查看添加的信息显示如下图:

201676182824926.png (730×203)

上面操作的信息已经写入到数据库中,数据库中的数据在save之后在数据库中生成了相应的行,但是此时还没有真正的保存,而是在数据库中已经有相对应的行数据,当使用session的事务提交完成后才把数据提交到了数据库中。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网