📄 newlivelyhibernatedao.java~406~
字号:
package org.lenovoAC.hibernateDao;
import org.lenovoAC.pojo.NewLively;
import java.util.List;
import org.lenovoAC.dao.NewLivelyDao;
import org.lenovoAC.tools.Tools;
import net.sf.hibernate.Query;
import org.lenovoAC.Page.Page;
import org.springframework.orm.hibernate.HibernateCallback;
import net.sf.hibernate.Session;
import net.sf.hibernate.HibernateException;
import java.sql.SQLException;
import oracle.sql.CLOB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.Query;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
* 最新动态
最新动态表: New_Lively
*/
public class NewLivelyHibernateDao
extends BaseDao
{
/**
* 添加最新动态
* @param nl NewLively
*/
public void insertNewLively(NewLively nl) throws Exception
{
this.getHibernateTemplate().save(nl);
Connection con = null;
ResultSet rs = null;
Session session = this.getSession();
con = session.connection();
con.setAutoCommit(false);
String sql = "select * from new_lively where Product_Id='" +
nl.getProductId() + "' for update";
PreparedStatement ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next())
{
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("PRODUCT_CONTEXT");
java.io.Writer out = clob.getCharacterOutputStream();
out.write(nl.getProductContext());
out.close();
con.commit();
con.close();
}
}
/**
* 修改最新动态
* @param nl NewLively
*/
public void updateNewLively(NewLively nl) throws Exception
{
Connection con = null;
ResultSet rs = null;
Session session = this.getSession();
con = session.connection();
con.setAutoCommit(false);
String sqlTwo = "UPDATE NEW_LIVELY SET product_title=?,time=sysdate,state=?,type_state=? WHERE product_id=?";
PreparedStatement ps2 = con.prepareStatement(sqlTwo);
ps2.setString(1, nl.getProductTitle());
ps2.setString(2, nl.getState());
ps2.setString(3, nl.getTypeState());
ps2.setString(4, nl.getProductId());
ps2.executeUpdate();
String upSql="update new_lively set PRODUCT_CONTEXT=empty_clob() where product_id=?";
PreparedStatement ps3 = con.prepareStatement(upSql);
String sql = "select * from new_lively where Product_Id='" +
nl.getProductId() + "' for update";
PreparedStatement ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next())
{
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("PRODUCT_CONTEXT");
java.io.Writer out = clob.getCharacterOutputStream();
out.write(nl.getProductContext());
out.close();
con.commit();
con.close();
}
}
/**
* 删除最新动态
* @param nl NewLively
*/
public void delNewLively(NewLively nl)
{
this.getHibernateTemplate().delete(nl);
this.getHibernateTemplate().flush();
}
/**
* 通过ID删除指定对象
* @param NewLivelyId String
*/
public void delNewLivelyTwo(String NewLivelyId)
{
NewLively nl = (NewLively)this.getHibernateTemplate().load(NewLively.class,
NewLivelyId);
this.delNewLively(nl);
}
/**
* 得到指定ID的动态新闻对象
* @param NewLivelyId String
* @return NewLively
*/
public NewLively getNewLively(String NewLivelyId)
{
return (NewLively)this.getHibernateTemplate().load(NewLively.class,
NewLivelyId);
}
/**
* 取得表中所有对象
* @return List
*/
public List getAllNewLively()
{
return this.getHibernateTemplate().loadAll(NewLively.class);
}
/**
* 取得表中所有对象 时间降序
* @return List
*/
public List getAllNewLivelyTwo()
{
List list = null;
String hql = "from NewLively as n order by n.time desc";
try
{
list = this.getHibernateTemplate().find(hql);
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* 得到最新的一条记录
* @param typeState String
* @return NewLively
*/
public List getNewNewLively(String typeState)
{
String hql =
"from NewLively as nl where nl.typeState=? and nl.state=? order by nl.time desc";
List list = this.getHibernateTemplate().find(hql, new String[]
{typeState, "01"});
if (list.isEmpty() || list.size() == 0)
{
return null;
}
else
{
return list;
}
}
public List getLimitNewLively(String typeState)
{
List list = null;
String hql =
"from NewLively as nl where nl.typeState=? and nl.state=? order by nl.time desc";
try
{
Query query = this.getHibernateTemplate().createQuery(getSession(), hql);
query.setString(0, typeState);
query.setString(1, "01");
query.setFirstResult(0);
query.setMaxResults(12);
list = query.list();
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* 分页
* @param sql String
* @return ResultSet
*/
public List getAllNewLivelyPage(Page page)
{
List list = null;
String hql = "from NewLively as n order by n.time desc";
try
{
Query query = this.getHibernateTemplate().createQuery(this.getSession(),
hql);
query.setFirstResult(page.getFirstResult());
query.setMaxResults(page.getPageSize());
list = query.list();
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* 得取记录总数
* @param countHql String
* @return int
* @throws Exception
*/
public int getRecordCount(final String countHql) throws Exception
{
Integer in = null;
try
{
Query query = this.getHibernateTemplate().createQuery(this.getSession(),
countHql);
in = (Integer) query.list().get(0);
}
catch (Exception e)
{
throw new Exception("传入countHql格式不对!" + e);
}
return in.intValue();
}
/**
* 分页
* @param page Page
* @return List
*/
public List getHelperList(final Page page)
{
return getHibernateTemplate().executeFind(new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException,
SQLException
{
Query query = session.createQuery(page.getHql());
query.setFirstResult(page.getFirstResult());
query.setMaxResults(page.getPageSize());
List list = query.list();
return list;
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -