温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

hibernate开发步骤

发布时间:2020-05-18 12:04:10 来源:网络 阅读:470 作者:FujiAtobe 栏目:数据库

Hibernate框架开发步骤

项目导入需要的jar:

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

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

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

<?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

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();

         }

}

代码分层优化:
获取SessionHibernateGetSession:

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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI