📄 newsdao.java
字号:
package com.dao;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.List;
import org.hibernate.Query;
import com.actionForm.NewsForm;
public class NewsDao {
private static SessionFactory sessionFactory = null;
private Session session = null;
Transaction tx = null;
static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public List selectTodayNews(String createTime) { //对今日新闻的查询
List list = null;
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hql = "from NewsForm where createTime='" + createTime +
"' order by createTime desc";
try {
Query query = session.createQuery(hql);
list = query.list();
} catch (Exception e) {
e.printStackTrace();
}
tx.commit();
session.close();
return list;
}
//===========================================================================
public void deleteNews(int id) {
Session session = sessionFactory.openSession();
try {
tx = session.beginTransaction();
NewsForm form = (NewsForm) session.load(NewsForm.class, id);
session.delete(form);
tx.commit();
} catch (Exception e) {
System.out.println("删除数据出错:" + e);
} finally {
session.close();
}
}
/*==========================================================================*/
public void updateNews(NewsForm form) {
Session session = sessionFactory.openSession();
try {
tx = session.beginTransaction();
session.load(NewsForm.class, form.getId());
session.update(form);
tx.commit();
} catch (Exception e) {
System.out.println("修改数据出错:" + e);
} finally {
session.close();
}
}
/*==========================================================================*/
public void insertNews(NewsForm form) { //对新闻的插入操作
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(form);
tx.commit();
} catch (Exception e) {
System.out.println("插入数据出错:" + e);
} finally {
session.close();
}
}
//==========================================================================
public NewsForm selectOneNews(int id) { //以自动编号为条件查询数据
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hql = "from NewsForm where id='" + id + "'";
NewsForm form = null;
try {
Query query = session.createQuery(hql);
form = (NewsForm) query.uniqueResult();
} catch (Exception e) {
System.out.println(e.getMessage());
}
tx.commit();
session.close();
return form;
}
/*==========================================================================*/
public List selectNews(String bigSort) { //对新闻的查询
List list = null;
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hql = "";
if (bigSort == null || bigSort.equals("")) {
hql = "from NewsForm order by createTime desc";
} else {
hql = "from NewsForm where bigSort='" + bigSort +
"' order by createTime desc";
}
try {
Query query = session.createQuery(hql);
list = query.list();
} catch (Exception e) {
e.printStackTrace();
}
tx.commit();
session.close();
return list;
}
//==============================================================================
public List selectKeyNews(String bigSort, String key) { //以新闻关键字查询
List list = null;
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hql = "";
if (bigSort == null || bigSort.equals("")) {
hql = "from NewsForm";
} else if (key == null || key.equals("")) {
hql = "from NewsForm where bigSort='" + bigSort +
"' order by id desc";
} else {
hql = "from NewsForm where bigSort='" + bigSort +
"' and (title like '%" + key + "%' or content like '%" + key +
"%')";
}
try {
Query query = session.createQuery(hql);
list = query.list();
} catch (Exception e) {
e.printStackTrace();
}
tx.commit();
session.close();
return list;
}
//===============================================================================
public List selectSmallNews(int sortId) {
List list = null;
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hql = "from NewsForm where sortId='" + sortId +
"' order by createTime desc";
try {
Query query = session.createQuery(hql);
list = query.list();
} catch (Exception e) {
e.printStackTrace();
}
tx.commit();
session.close();
return list;
}
//==================================================
public int selectSortNewsNumber(String bigSort) { //对新闻大类别条数
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hql = "from NewsForm where bigSort='" + bigSort +
"' order by createTime desc";
List list = null;
try {
Query query = session.createQuery(hql);
list = query.list();
} catch (Exception e) {
System.out.println(e.getMessage());
}
tx.commit();
session.close();
return list.size();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -