HIbernate CURD操作

來源:互聯網
上載者:User

標籤:工廠   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操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.