📄 notedaoimpl.java
字号:
package org.lxh.struts.note.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.lxh.struts.note.dao.NoteDAO;
import org.lxh.struts.note.vo.Note;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class NoteDAOImpl extends HibernateDaoSupport implements NoteDAO {
// 增加操作
public void insert(Note note) throws Exception {
super.getSession().save(note);
super.getSession().beginTransaction().commit();
}
// 修改操作
public void update(Note note) throws Exception {
String hql = "UPDATE Note SET title=?,author=?,content=? WHERE id=?" ;
Query q = super.getSession().createQuery(hql) ;
q.setString(0, note.getTitle()) ;
q.setString(1, note.getAuthor()) ;
q.setString(2, note.getContent()) ;
q.setInteger(3, note.getId()) ;
q.executeUpdate() ;
super.getSession().beginTransaction().commit();
}
// 删除操作
public void delete(int id) throws Exception {
String hql = "DELETE FROM Note WHERE id=?";
Query q = super.getSession().createQuery(hql);
q.setInteger(0, id);
q.executeUpdate();
super.getSession().beginTransaction().commit();
}
// 按ID查询,主要为更新使用
public Note queryById(int id) throws Exception {
Note note = null;
String hql = "FROM Note AS n WHERE n.id=?";
Query q = super.getSession().createQuery(hql);
q.setInteger(0, id);
List all = q.list();
if (all.size() > 0) {
note = (Note) all.get(0);
}
return note;
}
// 查询全部
public List queryAll() throws Exception {
List all = null;
String hql = "FROM Note AS n";
all = super.getSession().createQuery(hql).list();
return all;
}
// 模糊查询
public List queryByLike(String cond) throws Exception {
List all = null;
String hql = "FROM Note AS n WHERE n.title LIKE ? OR n.author LIKE ? OR n.content LIKE ?";
Query q = super.getSession().createQuery(hql);
q.setString(0, "%" + cond + "%");
q.setString(1, "%" + cond + "%");
q.setString(2, "%" + cond + "%");
all = q.list();
return all;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -