readerdaoimpl.java
来自「实现图书的借阅和管理信息化」· Java 代码 · 共 154 行
JAVA
154 行
package com.moonman.libraryManager.model.daoImpl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.moonman.libraryManager.model.dao.ReaderDAO;
import com.moonman.libraryManager.model.vo.Reader;
public class ReaderDAOImpl extends HibernateDaoSupport implements ReaderDAO {
public Reader getReaderByZjh(String zjh) {
Session session = this.getSession();
Query query = session.createQuery("from Reader where zjh=?");
query.setString(0, zjh);
return (Reader) query.uniqueResult();
}
public List<Reader> getReaderByContition(String type, String content) {
Session session = this.getSession();
try {
String hql = "from Books where title like ?";
if (type.equals("读者姓名")) {
hql = "from Reader where dzxm like ?";
content = "%" + content + "%";
} else if (type.equals("读者编号")) {
hql = "from Reader where zjh=?";
}
Query query = session.createQuery(hql).setString(0, content);
List<Reader> lst = query.list();
return lst;
} catch (HibernateException e) {
e.printStackTrace();
}
return null;
}
public List<Reader> getReaderByPageIndexAndContition(String type,
String content, int pageSize, int startRow) {
Session session = this.getSession();
try {
String hql = "from Books where title like ?";
if (type.equals("读者姓名")) {
hql = "from Reader where dzxm like ?";
content = "%" + content + "%";
} else if (type.equals("读者编号")) {
hql = "from Reader where zjh=?";
}
Query query = session.createQuery(hql).setString(0, content);
query.setMaxResults(pageSize);
query.setFirstResult(startRow);
List<Reader> lst = query.list();
return lst;
} catch (HibernateException e) {
e.printStackTrace();
}
return null;
}
/**
* 添加一个读者信息
*
* @param reader
* @return
*/
public boolean addReader(Reader reader) {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
session.save(reader);
tx.commit();
return true;
} catch (HibernateException e) {
// TODO Auto-generated catch block
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return false;
}
/**
* 根据读者编号删除读者信息
*
* @param readerId
* @return
*/
public boolean deleteReader(String readerId) {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
Query query = session.createQuery(
"delete from Reader where readerId=?").setInteger(0,
new Integer(readerId));
int flag = query.executeUpdate();
tx.commit();
return flag > 0;
} catch (Exception e) {
// TODO Auto-generated catch block
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return false;
}
/**
* 修改读者信息
*
* @param reader
* @return
*/
public boolean modifyReader(Reader reader) {
Session session = this.getSession();
Transaction tx = session.beginTransaction();
try {
session.update(reader);
tx.commit();
return true;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
session.close();
}
return false;
}
/**
* 根据读者名字进行模糊查询
*
* @return 查询结果
*/
public List<Reader> getReaderLikeName(String name) {
Session session = this.getSession();
Query query = session.createQuery("from Reader where dzxm like ?")
.setString(0, "%" + name + "%");
return query.list();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?