Hibernate 使用DetachedCriteria操作

如题:

建立一个Book类模拟图书馆高级搜索(使用DetachedCriteria操作),包含动态查找。
  name 书名
  author作者
  decribe 描述(模糊找主题词)
  publish 出版社

 

Book 实体类

package entity;

/**
 * Book 实体类
 * */
public class Book {
	private int id; // 编号
	private String name;// 作者
	private String author;// 书名
	private String decribe;// 描述
	private String publish;// 出版社
	/*
	 * 构造方法
	 */
	public Book() {
	}
	public Book(String name, String author, String decribe, String publish) {
		this.name = name;
		this.author = author;
		this.decribe = decribe;
		this.publish = publish;
	}
	/*
	 * 封装方法
	 */
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getDecribe() {
		return decribe;
	}
	public void setDecribe(String decribe) {
		this.decribe = decribe;
	}
	public String getPublish() {
		return publish;
	}
	public void setPublish(String publish) {
		this.publish = publish;
	}
}

 

Book 实体类的XML配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="entity.Book">
		<id name="id">
			<generator class="sequence">
				<param name="sequence">seq_book</param>
			</generator>
		</id>
		<property name="name"></property>
		<property name="author"></property>
		<property name="decribe"></property>
		<property name="publish"></property>
	</class>
</hibernate-mapping>

 

hibernate.cfg.xml 为配置文件进行映射

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
	<!-- 数据库URL -->
	<property name="connection.url">
		jdbc:oracle:thin:@localhost:1521:oracle11
	</property>
	<!-- 数据库用户 -->
	<property name="connection.username">A_hr</property>
	<!-- 数据库用户密码 -->
	<property name="connection.password">123456</property>
	<!-- 数据库 JDBC 驱动 -->
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<!-- 是否将运行期生成的 SQL 输出到日志以供调试 -->
	<property name="show_sql">true</property>
	<!-- 每个数据库都有其对应的 Dialect 以匹配其平台特征 -->
	<property name="dialect">
		org.hibernate.dialect.Oracle10gDialect
	</property>
	<property name="hbm2ddl.auto">create</property>
	<mapping resource="entity/Book.hbm.xml" />
</session-factory>
</hibernate-configuration>

 

进行数据操作

package test;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import entity.Book;

import util.HibernateSessionFactory;

/**
 * 建立一个Book类模拟图书馆高级搜索(使用DetachedCriteria操作),包含动态查找。
 * */
public class DetachedCriteriaTest {
	public static void main(String[] args) {
		add();// 添加数据
		// 动态查找
		Book searchBook = new Book();
		searchBook.setId(1);
		searchBook.setName("java开发实战经典");
		searchBook.setAuthor("李兴华");
		searchBook.setDecribe("通俗易懂的语言");
		searchBook.setDecribe(null);
		// 使用DetachedCriteria操作
		detachedCriteriaSearch(searchBook);
	}

	// 添加数据
	private static void add() {
		// 实例化 HibernateSessionFactory 工厂类
		Session session = HibernateSessionFactory.getSession();
		// 创建事务(增删改用事务)
		Transaction tx = null;
		try {
			// 打开事务
			tx = session.beginTransaction();
			// 实例化 Book 对象并传参
			Book book1 = new Book("java开发实战经典", "李兴华", "本书用通俗易懂的语言和丰富多彩的实例",
					"网易出版社");
			Book book2 = new Book("PHP开发实战经典", "何开", "本书用通俗易懂的语言和丰富多彩的实例",
					"新浪出版社");
			Book book3 = new Book("C#开发实战经典", "李兴华", "这是一本书", "新浪出版社");
			Book book4 = new Book("C++开发实战经典", "李炎恢", "这还是一本书", "网易出版社");
			// 发送 insert Sql 语句
			session.save(book1);
			session.save(book2);
			session.save(book3);
			session.save(book4);
			// 提交事务
			tx.commit();
			System.out.println("成功!");
		} catch (HibernateException e) {
			e.printStackTrace();
			// 回滚事务
			tx.rollback();
		} finally {
			// 关闭 session,释放资源
			HibernateSessionFactory.closeSession();
		}
	}
	// 使用DetachedCriteria操作
	private static void detachedCriteriaSearch(Book searchBook) {
		//实例化 DetachedCriteria
		DetachedCriteria dc = DetachedCriteria.forClass(Book.class);
		try {
			/*
			 * 判断参数是否为空,为空则不添加到 DetachedCriteria 的对象里面去
			 * */
			if (searchBook.getId() != 0) {
				dc.add(Restrictions.eq("id", searchBook.getId()));
			}
			if (searchBook.getName() != null) {
				dc.add(Restrictions.eq("name", searchBook.getName()));
			}
			if (searchBook.getAuthor() != null) {
				dc.add(Restrictions.eq("author", searchBook.getAuthor()));
			}
			if (searchBook.getDecribe() != null) {
				dc.add(Restrictions.like("decribe",
						"%" + searchBook.getDecribe() + "%"));
			}
			if (searchBook.getPublish() != null) {
				dc.add(Restrictions.eq("publish", searchBook.getPublish()));
			}
			// 线程休眠3秒
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//实例化 Criteria,实际占用session从这里开始
		Criteria critera = dc.getExecutableCriteria(HibernateSessionFactory
				.getSession());
		List<Book> books = critera.list();
		System.out.println("================显示列表结果==================");
		//打印结果
		for (Book book : books) {
			System.out.println(book.getId() + " " + book.getName() + " "
					+ book.getAuthor() + " " + book.getDecribe() + " "
					+ book.getPublish());
		}
		System.out.println("==========================================");
	}
}

 

效果图:
Hibernate 使用DetachedCriteria操作


Hibernate 使用DetachedCriteria操作
 

 

你可能感兴趣的:(Hibernate)