1.首先还是需要搭建hibernate环境
我之间博客有写到在这里不再重复。
2.写一个IhibernateDao接口定义我们要实现
3.写一个接口实现类实现我们定义的方法
public class HibernateImpl implements IhibernateDao {
/**
* 根据id查询数据
* @param
* @return List<Student>
*/
@Override
public List<Student> getById(Student stu, Serializable id) {
List<Student> stus=new ArrayList<>();
Session session = Util.getSession();
//开启事务
Transaction tx=session.beginTransaction();
tx.begin();
Criteria criteria = session.createCriteria(Student.class);
//添加约束
criteria.add(Restrictions.eq("id",1));
stus= criteria.list();
session.close();
return stus;
}
/**
* 根据名字查询(模糊查询)
* @param
* @return List<Student>
*/
@Override
public List<Student> getByName(String name) {
Transaction tx=null;
Query query=null;
Session session = Util.getSession();
//
// Criteria criteria = session.createCriteria(Student.class);
// //设置参数
// criteria.add(Restrictions.like("name","%"+name+"%"));
// stus = criteria.list();
try {
tx = session.beginTransaction();
query = session
.createQuery("from Student where name like ?");
// 执行模糊查询 参数从0开始
query.setParameter(0,"%"+name+"%");
} catch (Exception e) {
throw new RuntimeException(e);
}
finally{
tx.commit();
session.close();
}
return query.list();
}
/**
* 查询所有
* @param stu
* @return List<Student>
*/
@Override
public List<Student> getAll(Student stu) {
Query q=null;
Transaction tx=null;
try {
Session session = Util.getSession();
tx=session.beginTransaction();
q = session.createQuery("from Student");
} catch (HibernateException e) {
e.printStackTrace();
}
return q.list();
}
/**
* 插入数据
*/
@Override
public void save(Student stu) {
Session session=null;
Transaction tx=null;
try {
session = Util.getSession();
tx = session.beginTransaction();
session.save(stu);
System.out.println("插入成功");
} catch (HibernateException e) {
// TODO Auto-generated catch block
tx.rollback();
e.printStackTrace();
}
finally{
tx.commit();
session.close();
}
}
/**
* 更新数据
*/
@Override
public void update(Student stu) {
Query q=null;
Session session=null;
Transaction tx=null;
try {
session = Util.getSession();
tx=session.beginTransaction();
//更新数据必须要有主键
session.update(stu);
System.out.println("更新成功");
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}finally{
tx.commit();
session.close();
}
}
/**
* 删除数据
*/
@Override
public void delete(int id) {
Transaction tx=null;
Query q=null;
Session session=null;
try {
session = Util.getSession();
//开启事务
tx=session.beginTransaction();
// //先查询不为空再删除
Object obj = session.get(Student.class, id);
if(obj!=null){
session.delete(obj);
System.out.println("删除成功");
}
} catch (Exception e) {
//回滚事务
tx.rollback();
e.printStackTrace();
}finally{
//提交事务
tx.commit();
session.close();
}
}
/**
* 分页查询
* @param
* @return List<Student>
*/
@Override
public List<Student> getPage(int index, int count) {
Query q=null;
Session session=null;
Transaction tx=null;
List<Student> list=null;
try {
session = Util.getSession();
tx=session.beginTransaction();
q = session.createQuery("from Student");
q.setFirstResult(index);//从第index+1条(下标从0开始)开始
q.setMaxResults(count);//查询count条
list=q.list();
} catch (Exception e) {
e.printStackTrace();
}finally{
tx.commit();
session.close();
}
return list;
}
}
## 4.测试 ##
public class Stu_implTest {
HibernateImpl hibrenate = new HibernateImpl();
@Test
public void testGetById() {
System.out.println(hibrenate.getById(new Student(),1));
}
@Test
public void testGetPage() {
System.out.println(hibrenate.getPage(0,2));
}
@Test
public void testGetByName() {
System.out.println(hibrenate.getByName(“w”));
}
@Test
public void testGetAll() {
System.out.println(hibrenate.getAll(new Student()));
}
@Test
public void testSave() {
hibrenate.save(new Student(“zhangsan”,22));
}
@Test
public void testUpdate() {
hibrenate.update(new Student(1,”ding “,21));
}
@Test
public void testDelete() {
hibrenate.delete(100);
}
}
“`