In addition, give some suggestions to the landlord to lay a good foundation. This set of codes is messy and poorly written. Not suitable for study. If you want to learn, find good code, otherwise it will be easily misled.
/**
* A class in the DAO layer uses hibernate to control the persistence layer.
* Inherited from AbstractDAO, and realized the related methods of abstract classes.
* mainly includes obtaining book list, obtaining book records, adding book information, adding and updating book information,
* get the book information, and judge whether the book information can be deleted.
*/
Public class BookDAO extends AbstractDAO {
//Parameter-free construction method
Public library () {
}
/* * governance actually added a main method. I'm sorry I'm stupid, but I can't think of any other explanation except adding chaos.
* It is also possible that it was used for testing in the testing stage and later forgot to delete it.
*/
Public static void main(String[] args) {
// TODO automatically generated method stub
}
/**
* according to the keyword to obtain book information using fuzzy query to get a list of book information containing keywords.
* If the bookName attribute of the entered book object is control or an empty string, all book information will be queried by default.
The persistent object of *@pamar book book information should correspond to a table bookinfo in the database.
*@pamar pager should be an object that encapsulates paging logic.
The database returned by *@return List contains a list of book information, including keywords (titles) in books.
* By default, the information of all books in the database is returned.
* The return value here is designed as a list.
**/
The public list getBookList(Bookinfo book, Pager pager) throws a DataAccessException{
//hql statement returns all book information in the database by default.
string hql = " from com . jframe . biz . book . book info book "+
"where1=1";
/* If the book name exists and is not an empty string.
* Reconstruct hql statement, add restrictions, and return the fuzzy query result set of book names.
*/
if(book.getBookName()! = null & amp& amp! "".equals(book . get bookname()){
Hql += "and book.booknamelike"% "+book.getbookname ()+"%";
}
//If the book author is not empty and the string is not empty, add a restriction condition and return the fuzzy query result set of the specified author.
if(book.getAuthor()! = null & amp& amp! "".equals(book . get author()){
Hql += "and book.authorlike"% "+book.getauthor ()+"%";
}
//Create a query object to execute the hql statement.
Query query = this.getSession()。 create query(hql);
//The following two records are set to find the first record from the first record in the database for paging.
//Set the size of the result set to the number of records displayed per page set by paging logic.
query . setmaxresults(pager . get pagesize());
//Check the start record from the logic in the paging.
query . set first result((pager . getcurrentpage()- 1)* pager . get pagesize());
//Return the query result.
Returns query.list ();
}
/**
* query the number of records of books that meet the specified conditions.
*@pamar book a book information object
* @ returns the int book record.
*/
Public int getbookcount (bookinfobook) throws DataAccessException{
//Set the default hql to query the records of all books.
string hql = " select count(bookName)from com . jframe . biz . book . book info book "+
"where1=1";
//If the book name is not empty and the string is not empty, add a restricted fuzzy query of the book name.
if(book.getBookName()! = null & amp& amp! "".equals(book . get bookname()){
Hql += "and book.booknamelike"% "+book.getbookname ()+"%";
}
//If the book author is not empty and the string is not empty, add a restriction-fuzzy query of the book author.
if(book.getAuthor()! = null & amp& amp! "".equals(book . get author()){
Hql += "and book.authorlike"% "+book.getauthor ()+"%";
}
Query query = this.getSession()。 create query(hql);
//Returns the result set of the query to the list.
list list = query . list();
//If the result set object is not empty and there are results.
If (list! = null & amp& amplist . size()& gt; 0)
//Converts the first element in the result set to an integer, and then returns.
Return ((integer) query.list (). Iterator (). Next ()). int value();
other
//No record returns 0.
Returns 0;
}
/**
* Add book information
*@parma book book information object
*@return It's hard to understand why this method still has a return value, even if it needs a return value, it's Boolean.
*/
Public book info add book info (book info book) throws a DataAccessException{
//Create a hibernate template object.
hibernate template template = this . gethibernate template();
//Call the template saving method to save the book information.
Template.save (book);
//Clean up the cache or dirty data, and check whether the persistent objects in memory have been stored in the database.
template . flush();
template . evict(book);
//The return value will not be interpreted in the future. The method header has an explanation of parameters and return values.
Returns getbookinfo (book);
}
/**
* Save or update the method. Note that if this method looks like hibernate 3.0 or later, it was deleted in the old version of Hibernate.
*@parma book book information object
*@return Why does this and the last method still have a return value, even if it is Boolean?
*/
Public book info save book info (book info book) throws a DataAccessException{
//The same method will not be explained later.
hibernate template template = this . gethibernate template();
//If it is not in the database, insert the update.
template . saveourupdate(book);
Return books;
}
/**
* Delete the specified book.
* @ Palma Books Information
* @ returns void
*/
Public void delete bookinfo (bookinfo book) throws a DataAccessException{
hibernate template template = this . gethibernate template();
//Call the deletion method of the template.
Template.delete (book);
}
/**
* according to the ISBN query book information
*@param book book object
* @ Book Return Information
*/
Public book info getbook info (book info book) throws a DataAccessException{
hibernate template template = this . gethibernate template();
//sql statement to query the book information of the specified ISBN.
string hql = " from com . jframe . biz . book . book info book "+
"where 1= 1 and book.bookNo ='"+book.getbookNo ()+"'";
//The query class of the template that calls the query
list list = template . find(hql);
//Convert the first value of the query result set into an object return.
If (list! = null & amp& amplist . size()& gt; 0)
return (Bookinfo) list.iterator()。 next();
other
Returns null
}
/**
*r Returns true if the book number recorded in the log information can be deleted and exists in the database.
*/
public boolean is candelete(book info book){
Boolean flag = false;
hibernate template template = this . gethibernate template();
string hql = " from com . jframe . biz . log . log info log "+
"where 1= 1 and log.bookinfo.bookno ='"+book.getbookno ()+"'";
list list = template . find(hql);
If (list! = null & amp& amplist . size()& gt; 0)
flag = false
other
flag = true
Return flag;
}
}