⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notedaoimpl.java

📁 不错的留言簿,大家可以看看哦.喜欢的可以下的.
💻 JAVA
字号:
package com.zjf.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.zjf.dao.NoteDAO;
import com.zjf.factory.DefaultSessionFactory;
import com.zjf.vo.Note;

public class NoteDAOImpl implements NoteDAO {
	private Session session = null;

	public NoteDAOImpl() {
		this.session = DefaultSessionFactory.getSession();
	}

	public void delete(int id) throws Exception {
		String hql = "DELETE FROM Note WHERE id=?";
		Query q = this.session.createQuery(hql);
		q.setInteger(0, id);
		q.executeUpdate();
		this.session.beginTransaction().commit();
		this.session.close();
	}

	public void insert(Note note) throws Exception {
		this.session.save(note);
		this.session.beginTransaction().commit();
		this.session.close();
	}

	public List queryAll() throws Exception {
		List all = null;
		String hql = "FROM Note AS n";
		all = this.session.createQuery(hql).list();
		this.session.close();
		return all;

	}

	public Note queryById(int id) throws Exception {
		Note note = null;
		String hql = "FROM Note AS n WHERE n.id=?";
		Query q = this.session.createQuery(hql);
		q.setInteger(0, id);
		List all = q.list();
		if (all.size() > 0) {
			note = (Note) all.get(0);
		}
		this.session.close();
		return note;
	}

	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 = this.session.createQuery(hql);
		q.setString(0, "%" + cond + "%");
		q.setString(1, "%" + cond + "%");
		q.setString(2, "%" + cond + "%");
		all = q.list();
		this.session.close();
		return all;

	}

	public void update(Note note) throws Exception {
		this.session.update(note);
		this.session.beginTransaction().commit();
		this.session.close();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -