?? bookloaddaoimpl.java
字號:
public List doInHibernate(Session session){
Query query = session.createQuery( strHql2);
query.setString("bookauthor", keyword+"%");
query.setString("authorbook", keyword);
query.setFirstResult(secondResult);
query.setMaxResults(secondEndResult);
return query.list();
}
});
// for(int i = 0;i<result2.size();i++){
// System.out.println("result2>>"+result2.get(i).getBookAuthor());
// }
}
/*查詢出向后匹配的記錄*/
if((result1.size()+result2.size())<10){
/*完全匹配和向前匹配的記錄總數*/
long secondNum = 0;
String secondHql = "select count(*) from Book as book where book.bookAuthor like ?";
List secondList = this.getHibernateTemplate().find(secondHql, bookAuthor+"%");
if(null != secondList)
secondNum = (Long)secondList.get(0);
/*處理分頁*/
final int thirdResult = (int) ((currPage-1)*10 - secondNum);
final int thirdEndResult = 10 - result1.size() - result2.size();
/*查詢hql*/
//final String strHql3 = "from Book as book where book.bookName like :bookname and book.bookName != :namebook and book.bookName not like :namelikebook";
final String strHql3 = "from Book as book where book.bookAuthor like :bookauthor and book.bookAuthor not like :authorlikebook";
result3 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql3);
query.setString("bookauthor", "%"+keyword+"%");
//query.setString("namebook", keyword);
query.setString("authorlikebook", keyword+"%");
query.setFirstResult(thirdResult);
query.setMaxResults(thirdEndResult);
return query.list();
}
} );
// for(int i = 0;i<result3.size();i++){
// System.out.println("result3>>"+result3.get(i).getBookAuthor());
// }
}
}
if(result1.size()>0)result.addAll(result1);
if(result2.size()>0)result.addAll(result2);
if(result3.size()>0)result.addAll(result3);
return result;
}
/**
* Function: loadBookByPublisher
* Description: 從數據庫中獲得按出版社方式檢索的所有記錄
* Calls: no
* Called By: this.loadBook()
* @param publisher as String,currPage as int
* @return ArrayList<Book>
* @throws no
*/
private ArrayList<Book> loadBookByPublisher(String publisher,int currPage){
ArrayList<Book> result = new ArrayList<Book>();
ArrayList<Book> result1 = new ArrayList<Book>();
ArrayList<Book> result2 = new ArrayList<Book>();
ArrayList<Book> result3 = new ArrayList<Book>();
final List<String> keywordArr = new ArrayList<String>();
/*按" "和"+"進行分詞處理,分成單獨的一條條記錄*/
StringTokenizer st = new StringTokenizer(publisher, " ");
while (st.hasMoreElements()) {
keywordArr.add(st.nextToken());
}
/*如果輸入的關鍵字不是一個時的查詢方式*/
if(keywordArr.size()>1){
/*多個關鍵字同時存在時*/
String tem1 = "from Book as book where";
for(int i = 0;i<keywordArr.size();i++){
tem1 += " book.bookPublisher like :bookpublisher"+i+ " and";
}
final String strHql1 = tem1.substring(0, tem1.length()-3);
/*處理分頁*/
final int firstResult = (currPage-1)*10;
final int firstEndResult = 10;
result1 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql1);
for(int j = 0;j<keywordArr.size();j++)
query.setString("bookpublisher"+j, "%"+keywordArr.get(j)+"%");
query.setFirstResult(firstResult);
query.setMaxResults(firstEndResult);
return query.list();
}
} );
// System.out.println("result1:"+result1.size());
// for(int i = 0;i<result1.size();i++){
// System.out.println("result1>>"+result1.get(i).getBookPublisher());
// }
/*多個關鍵字同時匹配不夠十條時,進行不同時匹配查詢*/
if(result1.size()<10){
/*完全匹配的記錄總數*/
long firstNum = 0;
String firstHql = "select count(*) from Book as book where";
for(int i = 0;i<keywordArr.size();i++){
firstHql += " book.bookPublisher like ? and";
}
firstHql =firstHql.substring(0, firstHql.length()-3);
String []temKey = new String[keywordArr.size()];
for(int j = 0;j<keywordArr.size();j++)temKey[j] = "%" + keywordArr.get(j) + "%";
List firstList = this.getHibernateTemplate().find(firstHql, temKey);
if(null != firstList)
firstNum = (Long)firstList.get(0);
//System.out.println("firstNum:"+firstNum);
String tem2 = "from Book as tushu where (";
for(int i = 0;i<keywordArr.size();i++){
tem2 += " tushu.bookPublisher like :publisherbook"+i+ " or";
}
final String strHq2 = tem2.substring(0, tem2.length()-2) + ")and not exists ("+strHql1+"and tushu.id=book.id)";
/*處理分頁*/
final int secondResult = (int) ((currPage-1)*10 - firstNum);
final int secondEndResult = 10-result1.size();
result2 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHq2);
for(int j = 0;j<keywordArr.size();j++)
query.setString("publisherbook"+j, "%"+keywordArr.get(j)+"%");
for(int k = 0;k<keywordArr.size();k++)
query.setString("bookpublisher"+k, "%"+keywordArr.get(k)+"%");
query.setFirstResult(secondResult);
query.setMaxResults(secondEndResult);
return query.list();
}
});
// System.out.println("result2:"+result2.size());
// for(int i = 0;i<result2.size();i++){
// System.out.println("result2>"+result2.get(i).getBookPublisher());
// }
}
}else{
/*查詢出完全匹配的記錄*/
final String strHql1 = "from Book as book where book.bookPublisher = :bookpublisher";
/*處理分頁*/
final int firstResult = (currPage-1)*10;
final String keyword = keywordArr.get(0);
result1 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql1);
query.setString("bookpublisher", keyword);
//Query query = session.createCriteria(strHql, "%"+keyword+"%");
query.setFirstResult(firstResult);
query.setMaxResults(10);
return query.list();
}
} );
// for(int i = 0;i<result1.size();i++){
// System.out.println("result1>>"+result1.get(i).getBookPublisher());
// }
/*查詢向前匹配的記錄*/
if(result1.size()<10){
/*完全匹配的記錄總數*/
long firstNum = 0;
String firstHql = "select count(*) from Book as book where book.bookPublisher = ?";
List firstList = this.getHibernateTemplate().find(firstHql, publisher);
if(null != firstList)
firstNum = (Long)firstList.get(0);
/*處理分頁*/
final int secondResult = (int) ((currPage-1)*10 - firstNum);
final int secondEndResult = 10 - result1.size();
/*向前匹配的hql*/
final String strHql2 = "from Book as book where book.bookPublisher like :bookpublisher and book.bookPublisher != :publisherbook";
result2 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql2);
query.setString("bookpublisher", keyword+"%");
query.setString("publisherbook", keyword);
query.setFirstResult(secondResult);
query.setMaxResults(secondEndResult);
return query.list();
}
});
// for(int i = 0;i<result2.size();i++){
// System.out.println("result2>>"+result2.get(i).getBookPublisher());
// }
}
/*查詢出向后匹配的記錄*/
if((result1.size()+result2.size())<10){
/*完全匹配和向前匹配的記錄總數*/
long secondNum = 0;
String secondHql = "select count(*) from Book as book where book.bookPublisher like ?";
List secondList = this.getHibernateTemplate().find(secondHql, publisher+"%");
if(null != secondList)
secondNum = (Long)secondList.get(0);
/*處理分頁*/
final int thirdResult = (int) ((currPage-1)*10 - secondNum);
final int thirdEndResult = 10 - result1.size() - result2.size();
/*查詢hql*/
//final String strHql3 = "from Book as book where book.bookName like :bookname and book.bookName != :namebook and book.bookName not like :namelikebook";
final String strHql3 = "from Book as book where book.bookPublisher like :bookpublisher and book.bookPublisher not like :publisherlikebook";
result3 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql3);
query.setString("bookpublisher", "%"+keyword+"%");
//query.setString("namebook", keyword);
query.setString("publisherlikebook", keyword+"%");
query.setFirstResult(thirdResult);
query.setMaxResults(thirdEndResult);
return query.list();
}
} );
// for(int i = 0;i<result3.size();i++){
// System.out.println("result3>>"+result3.get(i).getBookPublisher());
// }
}
}
if(result1.size()>0)result.addAll(result1);
if(result2.size()>0)result.addAll(result2);
if(result3.size()>0)result.addAll(result3);
return result;
}
/**
* Function: loadBookNumByName
* Description: 從數據庫中獲得按圖書名稱匹配的記錄總數
* Calls: no
* Called By: this.loadBookNum()
* @param bookName as String
* @return long
* @throws no
*/
private long loadBookNumByName(String bookName){
long num = 0;
long firstNum = 0;
long secondNum = 0;
List<String> keywordArr = new ArrayList<String>();
/*按" "進行分詞處理,分成單獨的一條條記錄*/
StringTokenizer st = new StringTokenizer(bookName, " ");
while (st.hasMoreElements()) {
keywordArr.add(st.nextToken());
}
if(keywordArr.size()==1){
/*完全匹配,向前匹配,向后匹配的記錄總數*/
String temHql = "select count(*) from Book as book where book.bookName like ?";
List firstList = this.getHibernateTemplate().find(temHql, "%"+bookName+"%");
if(firstList.size()>0)
firstNum = (Long)firstList.get(0);
/*中文分詞之后的記錄總數*/
if(bookName.length()>2){
ArrayList<String> temList = new ArrayList<String>();
/*進行分詞,并把分詞之后的每個詞放到一個鏈表中*/
try {
TokenStream ts = this.writerAnalyzer.tokenStream("", new StringReader(bookName));
Token token ;
while ((token = ts.next()) != null) {
temList.add(token.toString().substring(1, token.toString().indexOf(",")).trim());
//System.out.println(token.toString().substring(1, token.toString().indexOf(",")));
}
}catch (Exception e) {
logger.error(e + "==========" + "庖丁解牛分詞出錯" + "==========");
//e.printStackTrace();
//System.out.println("poading error");
}
/*如果分出的詞個數大于零,則對每個詞進行模糊匹配*/
if(temList.size()>0){
/*處理查詢hql語句*/
String temStr = "select count(*) from Book as book where (";
for(int i = 0;i<temList.size();i++){
temStr += " book.bookName like ? or";
}
String strHql = temStr.substring(0, temStr.length()-2) + ") and book.bookName not like ?";
/*查詢參數*/
String args [] = new String[temList.size()+1];
int i = 0;
for( ;i<temList.size();i++) args[i] = "%"+temList.get(i)+"%";
args[i] = "%"+bookName+"%";
List secondList = this.getHibernateTemplate().find(strHql, args);
if(secondList.size()>0)
secondNum = (Long)secondList.get(0);
}
}
num = firstNum + secondNum;
//System.out.println("firstNum:"+firstNum+"secondNum:"+secondNum);
}else{
/*多個關鍵字同時存在和不同時存在時的記錄數*/
String firstHql = "select count(*) from Book as book where";
for(int i = 0;i<keywordArr.size();i++){
firstHql += " book.bookName like ? or";
}
firstHql =firstHql.substring(0, firstHql.length()-2);
/*查詢參數*/
String []temKey = new String[keywordArr.size()];
for(int j = 0;j<keywordArr.size();j++)temKey[j] = "%" + keywordArr.get(j) + "%";
List firstList = this.getHibernateTemplate().find(firstHql, temKey);
if(null != firstList)
firstNum = (Long)firstList.get(0);
/*對每個關鍵字進行分詞處理*/
ArrayList<String> temList = new ArrayList<String>();
for(int m = 0;m<keywordArr.size();m++){
/*如果長度大于2,就進行分詞處理*/
if(keywordArr.get(m).length()>2){
try {
TokenStream ts = this.writerAnalyzer.tokenStream("", new StringReader(keywordArr.get(m)));
Token token ;
while ((token = ts.next()) != null) {
temList.add(token.toString().substring(1, token.toString().indexOf(",")).trim());
//System.out.println(token.toString().substring(1, token.toString().indexOf(",")));
}
}catch (Exception e) {
logger.error(e);
//e.printStackTrace();
//System.out.println("poading error");
}
}
}
/*如果分詞之后,個數大于零,則查詢對于每個分詞的模糊匹配記錄數*/
if(temList.size()>0){
/*處理查詢hql語句*/
String temp3 = "";
for(int i = 0;i<keywordArr.size();i++){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -