Hibernate框架搭建步骤


一、在Web项目中导入Hibernate最小依赖jar包、数据库驱动jar包,如下图所示。



二、在src/config目录下添加Hibernate配置文件hibernate-cfg.xml。


【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 = "myeclipse.connection.profile" >
                com.mysql.jdbc.MySqlDriver
            </ property >
            < property name = "hibernate.connection.username" > root </ property >
            < property name = "hibernate.connection.password" > root </ property >
            < property name = "hibernate.connection.url" >
                jdbc:mysql:// localhost:3306/hibernate_demo?useUnicode=true &amp; characterEncoding= utf-8
            </ property >
            < property name = "hibernate.connection.driver_class" >
                com.mysql.jdbc.Driver
            </ property >
            < property name = "hibernate.dialect" >
                org.hibernate.dialect.MySQLDialect
            </ property >
            < property name = "hibernate.show_sql" > true </ property >
            < property name = "hibernate.hbm2ddl.auto" > validate </ property >
           

            < mapping resource = "Student.hbm.xml" />
      </ session-factory >


</ hibernate-configuration >


三、创建实体类(持久化类)。
【Student.java】对应数据库中的实体

public class Student {
      private int sid ;
      private String sno ;
      private String sname ;
      private String sgender ;
      private String college ;

      public Student() {

     }

      public Student( int sid, String sno, String sname, String sgender,
                String college) {
            super ();
            this . sid = sid;
            this . sno = sno;
            this . sname = sname;
            this . sgender = sgender;
            this . college = college;
     }

      public int getSid() {
            return sid ;
     }

      public void setSid( int sid) {
            this . sid = sid;
     }

      public String getSno() {
            return sno ;
     }

      public void setSno(String sno) {
            this . sno = sno;
     }

      public String getSname() {
            return sname ;
     }

      public void setSname(String sname) {
            this . sname = sname;
     }

      public String getSgender() {
            return sgender ;
     }

      public void setSgender(String sgender) {
            this . sgender = sgender;
     }

      public String getCollege() {
            return college ;
     }

      public void setCollege(String college) {
            this . college = college;
     }

}


四、创建对象-关系映射文件,并在hibernate.config.xml中添加标签<mapping resource="Student.hbm.xml "/>
【Student.hbm.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" >
< hibernate-mapping package = "com.demo.hibernate.entity" >
      < class name = "Student" table = "student" >
     
            < id name = "sid" type = "int" >
                 < column name = "sid" ></ column >
                 < generator class = "increment" ></ generator >
            </ id >
           
            < property name = "sno" type = "java.lang.String" >
                 < column name = "sno" ></ column >
            </ property >
           
            < property name = "sname" type = "java.lang.String" >
                 < column name = "sname" ></ column >
            </ property >
           
            < property name = "sgender" type = "java.lang.String" >
                 < column name = "sgender" ></ column >
            </ property >
           
            < property name = "college" type = "java.lang.String" >
                 < column name = "college" ></ column >
            </ property >

      </ class >
</ hibernate-mapping >

   

五、使用Hibernate API编写代码操作数据库

【CustomSessionFactory.java】

public class CustomSessionFactory {
      private static SessionFactory sessionFactory ;
      private static Session session ;
      private static Transaction transaction ;
     
      public static Session getSession(){
            init();
            return session ;
     }
     
      public static void init(){
            //创建配置对象
                     Configuration config= new Configuration().configure();
                      //创建服务配置对象
                     ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
                      //创建会话工厂对象
                      sessionFactory =config.buildSessionFactory(serviceRegistry);
                      //创建会话对象
                      session = sessionFactory .openSession();
                      //开启事务
                      transaction = session .beginTransaction();
     }
     
      public static void closeSession(){
            //提交事务
            transaction .commit();
            //关闭会话
            session .close();
            //关闭会话工厂
            sessionFactory .close();
     }

}

eg:保存一个Student对象到数据库:
【StudentDaoIml.java】

public class StudentDaoImpl implements StudentDao{

@Override
public void saveStudent(Student student) {
Session session=CustomSessionFactory.getSession();
session.save(student);
CustomSessionFactory.closeSession();
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值