大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

hibernate开发步骤

Hibernate框架开发步骤

项目导入需要的jar包:

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

康平网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联

编写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:

packagecom.edu.bean;

 

publicclassUser {

         privateintid;

         privateString username;

         privateString password;

         publicintgetId() {

                   returnid;

         }

         publicvoidsetId(intid) {

                   this.id =id;

         }

         publicString getUsername() {

                   returnusername;

         }

         publicvoidsetUsername(String username) {

                   this.username= username;

         }

         publicString getPassword() {

                   returnpassword;

         }

         publicvoidsetPassword(String password) {

                   this.password= password;

         }

publicUser(Stringusername, String password) {

                   super();

                   this.username= username;

                   this.password= password;

         }

         publicUser() {

                   //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:

packagecom.edu.test;

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

importorg.hibernate.Transaction;

importorg.hibernate.cfg.Configuration;

importorg.junit.Test;

importcom.edu.bean.User;

publicclassHibernateTest {

         @Test

         publicvoidtest(){

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

                   Configurationcfg=newConfiguration().configure("hibernate.cfg.xml");

                   //创建SessionFactory工厂

                   SessionFactorysf=cfg.buildSessionFactory();

                   //获取session

                   Sessionsession=sf.openSession();

                   //创建事务

                   Transactiontc=session.beginTransaction();

                   //创建User对象

                   Useruser=newUser("张三", "123456");

                   //持久化对象

                   try{

                            //保存数据

                            session.save(user);

                            //提交事务(不可少)

                            tc.commit();

                   }catch(Exception e){

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

                            tc.rollback();

                   }

                   //关闭session

                   session.close();

         }

}

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

packagecom.edu.dbconn;

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

importorg.hibernate.cfg.Configuration;

publicclassHibernateGetSession {

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

         privatestaticSessionFactory sf;

         static{

                   if(sf==null){

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

                            Configurationcfg=newConfiguration().configure("hibernate.cfg.xml");

                            //创建SessionFactory工厂

                            sf=cfg.buildSessionFactory();

                   }

         }

         publicstaticSession getSession(){

                   //创建session

                   Sessionsession=sf.openSession();

                   returnsession;       

         }

}

Dao层的UserDao类:

packagecom.edu.dao;

importorg.hibernate.Session;

importorg.hibernate.Transaction;

importcom.edu.bean.User;

importcom.edu.dbconn.HibernateGetSession;

 

publicclassUserDao {

         publicvoidsaveUser(){

                   //获取session

                   Sessionsession=HibernateGetSession.getSession();

                   //创建事务

                   Transactiontc=session.beginTransaction();

                   //创建User对象

                   Useruser=newUser("李四", "123456");

                   //持久化对象

                   try{

                            //保存数据

                            session.save(user);

                            //提交事务(不可少)

                            tc.commit();

                   }catch(Exception e){

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

                            tc.rollback();

                   }

                   //关闭session

                   session.close();

         }

}

测试层:

packagecom.edu.test;

importorg.junit.Test;

importcom.edu.dao.UserDao;

publicclassHibernateTest {

         @Test

         publicvoidtest(){

                   UserDaoud=newUserDao();

                   ud.saveUser();

         }

}

实例源码:

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


本文题目:hibernate开发步骤
URL分享:http://dzwzjz.com/article/gjpoii.html
在线咨询
服务热线
服务热线:028-86922220
TOP