Spring JdbcTemplate 查询方法中的RowMapper实现汇总

本文通过一个具体的例子展示了如何使用 Spring 框架中的 JDBC 模块进行数据库操作,包括插入、更新、删除及查询等基本功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.chinaunix.net/uid-10018502-id-2972078.html

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

从orcale更换到mysql,springbatch运行偶尔会报错: org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0 at org.springframework.dao.support.DataAccessUtils.nullableSingleResult(DataAccessUtils.java:97) at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:784) at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:809) at org.springframework.batch.core.repository.dao.JdbcJobExecutionDao.synchronizeStatus(JdbcJobExecutionDao.java:308) at org.springframework.batch.core.repository.support.SimpleJobRepository.update(SimpleJobRepository.java:166) at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:366) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:99) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy85.update(Unknown Source) at sun.reflect.GeneratedMethodAccessor147.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
最新发布
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值