现在的位置: 首页 > 综合 > 正文

Spring JDBCTemplate Query方法查询,改善系统效能

2013年03月26日 ⁄ 综合 ⁄ 共 2704字 ⁄ 字号 评论关闭
       近日系统有一个打印采购单的功能,发现连续打印多张后,主机宕机,看了下service和dao层的实现,很繁杂,估计原因主要出在组页面资料的时候,循环套循环导致效能下降,然后想想有什么办法可以减少组资料时的一些对象转换,dao层取回来的是map,然后还要转换成vo,然后又循环组资料,google下资料,发现jdbctemplate查询还有个query方法,可以直接在查好后返回你需要的vo,改好上线,不知道对效能有没有帮助,不过这个做法感觉不错,特把文章贴在blog里面,以备后用,呵呵

   

在内部建立内联类实现RowMapper接口
  1. package hysteria.contact.dao.impl;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.sql.Types;
  5. import java.util.List;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import org.springframework.jdbc.core.RowMapper;
  8. import hysteria.contact.dao.ItemDAO;
  9. import hysteria.contact.domain.Item;
  10. public class ItemDAOImpl implements ItemDAO {
  11.  private JdbcTemplate jdbcTemplate;
  12.  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  13.   this.jdbcTemplate = jdbcTemplate;
  14.  }
  15.  public Item insert(Item item) {
  16.   String sql = "INSERT INTO items(user_id,name,phone,email) VALUES(?,?,?,?)";
  17.   Object[] params = new Object[]{item.getUserId(),item.getName(),item.getPhone(),item.getEmail()};
  18.   int[] types = new int[]{Types.INTEGER,Types.VARCHAR,Types.CHAR,Types.VARCHAR};
  19.   jdbcTemplate.update(sql,params,types);
  20.   return item;
  21.  }
  22.  public Item update(Item item) {
  23.   String sql = "UPDATE items SET name = ?, phone = ?, email = ? WHERE id = ?";
  24.   Object[] params = new Object[] {item.getName(),item.getPhone(),item.getEmail(),item.getId()};
  25.   int[] types = new int[] {Types.VARCHAR,Types.CHAR,Types.VARCHAR,Types.VARCHAR,Types.INTEGER};
  26.   jdbcTemplate.update(sql,params,types);
  27.   return item;
  28.  }
  29.  public void delete(Item item) {
  30.   String sql = "DELETE FROM items WHERE id = ?";
  31.   Object[] params = new Object[] {item.getId()};
  32.   int[] types = new int[]{Types.INTEGER};
  33.   jdbcTemplate.update(sql,params,types);
  34.  }
  35.  public Item findById(int id) {
  36.   String sql = "SELECT * FROM items WHERE id = ?";
  37.   Object[] params = new Object[] {id};
  38.   int[] types = new int[] {Types.INTEGER};
  39.   List items = jdbcTemplate.query(sql,params,types,new ItemMapper());
  40.   if(items.isEmpty()){
  41.    return null;
  42.   }
  43.   return (Item)items.get(0);
  44.  }
  45.  public List<Item> findAll() {
  46.   String sql = "SELECT * FROM items";
  47.   return jdbcTemplate.query(sql,new ItemMapper());
  48.  }
  49.  public List<Item> findAllByUser(int user_id) {
  50.   String sql = "SELECT * FROM items WHERE user_id = ?";
  51.   Object[] params = new Object[]{user_id};
  52.   int[] types = new int[]{Types.INTEGER};
  53.   List items = jdbcTemplate.query(sql,params,types,new ItemMapper());
  54.   return items;
  55.  }
  56.  protected class ItemMapper implements RowMapper {
  57.   public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
  58.    Item item = new Item();
  59.    item.setId(rs.getInt("id"));
  60.    item.setUserId(rs.getInt("user_id"));
  61.    item.setName(rs.getString("name"));
  62.    item.setPhone(rs.getString("phone"));
  63.    item.setEmail(rs.getString("email"));
  64.    return item;
  65.   }
  66.  }
  67. }

抱歉!评论已关闭.