?? bookloaddaoimpl.java
字號:
secondNum = (Long)firstList.get(0);
//System.out.println("secondNum:"+secondNum);
final 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++){
temp3 += " book.bookName not like :bookname"+i+ " and";
}
final String tempStr = temp3.substring(0, temp3.length()-3);
String tem3 = "from Book as book where (";
for(int i = 0;i<temList.size();i++){
tem3 += " book.bookName like :namebook"+i+ " or";
}
final String strHql3 = tem3.substring(0, tem3.length()-2) + ")and ("+tempStr+")";
/*處理分頁*/
final int thirdResult = (int) ((currPage-1)*10 - secondNum);
final int thirdEndResult = 10 - result1.size() - result2.size();
result3 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql3);
for(int j = 0;j<temList.size();j++)
query.setString("namebook"+j, "%"+temList.get(j)+"%");
for(int m = 0;m<keywordArr.size();m++)
query.setString("bookname"+m, "%"+keywordArr.get(m)+"%");
query.setFirstResult(thirdResult);
query.setMaxResults(thirdEndResult);
return query.list();
}
} );
// System.out.println("result3:"+result3.size());
// for(int i = 0;i<result3.size();i++){
// System.out.println("result3>>"+result3.get(i).getBookName());
// }
}
}
}else{
/*查詢出完全匹配的記錄*/
final String strHql1 = "from Book as book where book.bookName = :bookname";
/*處理分頁*/
final int firstResult = (currPage-1)*10;
final String keyword = keywordArr.get(0);
//System.out.println("keyword" + keyword);
result1 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql1);
query.setString("bookname", 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).getBookName());
// }
/*查詢向前匹配的記錄*/
if(result1.size()<10){
/*完全匹配的記錄總數*/
long firstNum = 0;
String firstHql = "select count(*) from Book as book where book.bookName = ?";
List firstList = this.getHibernateTemplate().find(firstHql, bookName);
if(null != firstList)
firstNum = (Long)firstList.get(0);
//System.out.println("first>>bookNum>>" + firstNum);
/*處理分頁*/
final int secondResult = (int) ((currPage-1)*10 - firstNum);
final int secondEndResult = 10 - result1.size();
/*向前匹配的hql*/
final String strHql2 = "from Book as book where book.bookName like :bookname and book.bookName != :namebook";
result2 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql2);
query.setString("bookname", keyword+"%");
query.setString("namebook", 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).getBookName());
// }
}
/*查詢出向后匹配的記錄*/
if((result1.size()+result2.size())<10){
/*完全匹配和向前匹配的記錄總數*/
long secondNum = 0;
String secondHql = "select count(*) from Book as book where book.bookName like ?";
List secondList = this.getHibernateTemplate().find(secondHql, bookName+"%");
if(null != secondList)
secondNum = (Long)secondList.get(0);
//System.out.println("second>>bookNum>>" + secondNum);
/*處理分頁*/
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.bookName like :bookname and book.bookName not like :namelikebook";
result3 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql3);
query.setString("bookname", "%" + keyword + "%");
//query.setString("namebook", keyword);
query.setString("namelikebook", 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).getBookName());
// }
}
/*如果完全匹配,向前匹配,向后匹配的記錄數不夠當前顯示,則進行分詞后的匹配*/
if((result1.size()+result2.size()+result3.size())<10){
final ArrayList<String> temList = new ArrayList<String>();
/*完全匹配,向前匹配和向后匹配的記錄總數*/
long thirdNum = 0;
String thirdHql = "select count(*) from Book as book where book.bookName like ?";
List thirdList = this.getHibernateTemplate().find(thirdHql, "%"+bookName+"%");
if(null != thirdList)
thirdNum = (Long)thirdList.get(0);
//System.out.println("third>>bookNum>>" + thirdNum);
/*如果長度大于2,就進行分詞處理*/
if(bookName.length()>2){
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");
}
/*處理查詢hql語句*/
String temStr = "from Book as book where (";
for(int i = 0;i<temList.size();i++){
temStr += " book.bookName like :bookname"+i+ " or";
}
final String strHql4 = temStr.substring(0, temStr.length()-2) + ") and book.bookName not like :namelikebook";
//System.out.println("strHql4>>" + strHql4);
/*處理分頁*/
final int forthResult = (int) ((currPage-1)*10 - thirdNum);
final int forthEndResult = 10 - result1.size() - result2.size() - result3.size();
result4 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
public List doInHibernate(Session session){
Query query = session.createQuery( strHql4);
for(int j = 0;j<temList.size();j++)
query.setString("bookname"+j, "%"+temList.get(j)+"%");
query.setString("namelikebook", "%"+keyword+"%");
//Query query = session.createCriteria(strHql, "%"+keyword+"%");
query.setFirstResult(forthResult);
query.setMaxResults(forthEndResult);
return query.list();
}
} );
// for(int i = 0;i<result4.size();i++){
// System.out.println("result4>>"+result4.get(i).getBookName());
// }
}
}
}
if(result1.size()>0)result.addAll(result1);
if(result2.size()>0)result.addAll(result2);
if(result3.size()>0)result.addAll(result3);
if(result4.size()>0)result.addAll(result4);
return result;
}
/**
* Function: loadBookByAuthor
* Description: 從數據庫中獲得按作者方式檢索的所有記錄
* Calls: no
* Called By: this.loadBook()
* @param bookAuthor as String,currPage as int
* @return ArrayList<Book>
* @throws no
*/
private ArrayList<Book> loadBookByAuthor(String bookAuthor,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(bookAuthor, " ");
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.bookAuthor like :bookauthor"+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("bookauthor"+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).getBookAuthor());
// }
/*多個關鍵字同時匹配不夠十條時,進行不同時匹配查詢*/
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.bookAuthor 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.bookAuthor like :authorbook"+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("authorbook"+j, "%"+keywordArr.get(j)+"%");
for(int k = 0;k<keywordArr.size();k++)
query.setString("bookauthor"+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).getBookAuthor());
// }
}
}else{
/*查詢出完全匹配的記錄*/
final String strHql1 = "from Book as book where book.bookAuthor = :bookauthor";
/*處理分頁*/
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("bookauthor", 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).getBookAuthor());
// }
/*查詢向前匹配的記錄*/
if(result1.size()<10){
/*完全匹配的記錄總數*/
long firstNum = 0;
String firstHql = "select count(*) from Book as book where book.bookAuthor = ?";
List firstList = this.getHibernateTemplate().find(firstHql, bookAuthor);
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.bookAuthor like :bookauthor and book.bookAuthor != :authorbook";
result2 = (ArrayList<Book>) this .getHibernateTemplate().executeFind( new HibernateCallback() {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -