📄 notedaoiml.java
字号:
package com.zzx.struts.daoiml;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.zzx.struts.dao.NoteDao;
import com.zzx.struts.vo.Note;
public class NoteDaoIml extends HibernateDaoSupport implements NoteDao {
public void insert(Note note) {
// 这种方法也能实现插入,不是很简洁
// Session session = super.getSession();
// Transaction tx = session.beginTransaction();
// session.save(note);
// tx.commit();
// session.close();
// 这种方法不能正确插入,我的理解是调用了两次的session不是同一个对象
// super.getSession().save(note);
// super.getSession().commit();
//session.close();
this.getHibernateTemplate().save(note);
}
public void delete(int id) {
Session session = super.getSession();
String hql = "delete from Note where id=?";
Query q = session.createQuery(hql);
q.setInteger(0, id);
q.executeUpdate();
session.beginTransaction().commit();
//session.close();
}
public void update(Note note) {
//session.save(note);
// super.getSession().update(note);
// super.getSession().beginTransaction().commit();
//session.close();
this.getHibernateTemplate().update(note);
}
public List<Note> queryAll() {
List<Note> all = null;
String hql = "From Note as n";
all =super.getSession().createQuery(hql).list();
//session.close();
return all;
}
public List<Note> queryLike(String keyword) {
List<Note> all = new ArrayList<Note>();
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, "%" + keyword + "%");
q.setString(1, "%" + keyword + "%");
q.setString(2, "%" + keyword + "%");
all = q.list();
//session.beginTransaction().commit();
//session.close();
return all;
}
public Note queryById(int id) {
List<Note> all = new ArrayList<Note>();
Note note = new Note();
String hql = "FROM Note as n WHERE n.id=?";
Query q = super.getSession().createQuery(hql);
q.setInteger(0, id);
all = q.list();
if(all.size()>0){
note = all.get(0);
}
return note;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -