hibernate之基本操作(增,删,改,查)

来源:互联网 发布:正确的日干支推算法 编辑:程序博客网 时间:2024/05/18 04:42
 编写配置文件 hibernate-cfg.xml 
   <?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
<!-- 方言 --> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<!-- 数据库连接 --> 
<property name="connection.url">jdbc:mysql://localhost:3306/db_database14</property> 
<!-- 数据库连接用户名 --> 
<property name="connection.username">root</property> 
<!-- 数据库连接密码 --> 
<property name="connection.password">admin</property> 
<!-- 数据库驱动 --> 
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
<!-- 打印SQL语句 --> 
<property name="show_sql">true</property> 

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

<!-- 映射文件 --> 
<mapping resource="com/java0806/Medicine.hbm.xml" /> 
</session-factory> 
</hibernate-configuration> 


2 编写工具类 hibernateUtil.java 

package com.java0806.util; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 

/** 
* Hibernate初始化类,用于获取Session、SessionFactory 及关闭Session 
* @author Li Yong Qiang 
*/ 
public class HibernateUtil { 
// SessionFactory对象 
private static SessionFactory factory = null; 
// 静态块 
static { 
try { 
// 加载Hibernate配置文件 
Configuration cfg = new Configuration().configure(); 
// 实例化SessionFactory 
factory = cfg.buildSessionFactory(); 
} catch (HibernateException e) { 
e.printStackTrace(); 


/** 
* 获取Session对象 
* @return Session对象 
*/ 
public static Session getSession() { 
//如果SessionFacroty不为空,则开启Session 
Session session = (factory != null) ? factory.openSession() : null; 
return session; 

/** 
* 获取SessionFactory对象 
* @return SessionFactory对象 
*/ 
public static SessionFactory getSessionFactory() { 
return factory; 

/** 
* 关闭Session 
* @param session对象 
*/ 
public static void closeSession(Session session) { 
if (session != null) { 
if (session.isOpen()) { 
session.close(); // 关闭Session 






3 编写实体类 

package com.java0806; 
/** 
* 药品持久化类 
* @author Li Yong Qiang 
*/ 
public class Medicine { 
private Integer id; //id号 
private String name; //药品名称 
private double price; //价格 
private String factoryAdd; //出厂地址 
private String Description; //描述 
public Integer getId() { 
return id; 

public void setId(Integer id) { 
this.id = id; 

public String getName() { 
return name; 

public void setName(String name) { 
this.name = name; 

public double getPrice() { 
return price; 

public void setPrice(double price) { 
this.price = price; 

public String getFactoryAdd() { 
return factoryAdd; 

public void setFactoryAdd(String factoryAdd) { 
this.factoryAdd = factoryAdd; 

public String getDescription() { 
return Description; 

public void setDescription(String description) { 
Description = description; 




4编写映射文件 

<?xml version="1.0"?> 
<!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.lyq.model.Medicine" table="tb_medicine_select_get"> 
<id name="id"> 
<generator class="native"/> 
</id> 
<property name="name" not-null="true" length="200" /> 
<property name="price" not-null="true"/> 
<property name="factoryAdd" length="200"/> 
<property name="description" /> 
</class> 
</hibernate-mapping> 


5编写测试类 


package com.lyq.util; 

import org.hibernate.Session; 

import com.lyq.model.Medicine; 
/** 
* 增,删,改,查 
* @author Li Yong Qiang 
*/ 
public class Update { 
public static void main(String[] args) { 
Session session = null; //声明Session对象 
try { 
//获取Session 
session = HibernateUtil.getSession(); 
//开启事物 
session.beginTransaction(); 
/* 
// 删除 
Medicine medicine=(Medicine) session.load(Medicine.class,new Integer(1)); 
session.delete(medicine);*/ 

/*//添加数据 
            //实例化药品对象,并对其属性赋值 
Medicine medicine = new Medicine(); 
medicine.setId(2); 
medicine.setName("xxx感冒药"); 
medicine.setPrice(102.00); 
medicine.setFactoryAdd("制药一厂xxx"); 
medicine.setDescription("最新感冒药"); 
//保存药品对象 
session.save(medicine);*/ 


           /* 
              //两种查询方式 
Medicine medicine = (Medicine)session.get(Medicine.class, new Integer(1)); 
Medicine medicine =(Medicine)session.load(Medicine.class,new Integer(1)); 
System.out.println("药品ID:" + medicine.getId()); 
System.out.println("药品名称:" + medicine.getName());*/ 

//自动修改 
/*Medicine medicine = (Medicine)session.get(Medicine.class, new Integer(1)); 
medicine.setName("感冒胶囊"); //修改药品名称 
medicine.setPrice(10.05);*/ 


//手动修改 
/*Medicine medicine = new Medicine(); 
medicine.setId(1); 
medicine.setName("感冒药xx"); 
medicine.setPrice(12.00); 
medicine.setFactoryAdd("制药一厂"); 
medicine.setDescription("最新感冒药"); 
session.update(medicine);*/ 
//提交事物 
session.getTransaction().commit(); 
} catch (Exception e) { 
e.printStackTrace(); 
//出错将回滚事物 
session.getTransaction().rollback(); 
}finally{ 
//关闭Session对象 
HibernateUtil.closeSession(session); 


}
0 0
原创粉丝点击