之前的博客中已经已经讲过如何配置环境,如何防止多线程 得到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):
数据库变化:
总结:在开发过程中,使用接口能够更加方便代码的修改和日后的维护。随着以后开发经验的增加,我们就会知道使用接口的好处。