Spring JdbcTemplate 查询方法中的RowMapper实现汇总_再回首_百度空间

来源:互联网 发布:合工大网络教育 编辑:程序博客网 时间:2024/06/06 20:52
  1. 实现一、在内部建立内联类实现<SPANclass=hilite1>RowMapper</SPAN>接口   
  2. packagehysteria.contact.dao.impl;   
  3.   
  4. importjava.sql.ResultSet;   
  5. importjava.sql.SQLException;   
  6. importjava.sql.Types;   
  7. importjava.util.List;   
  8.   
  9. importorg.springframework.jdbc.core.JdbcTemplate;   
  10. importorg.springframework.jdbc.core.<SPANclass=hilite1>RowMapper</SPAN>;   
  11.   
  12. importhysteria.contact.dao.ItemDAO;   
  13. importhysteria.contact.domain.Item;   
  14.   
  15. publicclassItemDAOImplimplementsItemDAO {   
  16. privateJdbcTemplate jdbcTemplate;   
  17.   
  18. publicvoidsetJdbcTemplate(JdbcTemplate jdbcTemplate) {   
  19.   this.jdbcTemplate = jdbcTemplate;   
  20. }   
  21.   
  22. publicItem insert(Item item) {   
  23.    String sql ="INSERT INTO items(user_id,name,phone,email) VALUES(?,?,?,?)";   
  24.    Object[] params =newObject[]{item.getUserId(),item.getName(),item.getPhone(),item.getEmail()};   
  25.   int[] types =newint[]{Types.INTEGER,Types.VARCHAR,Types.CHAR,Types.VARCHAR};   
  26.    jdbcTemplate.update(sql,params,types);   
  27.   returnitem;   
  28. }   
  29.   
  30. publicItem update(Item item) {   
  31.    String sql ="UPDATE items SET name = ?, phone = ?, email = ? WHERE id = ?";   
  32.    Object[] params =newObject[] {item.getName(),item.getPhone(),item.getEmail(),item.getId()};   
  33.   int[] types =newint[] {Types.VARCHAR,Types.CHAR,Types.VARCHAR,Types.VARCHAR,Types.INTEGER};   
  34.    jdbcTemplate.update(sql,params,types);   
  35.   
  36.   returnitem;   
  37. }   
  38.   
  39. publicvoiddelete(Item item) {   
  40.    String sql ="DELETE FROM items WHERE id = ?";   
  41.    Object[] params =newObject[] {item.getId()};   
  42.   int[] types =newint[]{Types.INTEGER};   
  43.    jdbcTemplate.update(sql,params,types);   
  44. }   
  45.   
  46. publicItem findById(intid) {   
  47.    String sql ="SELECT * FROM items WHERE id = ?";   
  48.    Object[] params =newObject[] {id};   
  49.   int[] types =newint[] {Types.INTEGER};   
  50.    List items = jdbcTemplate.query(sql,params,types,newItemMapper());   
  51.   if(items.isEmpty()){   
  52.    returnnull;   
  53.    }   
  54.   return(Item)items.get(0);   
  55. }   
  56.   
  57. publicList<Item> findAll() {   
  58.    String sql ="SELECT * FROM items";   
  59.   returnjdbcTemplate.query(sql,newItemMapper());   
  60. }   
  61.   
  62.   
  63. publicList<Item> findAllByUser(intuser_id) {   
  64.    String sql ="SELECT * FROM items WHERE user_id = ?";   
  65.    Object[] params =newObject[]{user_id};   
  66.   int[] types =newint[]{Types.INTEGER};   
  67.    List items = jdbcTemplate.query(sql,params,types,newItemMapper());   
  68.   returnitems;   
  69. }   
  70.   
  71. protectedclassItemMapperimplements<SPANclass=hilite1>RowMapper</SPAN> {   
  72.   
  73.   publicObject mapRow(ResultSet rs,introwNum)throwsSQLException {   
  74.     Item item =newItem();   
  75.     item.setId(rs.getInt("id"));   
  76.     item.setUserId(rs.getInt("user_id"));   
  77.     item.setName(rs.getString("name"));   
  78.     item.setPhone(rs.getString("phone"));   
  79.     item.setEmail(rs.getString("email"));   
  80.   
  81.    returnitem;   
  82.    }   
  83.   
  84. }   
  85.   
  86.   
  87. }  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

