hibernate开发步骤-创新互联

Hibernate框架开发步骤

项目导入需要的jar包:

http://pan.baidu.com/s/1eRQ19C2

专注于为中小企业提供成都做网站、网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业池州免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
编写hibernate.cfg.xml文件

'1.0'encoding='UTF-8'?>

         "-//Hibernate/Hibernate Configuration DTD3.0//EN"

         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

   

       

       

       "connection.url">jdbc:mysql://127.0.0.1:3306/test

       

       "connection.username">root

       

       "connection.password">123456

       

       "connection.driver_class">com.mysql.jdbc.Driver

           

           

           "dialect">org.hibernate.dialect.MySQLDialect

           

           "show_sql">true

           

           "format_sql">true

           

           "hbm2ddl.auto">update

           

           "com/edu/bean/User.hbm.xml"/>

   

编写实体类,创建表

eg:

package com.edu.bean;

public class User {

        private intid;

        private String username;

        private String password;

        public intgetId() {

                  return id;

        }

        public voidsetId(int id) {

                  this.id =id;

        }

        public String getUsername() {

                  return username;

        }

        public voidsetUsername(String username) {

                  this.username= username;

        }

        public String getPassword() {

                  return password;

        }

        public voidsetPassword(String password) {

                  this.password= password;

        }

public User(Stringusername, String password) {

                  super();

                  this.username= username;

                  this.password= password;

        }

        public User() {

                  //TODO Auto-generated constructor stub

        }

}

编写映射文件

eg:

"1.0"encoding="UTF-8"?>

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

        

        "com.edu.bean.User"table="user">

                  

                  "id"column="id">

                           

                           "identity">

                  

                  "username"type="string"column="username">

                  "password"type="string"column="password">

        

将映射文件放入到hibernate.cfg.xml

"com/edu/bean/User.hbm.xml"/>

编写代码:

eg:

package com.edu.test;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.junit.Test;

import com.edu.bean.User;

public class HibernateTest {

        @Test

        public voidtest(){

                  //读取hibernate.cfg.xml主配置文件

                  Configurationcfg=new Configuration().configure("hibernate.cfg.xml");

                  //创建SessionFactory工厂

                  SessionFactorysf=cfg.buildSessionFactory();

                  //获取session

                  Sessionsession=sf.openSession();

                  //创建事务

                  Transactiontc=session.beginTransaction();

                  //创建User对象

                  Useruser=new User("张三", "123456");

                  //持久化对象

                  try{

                           //保存数据

                           session.save(user);

                           //提交事务(不可少)

                           tc.commit();

                  }catch(Exception e){

                           //数据操作失败,回滚事务

                           tc.rollback();

                  }

                  //关闭session

                  session.close();

        }

}

代码分层优化:
获取Session的HibernateGetSession类:

package com.edu.dbconn;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateGetSession {

        //保证SessionFactory工厂只创建一次

        private staticSessionFactory sf;

        static{

                  if(sf==null){

                           //读取hibernate.cfg.xml主配置文件

                           Configurationcfg=new Configuration().configure("hibernate.cfg.xml");

                           //创建SessionFactory工厂

                           sf=cfg.buildSessionFactory();

                  }

        }

        public staticSession getSession(){

                  //创建session

                  Sessionsession=sf.openSession();

                  return session;

        }

}

Dao层的UserDao类:

package com.edu.dao;

import org.hibernate.Session;

import org.hibernate.Transaction;

import com.edu.bean.User;

import com.edu.dbconn.HibernateGetSession;

public class UserDao {

        public voidsaveUser(){

                  //获取session

                  Sessionsession=HibernateGetSession.getSession();

                  //创建事务

                  Transactiontc=session.beginTransaction();

                  //创建User对象

                  Useruser=new User("李四", "123456");

                  //持久化对象

                  try{

                           //保存数据

                           session.save(user);

                           //提交事务(不可少)

                           tc.commit();

                  }catch(Exception e){

                           //数据操作失败,回滚事务

                           tc.rollback();

                  }

                  //关闭session

                  session.close();

        }

}

测试层:

package com.edu.test;

import org.junit.Test;

import com.edu.dao.UserDao;

public class HibernateTest {

        @Test

        public voidtest(){

                  UserDaoud=new UserDao();

                  ud.saveUser();

        }

}

实例源码:

http://pan.baidu.com/s/1eRPLFJO

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:hibernate开发步骤-创新互联
文章起源:http://ybzwz.com/article/eoioj.html