hibernate的增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate的增删改查相关的知识,希望对你有一定的参考价值。
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&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的增删改查的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate ORM框架——续第一章:Java增删改查与Hibernate的增删改查的对比