日期:2014-05-16  浏览次数:20627 次

Spring jdbc 操作实例
package com.szxhdz.dao;

import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.szxhdz.beans.MsgMessage;
import com.szxhdz.util.Factory;

public class MessageDao {
	private static MessageDao instance =null;
	public static synchronized MessageDao getInstance(){
		if(instance==null)
		instance = (MessageDao)Factory.getBean("messageDao");
		return instance;
	}
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public int getCount() {
		int count = getJdbcTemplate().queryForInt(
				"select count(*) from msg_message");
		return count;
	}
	public int getCount(String sql) {
		int count = getJdbcTemplate().queryForInt(sql);
		return count;
	}

	public int getNextval() {
		int nextval = getJdbcTemplate().queryForInt(
				"select get_id.nextval from dual");
		return nextval;
	}

	private class MessageRowMapper implements RowMapper {

		public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
			MsgMessage msgMessage = new MsgMessage();
			 try {
				Blob blob = rs.getBlob("m_content");
				int i = (int) blob.length();
				byte[] bytes = blob.getBytes(1, i);
				msgMessage.setMcontent(new String(bytes, "GBK"));
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			msgMessage.setMdatetime(rs.getString("m_datetime"));
			msgMessage.setMdeptid(new Integer(rs.getInt("M_DEPTID")));
			msgMessage.setMfromip(rs.getString("M_FROMIP"));
			msgMessage.setMhidetitle(new Integer(rs.getInt("M_HIDETITLE")));
			msgMessage.setMid(new Integer(rs.getInt("M_ID")));
			msgMessage.setMkeywords(rs.getString("M_KEYWORDS"));
			msgMessage.setMkindid(new Integer(rs.getInt("M_KINDID")));
			msgMessage.setMneedsignin(new Integer(rs.getInt("M_NEEDSIGNIN")));
			msgMessage.setMoriginalfilename(rs.getString("M_ORIGINALFILENAME"));
			msgMessage.setMreadtimes(new Integer(rs.getInt("M_READTIMES")));
			msgMessage.setMreviewtimes(new Integer(rs.getInt("M_REVIEWTIMES")));
			msgMessage.setMsavefilename(rs.getString("M_SAVEFILENAME"));
			msgMessage.setMsavepathfilename(rs.getString("M_SAVEPATHFILENAME"));
			msgMessage.setMsource(rs.getString("M_SOURCE"));
			msgMessage.setMstate(new Integer(rs.getInt("M_STATE")));
			msgMessage.setMsummary(rs.getString("M_SUMMARY"));
			msgMessage.setMtimestampposition(new Integer(rs.getInt("M_TIMESTAMPPOSITION")));
			msgMessage.setMtitle(rs.getString("M_TITLE"));
			msgMessage.setMtitleimgfilename(rs.getString("M_TITLEIMGFILENAME"));
			msgMessage.setMupdatetime(rs.getString("M_UPDATETIME"));
			msgMessage.setMuserid(new Integer(rs.getInt("M_USERID")));

			return msgMessage;
		}
	}

	public MsgMessage getMsgMessage(int id) {
		List list = getJdbcTemplate().query(
				"select * from msg_message where m_id=?",
				new Object[] { new Integer(id) }, new MessageRowMapper());
		MsgMessage msgMessage = (MsgMessage) list.get(0);
		return msgMessage;
	}

	public List getMsgMessages() {
		List list = getJdbcTemplate().query("select * from msg_Message order by m_id desc ",
				new MessageRowMapper());
		return list;
	}
	/**
	 *
	 * @param kinds   信息类别
	 * @param curPage 当前页
	 * @param pageSize 每页记录数
	 * @return
	 */
	public List getMsgMessages(String sql,int startRow,int endRow) {
   //     System.out.println(sql);
		StringBuffer pagingSelect = new StringBuffer( sql.length()+100 );
		pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
		pagingSelect.append(sql);
		pagingSelect.append(" ) row_ where rownum <= "+endRow+") where rownum_ >= "+startRow+"");
		List list = getJdbcTemplate().query(pagingSelect.toString(), new MessageRowMapper());
		return list;
	}
	/**
	 * 审核
	 * @param m_id
	 */

	public vo