HIbernate CURD操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HIbernate CURD操作相关的知识,希望对你有一定的参考价值。

添加:save()

更新:update()

删除:delete()

通过id查询:get(*.class,id)

查询所有:session.createQuery(“from User”).list()

分页:

query.setFirstResult()开始索引号

query.setMaxResult()每页显示个数

代码如下:

package cn.hibernate.test;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import cn.hibernate.bean.User;

public class UserCURD {

@Test
public void demo1(){
//更新
User user = new User();
user.setUserId(1);
user.setUserName("吊炸天");
user.setUserPassword("就是这么叼");

Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
session.update(user);
transaction.commit(); //生成sql语句:update user set userName=?, userPassword=? where userId=?
session.close();
factory.close();

}

@Test
public void demo2(){
//删除
User user = new User();
user.setUserId(2);

//4步 获取配置对象 、session工厂、获取session、开启事物
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();

//操作
session.delete(user);

//3步 提交 、关闭
transaction.commit();
session.close();
factory.close();
}

@Test
public void demo3(){
//通过id查询


Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();

User user =(User)session.get(User.class, 1);//必须强制类型转换,get()返回Object类型
System.out.println(user);

transaction.commit();
session.close();
}

@Test
public void demo4(){
//查询所有
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
//需要通过HQL语句查询所有,HQL:表示hibernate查询语句,面向对象的查询语句,类似sql语句。注意:必须使用对象
//session.createQuery("from cn.hibernate.bean.User")
Query query = session.createQuery("from User");
List<User> user = query.list();
for (User user2 : user) {
System.out.println(user2);
}
transaction.commit();
session.close();
}


@Test
public void demo5(){
//分页
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from User");
//确定分页数据 回顾mysql分页:select ... from User limit 0,0

//第一页
//query.setFitstResult(0)
//query.setMaxResults(2)


//第二页
query.setFirstResult(2);
query.setMaxResults(2);
List<User> user = query.list();
for (User user2 : user) {
System.out.println(user2);
}
transaction.commit();
session.close();
}
}

 














































































































以上是关于HIbernate CURD操作的主要内容,如果未能解决你的问题,请参考以下文章

无法提交断点LineBreakpoint hibernate4CURD : -1, 原因是: 找不到 URL 'file:/E:/版本控制/Design-java/hibernate4CURD

Hibernate的配置及CURD

Hibernate的入门Curd用法

hibernate-笔记

hibernate总结

Hibernate框架的配置