Hibernate入门 CRUD

Hibernate入门 小测 实现数据库的简单增删改查


package com;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HelloWorldApp {
 @Test
 public void TestSQLQueryAll() {
  // 实例化配置对象,加载hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 创建会话连接工厂
  SessionFactory sf = configuration.buildSessionFactory();
  // 创建会话
  Session session = sf.openSession();
  // 开启事务
  Transaction transaction = session.beginTransaction();
  String sql = "select * from customer";
  SQLQuery sqlQuery = session.createSQLQuery(sql);
  List<Object[]> resultCustomers = sqlQuery.list();
  for (Object[] customers : resultCustomers) {
   System.out.println(Arrays.toString(customers));
  }
  // 提交事务
  transaction.commit();
  // 关闭会话连接
  session.close();
  // 关闭会化工厂
  sf.close();
 }
 @Test
 public void TestHQLQueryAll() {
  // 实例化配置对象,加载hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 创建会话连接工厂
  SessionFactory sf = configuration.buildSessionFactory();
  // 创建会话
  Session session = sf.openSession();
  // 开启事务
  Transaction transaction = session.beginTransaction();
  String hql = "from Customer";
  Query query = session.createQuery(hql);
  List<Customer> resultCustomers = query.list();
  for (Customer customer : resultCustomers) {
   System.out.println(customer);
  }
  // 提交事务
  transaction.commit();
  // 关闭会话连接
  session.close();
  // 关闭会化工厂
  sf.close();
 }
 @Test
 public void TestDelete() {
  // 实例化配置对象,加载hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 创建会话连接工厂
  SessionFactory sf = configuration.buildSessionFactory();
  // 创建会话
  Session session = sf.openSession();
  // 开启事务
  Transaction transaction = session.beginTransaction();
  Customer customer = new Customer();
  customer.setId(1);
  session.delete(customer);
  // 提交事务
  transaction.commit();
  // 关闭会话连接
  session.close();
  // 关闭会化工厂
  sf.close();
 }
 @Test
 public void TestUpdate() {
  // 实例化配置对象,加载hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 创建会话连接工厂
  SessionFactory sf = configuration.buildSessionFactory();
  // 创建会话
  Session session = sf.openSession();
  // 开启事务
  Transaction transaction = session.beginTransaction();
  Customer customer = new Customer();
  customer.setId(2);
  customer.setCity("tj");
  customer.setName("李雷");
  customer.setAge(23);
  session.update(customer);
  // 提交事务
  transaction.commit();
  // 关闭会话连接
  session.close();
  // 关闭会化工厂
  sf.close();
 }
 @Test
 public void TestInsert() {
  // 实例化配置对象,加载hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 创建会话连接工厂
  SessionFactory sf = configuration.buildSessionFactory();
  // 创建会话
  Session session = sf.openSession();
  // 开启事务
  Transaction transaction = session.beginTransaction();
  Customer customer = new Customer();
  customer.setAge(32);
  customer.setCity("hb");
  customer.setName("李连杰");
  session.save(customer);
  // 提交事务
  transaction.commit();
  // 关闭会话连接
  session.close();
  // 关闭会化工厂
  sf.close();
 }
 @Test
 public void TestSelect() {
  // 实例化配置对象,加载hibernate.cfg.xml
  Configuration configuration = new Configuration().configure();
  // 创建会话连接工厂
  SessionFactory sf = configuration.buildSessionFactory();
  // 创建会话
  Session session = sf.openSession();
  // 开启事务
  Transaction transaction = session.beginTransaction();
  int id = 1;
  Customer result = (Customer) session.get(Customer.class, id);
  System.out.println(result);
  // 提交事务
  transaction.commit();
  // 关闭会话连接
  session.close();
  // 关闭会化工厂
  sf.close();
 }
}


你可能感兴趣的:(数据库,Web,import)