码迷,mamicode.com
首页 > Web开发 > 详细

HIbernate CURD操作

时间:2017-06-02 14:53:43      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:工厂   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();
}
}

 

HIbernate CURD操作

标签:工厂   dem   class   import   oid   ccf   .class   update   .config   

原文地址:http://www.cnblogs.com/MisterLiu/p/6932994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!