1. 程式人生 > >hibernate的增刪改查

hibernate的增刪改查

add .org native interface har res 程序 bean code

1.導入jar包

技術分享

2.編寫相應的配置文件hibernate.cfg.xml

(數據庫的驅動,密碼,用戶名,方言,顯示sql語句等)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/company
?useUnicode=true&amp;characterEncoding=utf-8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="cn/hibernate/bean/Detp.hbm.xml"/>
</session-factory>
</hibernate-configuration>

3.編寫Detp.hbm.xml配置文件

(name是實體類的屬性,column是數據庫表的字段,type是數據庫和實體類的類型,數據庫字段類型必須和實體類型一致)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.hibernate.bean.Detp" table="detp">
<id name="deptNo" column="deptNo">
<!-- 主鍵生成策略 identity:標識列 assigned:程序 native:本地
-->
<generator class="identity"></generator>
</id>
<property name="deptName" column="deptName" type="java.lang.String"></property>
<property name="deptLocation" column="deptLocation" type="java.lang.String"></property>
</class>
</hibernate-mapping>

4.編寫接口DetpDao

package cn.hibernate.dao;

import java.util.List;

import cn.hibernate.bean.Detp;

public interface DetpDao {

/** * 添加detp表的信息 * @param detp * @return */

public int addDetp(Detp detp);

/** * 更新detp表的信息 * @param detp * @return */

public int updateDetp(Detp detp);

/** * 刪除detp表的信息 * @param detp * @return */

public int deteleDetp(Detp detp);

/** * 查詢全部信息 * @return */

public List<Detp> finDetps();

5.DetpDao的實現類DetpDaoImpl

import java.util.ArrayList; import java.util.List;

import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction;

import cn.hibernate.bean.Detp; import cn.hibernate.dao.DetpDao; import cn.hibernate.dao.HibernateUtils;

public class DetpDaoImpl implements DetpDao { private Session session; private Transaction transaction;

@Override public int addDetp(Detp detp) { int num=0; try { session=HibernateUtils.getSession(); transaction = session.beginTransaction(); session.save(detp); transaction.commit(); num=1; } catch (Exception e) { e.printStackTrace(); transaction.rollback(); num=-1; } finally{ HibernateUtils.closeSession(); } return num; }

@Override public int updateDetp(Detp detp) { int num = 0; try { session = HibernateUtils.getSession(); transaction = session.beginTransaction(); session.update(detp); transaction.commit(); num = 1; } catch (Exception e) { e.printStackTrace(); transaction.rollback(); num = -1; }finally{ HibernateUtils.closeSession(); } return num; }

@Override public int deteleDetp(Detp detp) { int num = 0; try { session = HibernateUtils.getSession(); transaction = session.beginTransaction(); session.delete(detp); transaction.commit(); num = 1; } catch (Exception e) { e.printStackTrace(); transaction.rollback(); num = -1; }finally{ HibernateUtils.closeSession(); } return num; }

@Override public List<Detp> finDetps() { List<Detp> Dlist=new ArrayList<Detp>(); String hql="from Detp"; try { session=HibernateUtils.getSession(); Query query=session.createQuery(hql); Dlist =query.list(); } catch (Exception e) { e.printStackTrace(); } finally{ HibernateUtils.closeSession(); } for (Detp detp : Dlist) { System.out.println("編號:"+detp.getDeptNo()+"\t\t部門:"+detp.getDeptName()+"\t\t部門位置:"+detp.getDeptLocation()); } return Dlist; }

6.編寫測試類

public class TestDeom { DetpDao dao = new DetpDaoImpl();

@Test public void tUpdateDeom(){ Detp detp = new Detp(); detp.setDeptNo(6); detp.setDeptName("人力部001"); detp.setDeptLocation("100"); int num = dao.updateDetp(detp); System.out.println(num); } @Test public void tFindDeom(){ List<Detp> detps= dao.finDetps(); System.out.println(detps.size()); } @Test public void tAddDeom(){ Detp detp=new Detp(); detp.setDeptNo(11); detp.setDeptName( "外交部01"); detp.setDeptLocation("102"); dao.addDetp(detp); } @Test public void tDelDeom(){ Detp detp=new Detp(); detp.setDeptNo(5); int num= dao.deteleDetp(detp); System.out.println(num); }

hibernate的增刪改查