第二个案例

package com.flmusic.cu.dao.jdbc;

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


import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.flmusic.cu.compere.CompereInfoBean;
import com.flmusic.cu.dao.CommendCompereDAO;


public class CommendCompereDAOJdbcImpl extends JdbcDaoSupport implements CommendCompereDAO{
/**
* 新增推荐主持人 状态:0--未审核;1--审核
* @param userId
*/
public void addCommendCompere(final Integer userId){
  
   StringBuffer sql = new StringBuffer(200);
   //新增推荐主持人
   sql.append("insert into u_commend_compere_info(I64UserID,status) values(?,0)");
   this.getJdbcTemplate().execute(sql.toString(),new PreparedStatementCallback(){
     public Object doInPreparedStatement(PreparedStatement ps)throws SQLException {
        ps.setInt(1, userId);
        ps.execute();
        return null;
     }
   });
   //更新推荐状态--已推荐(1)
   updateCommendFlag(userId,1);
}
/**
* 查询推荐主持人列表
* @return
*/
public List findCommendCompereList(Integer agentId){  
   StringBuffer sql = new StringBuffer(200);
   sql.append("select uc.compereName,uc.ilevel,com.I64UserID,com.status,a.agentName ");
   sql.append(" from u_commend_compere_info com ,u_compere_info uc left join webcu.dbo.agent a on uc.agentId=a.agentId ");
   sql.append("where com.I64UserID = uc.I64UserID ");
  
   if (agentId != null){
    sql.append(" and uc.agentId=").append(agentId);
   }
   List<CompereInfoBean> list = this.getJdbcTemplate().query(sql.toString(), new RowMapper(){
    public Object mapRow(ResultSet rs,int index) throws SQLException{
     CompereInfoBean bean = new CompereInfoBean();
     bean.setUserId(rs.getInt("I64UserID"));
     bean.setCompereName(rs.getString("compereName"));
     bean.setLevel(rs.getInt("ilevel"));
     bean.setCommendStatus(rs.getInt("status"));
     bean.setAgentName(rs.getString("agentName"));
     return bean;
    }
   });
   return list;
}
/**
* 更新推荐状态
* @param userId
* @param flag
*/
private void updateCommendFlag(final Integer userId,final int flag){
   //更新主持人的是否推荐状态
   StringBuffer sql = new StringBuffer(200);
   sql.append("update u_compere_info set commendFlag=? where I64UserID=?");
   this.getJdbcTemplate().execute(sql.toString(),new PreparedStatementCallback(){
     public Object doInPreparedStatement(PreparedStatement ps)throws SQLException {
        ps.setInt(1, flag);
        ps.setInt(2, userId);
        ps.execute();
        return null;
     }
   });
}

/**
* 根据室主Id查询主持人对象列表
*
*
*/
public List findCommendCompereListByHostId(Integer hostId) {
   // TODO Auto-generated method stub
   StringBuffer sql = new StringBuffer(200);
   sql.append("select a.I64UserID,a.compereName,a.iLevel from flMusicUserDB.dbo.u_compere_info a ");
   sql.append(" where a.compereNumber in (select compereNumber from RoomDB.dbo.Host_Compere_Mapping where hostNumber="+hostId+") ");
  
   List<CompereInfoBean> list = this.getJdbcTemplate().query(sql.toString(), new RowMapper(){
    public Object mapRow(ResultSet rs,int index) throws SQLException{
     CompereInfoBean bean = new CompereInfoBean();
     bean.setUserId(rs.getInt("I64UserID"));
     bean.setCompereName(rs.getString("compereName"));
     bean.setLevel(rs.getInt("ilevel"));
     return bean;
    }
   });
   return list;
}}


原创粉丝点击