欢迎访问成都祈钰瑶科技有限公司!

全国免费服务热线:

18982081108
✕

成都网站设计公司,成都响应式网站设计,成都移动端网站设计,成都网站UI设计

hibernate开发步骤

Hibernate框架开发步骤

项目导入需要的jar包:

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

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

编写hibernate.cfg.xml文件

<?xml version='1.0'encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

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

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

<!-- Generatedby MyEclipse Hibernate Tools. -->

<hibernate-configuration>

    <session-factory>

        <!--配置连接数据库信息 -->

        <!--加载数据库的URL -->

        <property name="connection.url">jdbc:MySQL://127.0.0.1:3306/test</property>

        <!--数据库连接名 -->

        <property name="connection.username">root</property>

        <!--数据库的连接密码 -->

        <property name="connection.password">123456</property>

        <!--加载数据库驱动 -->

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>   

             <!--配置Hibernate属性信息 -->

             <!--设置数据库方言 -->

             <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

             <!--后台是否显示SQL语句 -->

             <property name="show_sql">true</property>

             <!--是否格式化SQL语句 -->

             <property name="format_sql">true</property>

             <!--设置数据库生成策略默认为update-->

             <property name="hbm2ddl.auto">update</property>

             <!--加载Hibernate映射文件 -->

             <mapping resource="com/edu/bean/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

编写实体类,创建表

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:

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

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

<hibernate-mapping>

         <!-- name对应实体类,table对应数据库中的表 -->

         <class name="com.edu.bean.User"table="user">

                   <!-- name对应实体类中属性名,column对应表中字段,type为表中字段类型-->

                   <id name="id"column="id">

                            <!--设置主键生成策略 -->

                            <generator class="identity"></generator>

                   </id>

                   <property name="username"type="string"column="username"></property>

                   <property name="password"type="string"column="password"></property>

         </class>

</hibernate-mapping>

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

<!--加载Hibernate映射文件 -->

<mapping resource="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