bookmessageop.java

来自「在eclipse平台下写的」· Java 代码 · 共 59 行

JAVA
59
字号
/**
 * 
 */
package com.internetshop.hibernate.book_message;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * @author Echo_Me
 *
 */
public class BookMessageOp 
{
	//在Hibernate中,所有的操作都是通过Session完成
	// 此Session不同于JSP的Session
	private Session session = null ;
	private String searchName = null;
	private String type =null;
	// 在构造方法之中实例化session对象
	public BookMessageOp(String researchcontent, String type)
	{
		// 找到Hibernate配置
		Configuration config = new Configuration().configure() ;
		// 从配置中取出SessionFactory
		SessionFactory factory = config.buildSessionFactory() ;
		// 从SessionFactory中取出一个Session
		this.session = factory.openSession() ;
		this.searchName=researchcontent;
		this.type = type;
	}
	// 模糊查询
	public List getObjectList() {
		// TODO Auto-generated method stub
		Query query = this.session.createQuery(this.getHqlString(this.type));
		query.setString(0, "%"+this.searchName+"%");
		List list = query.list();
		return list;
	}
	
	public String getHqlString(String type)
	{
		String hql=null;
		int searType = Integer.parseInt(type);
		switch(searType)
		{
			case 1:hql="from BookMessage where book_name like ?";break;
			case 2:hql="from BookMessage where author like ?";break;
			case 3:hql="from BookMessage where publish like ?";break;
			case 4:hql="from BookMessage where book_ISBN like ?";break;
		}
		return hql;
	}
}

⌨️ 快捷键说明

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