千家信息网

hibernate开发步骤

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,Hibernate框架开发步骤项目导入需要的jar包:http://pan.baidu.com/s/1eRQ19C2编写hibernate.cfg.xml文件
千家信息网最后更新 2025年02月01日hibernate开发步骤

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

<hibernate-configuration>

<session-factory>

<property name="connection.url">jdbc:mysql://127.0.0.1:3306/testproperty>

<property name="connection.username">rootproperty>

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

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

<property name="dialect">org.hibernate.dialect.MySQLDialectproperty>

<property name="show_sql">trueproperty>

<property name="format_sql">trueproperty>

<property name="hbm2ddl.auto">updateproperty>

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

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

<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

<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


0