?? 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 + -