⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 businessservice.java

📁 hibernate的经典练习
💻 JAVA
字号:
package com.ghy.test7;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
import java.sql.*;

// 二级缓存
public class BusinessService {
	public static SessionFactory sessionFactory;
	static {
		try {
			// Create a configuration based on the properties file we've put
			Configuration config = new Configuration();
			config.addClass(User.class);

			// Get the session factory we can use for persistence
			sessionFactory = config.buildSessionFactory();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void saveUsers() {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();

			for (int i = 0; i < 2000; i++) {
				
				User user = new User("ghy"+i,i);
				session.save(user);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				// Something went wrong; discard all partial changes
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			// No matter what, close the session
			session.close();
		}
	}

	public void batchUpdateUser() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session
					.createQuery("from User c where c.number>:number");
			query.setInteger("number", 0);
			Iterator usrs = query.iterate();

			while (usrs.hasNext()) {
				User usr = (User) usrs.next();
				usr.setNumber(usr.getNumber() + 1);
				System.out.println(usr.getName());
				session.flush();
				session.evict(usr);
				System.out.println("������session����� ==========="
						+ usr.getName());
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				// Something went wrong; discard all partial changes
				tx.rollback();
			}
			throw e;
		} finally {
			// No matter what, close the session
			session.close();
		}
	}

	public void deleteUser()
	{
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session
					.createQuery("from User c where c.number>:number");
			query.setInteger("number", 100);
			Iterator users = query.iterate();

			while (users.hasNext()) {
				User user = (User) users.next();
				session.delete(user);
				session.flush();
				session.evict(user);
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				// Something went wrong; discard all partial changes
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			// No matter what, close the session
			session.close();
		}
	}
	public void useQueryCache() throws Exception {
		Session session = sessionFactory.openSession();
		Transaction tx = null;
		try {

			Query usrByAgeQuery = session
					.createQuery("from User c where c.number>:number");
			usrByAgeQuery.setInteger("number", 0);
			usrByAgeQuery.setCacheable(true);
			usrByAgeQuery.setCacheRegion("usrQueries");

			for (int i = 0; i < 100; i++) {
				System.out.println("useQueryCache开始查找 ===========");
				System.out.println(usrByAgeQuery.list().size());
				System.out.println("结束useQueryCache查找 ===========");
			}

		} catch (Exception e) {
			if (tx != null) {
				// Something went wrong; discard all partial changes
				tx.rollback();
			}
			throw e;
		} finally {
			// No matter what, close the session
			session.close();
		}
	}

	public void test() throws Exception {

//		saveUsers();
//		deleteUser();
//		batchUpdateUser();
		useQueryCache();
	}

	public static void main(String args[]) throws Exception {
		new BusinessService().test();
		Thread.sleep(10000);
		sessionFactory.close();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -