码迷,mamicode.com
首页 > Web开发 > 详细

hibernate的增删改查

时间:2017-10-07 18:45:46      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:column   roo   type   factory   mat   bean   .hbm.xml   pack   cte   

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的增删改查

标签:column   roo   type   factory   mat   bean   .hbm.xml   pack   cte   

原文地址:http://www.cnblogs.com/BuleSky001/p/7635157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!