HIbernate CURD操作
阿新 • • 發佈:2017-06-02
import open conf lis mit nat configure color ber
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
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();
}
}
添加: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操作