运用hibernate 通过接口实现增删改查方法。

本文档介绍了如何通过Hibernate接口进行增删改查操作。首先创建Person实体类和映射文件,然后定义PersonDao接口及其实现类PersonDaoImpl。接着,利用HibernateUtil获取SessionFactory。测试类中分别演示了增加、查询、删除和更新操作,展示了接口在代码维护中的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前的博客中已经已经讲过如何配置环境,如何防止多线程 得到SessionFactory等等,所以这里就不一一介绍了

首先创建Person实体类,配置Person.hbm.xml映射文件,创建PersonDao借口,创建PersonDaoImpl实现PersonDao中的方法,创建HibernateUtil类,获取当前的SessionFactory对象,最后编写测试类。

PersonDao接口:

package com.DAO;

import com.ldy.entity.Person;


public interface PersonDao {
	public Person selectPerson(int id);
	public void updatePerson(int id,Person person);
	public void daletePerson(int id);
	public void addPerson(Person person);
}

PersonDaoImpl具体实现增删改查的方法:

package com.DAO;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


import com.ldy.entity.Person;

public class PersonDaoImpl implements PersonDao {

	public Person selectPerson(int id) {
		// TODO Auto-generated method stub
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				Person p = new Person();
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();			
					p = session.get(Person.class,id);
					System.out.println(p.toString());
				} catch (HibernateException e) {
					// TODO Auto-generated catch block
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
				}		
	return p;
	}

	public void updatePerson(int id,Person person) {
		// TODO Auto-generated method stub
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();
					person.setId(id);
					session.update(person);
					System.out.println(person.getId());
					ts.commit();
				} catch (HibernateException e) {
					// TODO Auto-generated catch block
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
				}
		
	}

	public void daletePerson(int id) {
		// TODO Auto-generated method stub
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();
					Person p = session.get(Person.class, id);
					session.delete(p);
					System.out.println(p.getId());
					ts.commit();
				} catch (HibernateException e) {
					// TODO Auto-generated catch block
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
				}
	
	}

	public void  addPerson(Person person) {
		// TODO Auto-generated method stub
				SessionFactory sf = null;		
				Session session = null;
				Transaction ts = null;
				System.out.println("1");
				try {
		  			sf = HibernateUtil.getSessionFactory();
					session = sf.getCurrentSession();
					ts = session.beginTransaction();			
					session.save(person);
					System.out.println(person.toString());
					ts.commit();
				} catch (HibernateException e) {
					// TODO Auto-generated catch block
					if(ts != null)
					{
						ts.rollback();
					}
					e.printStackTrace();
		}
	}

}

编写测试类:

package com.DAO;

import org.junit.Test;

import com.ldy.entity.Person;

public class TestTest {
	PersonDaoImpl personDaoImpl = new PersonDaoImpl();
	@Test
	public void testadd(){
			Person p = new Person("lidongyu",90,"123456");
			personDaoImpl.addPerson(p);	
		}
	@Test
	public void testselect(){	
		
		personDaoImpl.selectPerson(1);
	}
	@Test
	public void testdel(){		
		personDaoImpl.daletePerson(1);	
	}
	@Test
	public void testupdate(){
		Person p = new Person("zhangsan",91,"000000");
	}
}

执行增加方法:

数据库变化:

执行查询方法(查询id为3的):


执行删除方法(删除id为2):


数据库变化:

执行更新方法(更新id为3):

数据库变化:

总结:在开发过程中,使用接口能够更加方便代码的修改和日后的维护。随着以后开发经验的增加,我们就会知道使用接口的好处。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值