标签:工厂 dem class import oid ccf .class update .config
添加: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();
}
}
标签:工厂 dem class import oid ccf .class update .config
原文地址:http://www.cnblogs.com/MisterLiu/p/6932994.html