Jdbc Template详解

  1. /**
  2. * Copyright: Copyright (c) 2005-2005
  3. * Company: JavaResearch(http://www.javaresearch.org)
  4. */
  5. package org.javaresearch.jerch;
  6. import java.lang.reflect.Method;
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.ResultSetMetaData;
  11. import java.sql.SQLException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import javax.sql.DataSource;
  15. /**
  16. * JDBC的模板定义,包含所有支持的方法。
  17. * 这个类是这个类库的核心,所有的实际功能差不多都是这个定义并完成的。
  18. * 最后更新日期:2005年5月7日
  19. * @author cherami
  20. */
  21. public class JDBCTemplate {
  22.   /**
  23.    * 数据源。
  24.    */
  25.   privateDataSource datasource;
  26.   /**
  27.    * 构造一个空的JDBCTemplate。
  28.    */
  29.   protectedJDBCTemplate() {
  30.   }
  31.   /**
  32.    * 以指定的数据源构造一个JDBCTemplate。
  33.    * @param datasource 数据源
  34.    */
  35.   protectedJDBCTemplate(DataSource datasource) {
  36.     this.datasource = datasource;
  37.   }
  38.  
  39.  
  40.   /**
  41.    * 设置当前JDBCTemplate的数据源。
  42.    * @param datasource 数据源
  43.    */
  44.   protectedvoid setDataSource(DataSource datasource) {
  45.     this.datasource = datasource;
  46.   }
  47.   /**
  48.    * 得到当前JDBCTemplate的数据源。
  49.    * @return 当前JDBCTemplate的数据源
  50.    */
  51.   protectedDataSource getDataSource() {
  52.     return datasource;
  53.   }
  54.   /**
  55.    * 使用psc创建PreparedStatement并使用pss设置相关的参数最后调用action执行。
  56.    * @param psc PreparedStatement的创建器
  57.    * @param pss PreparedStatement的参数设置器
  58.    * @param action PreparedStatement执行的回掉实现
  59.    * @return 执行后的结果
  60.    */
  61.   publicObject execute(PreparedStatementCreator psc,
  62.       PreparedStatementSetter pss, PreparedStatementCallback action) {
  63.     Connection con = Utils.getConnection(datasource);
  64.     PreparedStatement ps = null;
  65.     try {
  66.       ps = psc.createPreparedStatement(con);
  67.       pss.setValues(ps);
  68.       Object result = action.doInPreparedStatement(ps);
  69.       return result;
  70.     }
  71.     catch (SQLException ex) {
  72.       Utils.error("JDBCTemplate.EXECUTE_ERROR", ex);
  73.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR",ex);
  74.     } finally {
  75.       Utils.closeStatement(ps);
  76.       Utils.closeConnection(con);
  77.     }
  78.   }
  79.   /**
  80.    * 执行指定的sql并返回更新的记录数。
  81.    * @param sql SQL语句
  82.    * @return 更新的记录数
  83.    */
  84.   publicint update(String sql) {
  85.     return update(sql, nullPSS);
  86.   }
  87.   /**
  88.    * 执行指定的sql并返回更新的记录数。
  89.    * @param sql SQL语句
  90.    * @param args 参数中的值
  91.    * @return 更新的记录数
  92.    */
  93.   publicint update(String sql, Object[] args) {
  94.     return update(sql, new ArgPreparedStatementSetter(args));
  95.   }
  96.   /**
  97.    * 执行指定的sql并返回更新的记录数。
  98.    * @param sql SQL语句
  99.    * @param args 参数中的值
  100.    * @param types 参数类型
  101.    * @return 更新的记录数
  102.    */
  103.   publicint update(String sql, Object[] args, int[] types) {
  104.     return update(sql, new ArgTypePreparedStatementSetter(args, types));
  105.   }
  106.   /**
  107.    * 执行指定的sql并返回更新的记录数。
  108.    * @param sql SQL语句
  109.    * @param pss PreparedStatement的参数设置器
  110.    * @return 更新的记录数
  111.    */
  112.   publicint update(String sql, PreparedStatementSetter pss) {
  113.     Integer result = (Integer) execute(new SimplePreparedStatementCreator(sql),
  114.         pss, newPreparedStatementCallback() {
  115.           publicObject doInPreparedStatement(PreparedStatement stmt)
  116.               throwsSQLException {
  117.             returnnewInteger(stmt.executeUpdate());
  118.           }
  119.         });
  120.     return result.intValue();
  121.   }
  122.   /**
  123.    * 批量执行更新并返回每次的更新记录数
  124.    * @param sql SQL语句
  125.    * @param args 每次执行时的参数值
  126.    * @return 每次执行更新的记录数数组
  127.    */
  128.   publicint[] batchUpdate(String sql, Object[][] args) {
  129.     return batchUpdate(sql, new ArgBatchPreparedStatementSetter(args));
  130.   }
  131.   /**
  132.    * 批量执行更新并返回每次的更新记录数
  133.    * @param sql SQL语句
  134.    * @param args 每次执行时的参数值
  135.    * @param types 参数类型
  136.    * @return 每次执行更新的记录数数组
  137.    */
  138.   publicint[] batchUpdate(String sql, Object[][] args, int[] types) {
  139.     return batchUpdate(sql,
  140.         new ArgTypeBatchPreparedStatementSetter(args, types));
  141.   }
  142.   /**
  143.    * 批量执行更新并返回每次的更新记录数
  144.    * @param sql SQL语句
  145.    * @param bpss PreparedStatement的批量参数设置器
  146.    * @return 每次执行更新的记录数数组
  147.    */
  148.   publicint[] batchUpdate(String sql, BatchPreparedStatementSetter bpss) {
  149.     return batchUpdate(sql, new BatchPreparedStatementSetterConverter(bpss));
  150.   }
  151.   /**
  152.    * 批量执行更新并返回每次的更新记录数
  153.    * @param sql SQL语句
  154.    * @param pss PreparedStatement的参数设置器
  155.    * @return 每次执行更新的记录数数组
  156.    */
  157.   publicint[] batchUpdate(String sql, PreparedStatementSetter pss) {
  158.     return (int[]) execute(new SimplePreparedStatementCreator(sql), pss,
  159.         newPreparedStatementCallback() {
  160.           publicObject doInPreparedStatement(PreparedStatement stmt)
  161.               throwsSQLException {
  162.             return stmt.executeBatch();
  163.           }
  164.         });
  165.   }
  166.   /**
  167.    * 查询一个整型结果。
  168.    * @param sql SQL语句
  169.    * @return 查询的第一行的第一个字段的整型值。
  170.    */
  171.   publicint queryForInt(String sql) {
  172.     return queryForInt(sql, nullPSS);
  173.   }
  174.   /**
  175.    * 查询一个整型结果。
  176.    * @param sql SQL语句
  177.    * @param args 参数中的值
  178.    * @return 查询的第一行的第一个字段的整型值。
  179.    */
  180.   publicint queryForInt(String sql, Object[] args) {
  181.     return queryForInt(sql, new ArgPreparedStatementSetter(args));
  182.   }
  183.   /**
  184.    * 查询一个整型结果。
  185.    * @param sql SQL语句
  186.    * @param args 参数中的值
  187.    * @param types 参数类型
  188.    * @return 查询的第一行的第一个字段的整型值。
  189.    */
  190.   publicint queryForInt(String sql, Object[] args, int[] types) {
  191.     return queryForInt(sql, new ArgTypePreparedStatementSetter(args, types));
  192.   }
  193.   /**
  194.    *  查询一个整型结果。
  195.    * @param sql SQL语句
  196.    * @param pss PreparedStatement的参数设置器
  197.    * @return 查询的第一行的第一个字段的整型值。
  198.    */
  199.   publicint queryForInt(String sql, PreparedStatementSetter pss) {
  200.     Number result = (Number) queryObject(sql, pss,
  201.         new ObjectResultRowExtractor());
  202.     if (result==null) {
  203.       return 0;
  204.     }
  205.     return result.intValue();
  206.   }
  207.   /**
  208.    * 查询一个长整型结果。
  209.    * @param sql SQL语句
  210.    * @return 查询的第一行的第一个字段的长整型值。
  211.    */
  212.   publiclong queryForLong(String sql) {
  213.     return queryForLong(sql, nullPSS);
  214.   }
  215.   /**
  216.    * 查询一个长整型结果。
  217.    * @param sql SQL语句
  218.    * @param args 参数中的值
  219.    * @return 查询的第一行的第一个字段的长整型值。
  220.    */
  221.   publiclong queryForLong(String sql, Object[] args) {
  222.     return queryForLong(sql, new ArgPreparedStatementSetter(args));
  223.   }
  224.   /**
  225.    * 查询一个长整型结果。
  226.    * @param sql SQL语句
  227.    * @param args 参数中的值
  228.    * @param types 参数类型
  229.    * @return 查询的第一行的第一个字段的长整型值。
  230.    */
  231.   publiclong queryForLong(String sql, Object[] args, int[] types) {
  232.     return queryForLong(sql, new ArgTypePreparedStatementSetter(args, types));
  233.   }
  234.   /**
  235.    *  查询一个长整型结果。
  236.    * @param sql SQL语句
  237.    * @param pss PreparedStatement的参数设置器
  238.    * @return 查询的第一行的第一个字段的长整型值。
  239.    */
  240.   publiclong queryForLong(String sql, PreparedStatementSetter pss) {
  241.     Number result = (Number) queryObject(sql, pss,
  242.         new ObjectResultRowExtractor());
  243.     if (result==null) {
  244.       return 0;
  245.     }
  246.     return result.longValue();
  247.   }
  248.   /**
  249.    * 查询一个字符串结果。
  250.    * @param sql SQL语句
  251.    * @return 查询的第一行的第一个字段的字符串值。
  252.    */
  253.   publicString queryString(String sql) {
  254.     return queryString(sql, nullPSS);
  255.   }
  256.   /**
  257.    * 查询一个字符串结果。
  258.    * @param sql SQL语句
  259.    * @param args 参数中的值
  260.    * @return 查询的第一行的第一个字段的字符串值。
  261.    */
  262.   publicString queryString(String sql, Object[] args) {
  263.     return queryString(sql, new ArgPreparedStatementSetter(args));
  264.   }
  265.   /**
  266.    * 查询一个字符串结果。
  267.    * @param sql SQL语句
  268.    * @param args 参数中的值
  269.    * @param types 参数类型
  270.    * @return 查询的第一行的第一个字段的字符串值。
  271.    */
  272.   publicString queryString(String sql, Object[] args, int[] types) {
  273.     return queryString(sql, new ArgTypePreparedStatementSetter(args, types));
  274.   }
  275.   /**
  276.    *  查询一个字符串结果。
  277.    * @param sql SQL语句
  278.    * @param pss PreparedStatement的参数设置器
  279.    * @return 查询的第一行的第一个字段的字符串值。
  280.    */
  281.   publicString queryString(String sql, PreparedStatementSetter pss) {
  282.     return (String) queryObject(sql, pss, new StringResultRowExtractor());
  283.   }
  284.   /**
  285.    * 查询一个对象结果。
  286.    * @param sql SQL语句
  287.    * @param rre 单行结果提取器
  288.    * @return 查询的第一行的结果并使用rre转换为结果对象。
  289.    */
  290.   publicObject queryObject(String sql, ResultRowExtractor rre) {
  291.     return queryObject(sql, nullPSS, rre);
  292.   }
  293.   /**
  294.    * 查询一个对象结果。
  295.    * @param sql SQL语句
  296.    * @param args 参数中的值
  297.    * @param rre 单行结果提取器
  298.    * @return 查询的第一行的结果并使用rre转换为结果对象。
  299.    */
  300.   publicObject queryObject(String sql, Object[] args, ResultRowExtractor rre) {
  301.     return queryObject(sql, new ArgPreparedStatementSetter(args), rre);
  302.   }
  303.   /**
  304.    * 查询一个对象结果。
  305.    * @param sql SQL语句
  306.    * @param args 参数中的值
  307.    * @param types 参数类型
  308.    * @param rre 单行结果提取器
  309.    * @return 查询的第一行的结果并使用rre转换为结果对象。
  310.    */
  311.   publicObject queryObject(String sql, Object[] args, int[] types,
  312.       ResultRowExtractor rre) {
  313.     return queryObject(sql, new ArgTypePreparedStatementSetter(args, types),
  314.         rre);
  315.   }
  316.   /**
  317.    * 查询一个对象结果。
  318.    * @param sql SQL语句
  319.    * @param pss PreparedStatement的参数设置器
  320.    * @param rre 单行结果提取器
  321.    * @return 查询的第一行的结果并使用rre转换为结果对象。
  322.    */
  323.   publicObject queryObject(String sql, PreparedStatementSetter pss,
  324.       finalResultRowExtractor rre) {
  325.     Object result = (Object) execute(new SimplePreparedStatementCreator(sql),
  326.         pss, newPreparedStatementCallback() {
  327.           publicObject doInPreparedStatement(PreparedStatement ps)
  328.               throwsSQLException {
  329.             ResultSet rs = null;
  330.             try {
  331.               rs = ps.executeQuery();
  332.               if (rs.next()) {
  333.                 return rre.extractRow(rs);
  334.               } else {
  335.                 returnnull;
  336.               }
  337.             }
  338.             catch (SQLException e) {
  339.               Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  340.               thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  341.             } finally {
  342.               Utils.closeResultSet(rs);
  343.             }
  344.           }
  345.         });
  346.     return result;
  347.   }
  348.   /**
  349.    * 查询一个对象列表结果。
  350.    * @param sql SQL语句
  351.    * @param rre 单行结果提取器
  352.    * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  353.    */
  354.   publicList query(String sql, ResultRowExtractor rre) {
  355.     return (List) execute(new SimplePreparedStatementCreator(sql), nullPSS,
  356.         new ResultRowListPreparedStatementCallback(rre));
  357.   }
  358.   /**
  359.    * 查询结果。
  360.    * @param sql SQL语句
  361.    * @param rse 全部结果提取器。
  362.    * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  363.    */
  364.   publicObject query(String sql, finalResultSetExtractor rse) {
  365.     return (Object) execute(new SimplePreparedStatementCreator(sql), nullPSS,
  366.         new ResultSetPreparedStatementCallback(rse));
  367.   }
  368.   /**
  369.    * 查询一个对象列表结果。
  370.    * @param sql SQL语句
  371.    * @param args 参数中的值
  372.    * @param types 参数类型
  373.    * @param rre 单行结果提取器
  374.    * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  375.    */
  376.   publicList query(String sql, Object[] args, int[] types, ResultRowExtractor rre) {
  377.     return (List) execute(new SimplePreparedStatementCreator(sql),
  378.         new ArgTypePreparedStatementSetter(args,types),
  379.         new ResultRowListPreparedStatementCallback(rre));
  380.   }
  381.   /**
  382.    * 查询结果。
  383.    * @param sql SQL语句
  384.    * @param args 参数中的值
  385.    * @param types 参数类型
  386.    * @param rse 全部结果提取器。
  387.    * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  388.    */
  389.   publicObject query(String sql, Object[] args, int[] types, ResultSetExtractor rse) {
  390.     return (Object) execute(new SimplePreparedStatementCreator(sql),
  391.         new ArgTypePreparedStatementSetter(args,types),
  392.         new ResultSetPreparedStatementCallback(rse));
  393.   }
  394.   /**
  395.    * 查询一个对象列表结果。
  396.    * @param sql SQL语句
  397.    * @param args 参数中的值
  398.    * @param rre 单行结果提取器
  399.    * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  400.    */
  401.   publicList query(String sql, Object[] args, ResultRowExtractor rre) {
  402.     return (List) execute(new SimplePreparedStatementCreator(sql),
  403.         new ArgPreparedStatementSetter(args),
  404.         new ResultRowListPreparedStatementCallback(rre));
  405.   }
  406.   /**
  407.    * 查询结果。
  408.    * @param sql SQL语句
  409.    * @param args 参数中的值
  410.    * @param rse 全部结果提取器。
  411.    * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  412.    */
  413.   publicObject query(String sql, Object[] args, ResultSetExtractor rse) {
  414.     return (Object) execute(new SimplePreparedStatementCreator(sql),
  415.         new ArgPreparedStatementSetter(args),
  416.         new ResultSetPreparedStatementCallback(rse));
  417.   }
  418.   /**
  419.    * 查询一个对象列表结果。
  420.    * @param sql SQL语句
  421.    * @param pss PreparedStatement的参数设置器
  422.    * @param rre 单行结果提取器
  423.    * @return 查询所有结果并使用rre将每行结果转换为结果对象元素。
  424.    */
  425.   publicList query(String sql, PreparedStatementSetter pss,
  426.       ResultRowExtractor rre) {
  427.     return (List) execute(new SimplePreparedStatementCreator(sql), pss,
  428.         new ResultRowListPreparedStatementCallback(rre));
  429.   }
  430.   /**
  431.    * 查询结果。
  432.    * @param sql SQL语句
  433.    * @param pss PreparedStatement的参数设置器
  434.    * @param rse 全部结果提取器。
  435.    * @return 查询结果并根据自定义的结果提取器提取数据并返回。
  436.    */
  437.   publicObject query(String sql, PreparedStatementSetter pss,
  438.       ResultSetExtractor rse) {
  439.     return (Object) execute(new SimplePreparedStatementCreator(sql), pss,
  440.         new ResultSetPreparedStatementCallback(rse));
  441.   }
  442.   /**
  443.    * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  444.    * @param sql SQL语句
  445.    * @param bean 结果Bean的类型
  446.    * @return 第一行的结果并转换为Bean实例。
  447.    */
  448.   publicObject queryForBean(String sql, Class bean) {
  449.     try {
  450.       Object obj = bean.newInstance();
  451.       if (obj instanceofMappable) {
  452.         return queryObject(sql, nullPSS, new MappableResultRowExtractor(obj,
  453.             (Mappable) obj));
  454.       } else {
  455.         return queryObject(sql, nullPSS, new MappableResultRowExtractor(obj,
  456.             newNameMatchMappable()));
  457.       }
  458.     }
  459.     catch (Exception e) {
  460.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  461.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  462.     }
  463.   }
  464.   /**
  465.    * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  466.    * @param sql SQL语句
  467.    * @param args 参数中的值
  468.    * @param bean 结果Bean的类型
  469.    * @return 第一行的结果并转换为Bean实例。
  470.    */
  471.   publicObject queryForBean(String sql, Object[] args, Class bean) {
  472.     try {
  473.       Object obj = bean.newInstance();
  474.       if (obj instanceofMappable) {
  475.         return queryObject(sql, new ArgPreparedStatementSetter(args),
  476.             new MappableResultRowExtractor(obj, (Mappable) obj));
  477.       } else {
  478.         return queryObject(sql, new ArgPreparedStatementSetter(args),
  479.             new MappableResultRowExtractor(obj, newNameMatchMappable()));
  480.       }
  481.     }
  482.     catch (Exception e) {
  483.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  484.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  485.     }
  486.   }
  487.   /**
  488.    * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  489.    * @param sql SQL语句
  490.    * @param args 参数中的值
  491.    * @param types 参数类型
  492.    * @param bean 结果Bean的类型
  493.    * @return 第一行的结果并转换为Bean实例。
  494.    */
  495.   publicObject queryForBean(String sql, Object[] args, int[] types, Class bean) {
  496.     try {
  497.       Object obj = bean.newInstance();
  498.       if (obj instanceofMappable) {
  499.         return queryObject(sql, new ArgTypePreparedStatementSetter(args,types),
  500.             new MappableResultRowExtractor(obj, (Mappable) obj));
  501.       } else {
  502.         return queryObject(sql, new ArgTypePreparedStatementSetter(args,types),
  503.             new MappableResultRowExtractor(obj, newNameMatchMappable()));
  504.       }
  505.     }
  506.     catch (Exception e) {
  507.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  508.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  509.     }
  510.   }
  511.   /**
  512.    * 查询并返回第一行的结果并根据字段名自动转换为bean的对应属性。
  513.    * @param sql SQL语句
  514.    * @param pss PreparedStatement的参数设置器
  515.    * @param bean 结果Bean的类型
  516.    * @return 第一行的结果并转换为Bean实例。
  517.    */
  518.   publicObject queryForBean(String sql, PreparedStatementSetter pss, Class bean) {
  519.     try {
  520.       Object obj = bean.newInstance();
  521.       if (obj instanceofMappable) {
  522.         return queryObject(sql, pss,
  523.             new MappableResultRowExtractor(obj, (Mappable) obj));
  524.       } else {
  525.         return queryObject(sql, pss,
  526.             new MappableResultRowExtractor(obj, newNameMatchMappable()));
  527.       }
  528.     }
  529.     catch (Exception e) {
  530.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  531.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  532.     }
  533.   }
  534.   /**
  535.    * 查询并返回第一行的结果并使用m将其转换为bean。
  536.    * @param sql SQL语句
  537.    * @param bean 结果Bean的类型
  538.    * @param m 字段-属性映射器
  539.    * @return 第一行的结果并使用m将其转换为Bean实例。
  540.    */
  541.   publicObject queryForBean(String sql, Class bean, Mappable m) {
  542.     try {
  543.       return queryObject(sql, nullPSS, new MappableResultRowExtractor(bean
  544.           .newInstance(), m));
  545.     }
  546.     catch (Exception e) {
  547.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  548.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  549.     }
  550.   }
  551.   /**
  552.    * 查询并返回第一行的结果并使用m将其转换为bean。
  553.    * @param sql SQL语句
  554.    * @param args 参数中的值
  555.    * @param bean 结果Bean的类型
  556.    * @param m 字段-属性映射器
  557.    * @return 第一行的结果并使用m将其转换为Bean实例。
  558.    */
  559.   publicObject queryForBean(String sql, Object[] args, Class bean, Mappable m) {
  560.     try {
  561.       return queryObject(sql, new ArgPreparedStatementSetter(args),
  562.           new MappableResultRowExtractor(bean.newInstance(), m));
  563.     }
  564.     catch (Exception e) {
  565.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  566.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  567.     }
  568.   }
  569.   /**
  570.    * 查询并返回第一行的结果并使用m将其转换为bean。
  571.    * @param sql SQL语句
  572.    * @param args 参数中的值
  573.    * @param types 参数类型
  574.    * @param bean 结果Bean的类型
  575.    * @param m 字段-属性映射器
  576.    * @return 第一行的结果并使用m将其转换为Bean实例。
  577.    */
  578.   publicObject queryForBean(String sql, Object[] args, int[] types, Class bean, Mappable m) {
  579.     try {
  580.       return queryObject(sql, new ArgTypePreparedStatementSetter(args,types),
  581.           new MappableResultRowExtractor(bean.newInstance(), m));
  582.     }
  583.     catch (Exception e) {
  584.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  585.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR", e);
  586.     }
  587.   }
  588.   /**
  589.    * 查询并返回第一行的结果并使用m将其转换为bean。
  590.    * @param sql SQL语句
  591.    * @param pss PreparedStatement的参数设置器
  592.    * @param bean 结果Bean的类型
  593.    * @param m 字段-属性映射器
  594.    * @return 第一行的结果并使用m将其转换为Bean实例。
  595.    */
  596.   publicObject queryForBean(String sql, PreparedStatementSetter pss,
  597.       Class bean, Mappable m) {
  598.     try {
  599.       return queryObject(sql, pss, new MappableResultRowExtractor(bean
  600.           .newInstance(), m));
  601.     }
  602.     catch (Exception e) {
  603.       Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  604.       thrownewJerchException("JDBCTemplate.EXECUTE_ERROR",e);
  605.     }
  606.   }
  607.   /**
  608.    * 无任何参数的PreparedStatement设置器实例。
  609.    */
  610.   privatestaticfinalPreparedStatementSetter nullPSS = newPreparedStatementSetter() {
  611.     publicvoid setValues(PreparedStatement ps) {
  612.     }
  613.   };
  614.   /**
  615.    * 将参数数组转换为PreparedStatement的设置器的简单适配器。
  616.    */
  617.   privatestaticclass ArgPreparedStatementSetter implements
  618.       PreparedStatementSetter {
  619.     privatefinalObject[] args;
  620.     public ArgPreparedStatementSetter(Object[] args) {
  621.       this.args = args;
  622.     }
  623.     publicvoid setValues(PreparedStatement ps) throwsSQLException {
  624.       if (this.args != null) {
  625.         for (int i = 0; i < this.args.length; i++) {
  626.           ps.setObject(i + 1, this.args[i]);
  627.         }
  628.       }
  629.     }
  630.   }
  631.   /**
  632.    * 将参数数组和其类型转换为PreparedStatement的设置器的简单适配器。
  633.    */
  634.   privatestaticclass ArgTypePreparedStatementSetter implements
  635.       PreparedStatementSetter {
  636.     privatefinalObject[] args;
  637.     privatefinalint[] types;
  638.     public ArgTypePreparedStatementSetter(Object[] args, int[] types) {
  639.       if ((args != null && types == null)
  640.           || (args == null && types != null)
  641.           || (args != null && args.length != types.length)) {
  642.         thrownewJerchException("ArgTypePreparedStatementSetter.ARG_NOT_MATCH");
  643.       }
  644.       this.args = args;
  645.       this.types = types;
  646.     }
  647.     publicvoid setValues(PreparedStatement ps) throwsSQLException {
  648.       if (this.args != null) {
  649.         for (int i = 0; i < this.args.length; i++) {
  650.           ps.setObject(i + 1, this.args[i], this.types[i]);
  651.         }
  652.       }
  653.     }
  654.   }
  655.   /**
  656.    * 将sql语句转换为PreparedStatement的创建器的简单适配器。
  657.    */
  658.   privatestaticclass SimplePreparedStatementCreator implements
  659.       PreparedStatementCreator {
  660.     privatefinalString sql;
  661.     public SimplePreparedStatementCreator(String sql) {
  662.       this.sql = sql;
  663.     }
  664.     publicPreparedStatement createPreparedStatement(Connection con)
  665.         throwsSQLException {
  666.       return con.prepareStatement(this.sql);
  667.     }
  668.   }
  669.   /**
  670.    * 将第一个字段作为String类型提取的单行结果提取器。
  671.    */
  672.   privatestaticclass StringResultRowExtractor implementsResultRowExtractor {
  673.     publicObject extractRow(java.sql.ResultSet rs) throwsSQLException {
  674.       return rs.getString(1);
  675.     }
  676.   }
  677.   /**
  678.    * 将第一个字段作为Object类型提取的单行结果提取器。
  679.    */
  680.   privatestaticclass ObjectResultRowExtractor implementsResultRowExtractor {
  681.     publicObject extractRow(java.sql.ResultSet rs) throwsSQLException {
  682.       return rs.getObject(1);
  683.     }
  684.   }
  685.   /**
  686.    * 将BatchPreparedStatement设置其转换为PreparedStatement设置器的适配器。
  687.    */
  688.   privatestaticclass BatchPreparedStatementSetterConverter implements
  689.       PreparedStatementSetter {
  690.     BatchPreparedStatementSetter bpss;
  691.     public BatchPreparedStatementSetterConverter(
  692.         BatchPreparedStatementSetter bpss) {
  693.       this.bpss = bpss;
  694.     }
  695.     publicvoid setValues(PreparedStatement ps) throwsSQLException {
  696.       for (int i = 0; i < bpss.getBatchSize(); i++) {
  697.         bpss.setValues(ps, i);
  698.         ps.addBatch();
  699.       }
  700.     }
  701.   }
  702.   /**
  703.    * 将批量参数数组转换为PreparedStatement设置器的适配器。
  704.    */
  705.   privatestaticclass ArgBatchPreparedStatementSetter implements
  706.       PreparedStatementSetter {
  707.     privatefinalObject[][] args;
  708.     public ArgBatchPreparedStatementSetter(Object[][] args) {
  709.       this.args = args;
  710.     }
  711.     publicvoid setValues(PreparedStatement ps) throwsSQLException {
  712.       if (this.args != null) {
  713.         for (int i = 0; i < this.args.length; i++) {
  714.           Object[] arg = args[i];
  715.           for (int j = 0; j < arg.length; j++) {
  716.             ps.setObject(j + 1, arg[j]);
  717.           }
  718.           ps.addBatch();
  719.         }
  720.       }
  721.     }
  722.   }
  723.   /**
  724.    * 将批量参数数组和对应类型转换为PreparedStatement设置器的适配器。
  725.    */
  726.   privatestaticclass ArgTypeBatchPreparedStatementSetter implements
  727.       PreparedStatementSetter {
  728.     privatefinalObject[][] args;
  729.     privatefinalint[] types;
  730.     public ArgTypeBatchPreparedStatementSetter(Object[][] args, int[] types) {
  731.       if ((args != null && types == null)
  732.           || (args == null && types != null)
  733.           || (args != null && args.length != types.length)) {
  734.         thrownewJerchException("ArgTypePreparedStatementSetter.ARG_NOT_MATCH");
  735.       }
  736.       this.args = args;
  737.       this.types = types;
  738.     }
  739.     publicvoid setValues(PreparedStatement ps) throwsSQLException {
  740.       if (this.args != null) {
  741.         for (int i = 0; i < this.args.length; i++) {
  742.           Object[] arg = args[i];
  743.           for (int j = 0; j < arg.length; j++) {
  744.             ps.setObject(j + 1, arg[j], types[j]);
  745.           }
  746.           ps.addBatch();
  747.         }
  748.       }
  749.     }
  750.   }
  751.   /**
  752.    * 使用ResultRowExtractor提取单行结果并将全部结果添加到List的PreparedStatement回调实现。
  753.    */
  754.   privatestaticclass ResultRowListPreparedStatementCallback implements
  755.       PreparedStatementCallback {
  756.     ResultRowExtractor rre;
  757.     public ResultRowListPreparedStatementCallback(ResultRowExtractor rre) {
  758.       this.rre = rre;
  759.     }
  760.     publicObject doInPreparedStatement(PreparedStatement ps)
  761.         throwsSQLException {
  762.       ResultSet rs = null;
  763.       List result = newArrayList();
  764.       try {
  765.         rs = ps.executeQuery();
  766.         while (rs.next()) {
  767.           result.add(rre.extractRow(rs));
  768.         }
  769.       }
  770.       catch (SQLException e) {
  771.         Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  772.         thrownewJerchException("JDBCTemplate.EXECUTE_ERROR",e);
  773.       } finally {
  774.         Utils.closeResultSet(rs);
  775.       }
  776.       return result;
  777.     }
  778.   }
  779.   /**
  780.    * 使用ResultSetExtractor提取全部结果的PreparedStatement回调实现。
  781.    */
  782.   privatestaticclass ResultSetPreparedStatementCallback implements
  783.       PreparedStatementCallback {
  784.     ResultSetExtractor rse;
  785.     public ResultSetPreparedStatementCallback(ResultSetExtractor rse) {
  786.       this.rse = rse;
  787.     }
  788.     publicObject doInPreparedStatement(PreparedStatement ps)
  789.         throwsSQLException {
  790.       ResultSet rs = null;
  791.       try {
  792.         rs = ps.executeQuery();
  793.         return rse.extractSet(rs);
  794.       }
  795.       catch (SQLException e) {
  796.         Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  797.         thrownewJerchException("JDBCTemplate.EXECUTE_ERROR",e);
  798.       } finally {
  799.         Utils.closeResultSet(rs);
  800.       }
  801.     }
  802.   }
  803.   /**
  804.    * 使用Mappable将单行结果转换为对象实例的ResultRow提取器实现。
  805.    */
  806.   privatestaticclass MappableResultRowExtractor implementsResultRowExtractor {
  807.     privateMappable map;
  808.     privateObject obj;
  809.     public MappableResultRowExtractor(Object obj, Mappable map) {
  810.       this.obj = obj;
  811.       this.map = map;
  812.     }
  813.     publicObject extractRow(ResultSet rs) throwsSQLException {
  814.       Class c = obj.getClass();
  815.       ResultSetMetaData metaData = rs.getMetaData();
  816.       int count = metaData.getColumnCount();
  817.       try {
  818.         for (int i = 0; i < count; i++) {
  819.           String fieldName = metaData.getColumnName(i + 1);
  820.           int type = metaData.getColumnType(i + 1);
  821.           Class[] types = { map.getMethodParameterType(fieldName, type) };
  822.           Method m = c.getMethod(map.getMapMethod(fieldName), types);
  823.           Object[] args = { ValueConverterFactory.convert(rs
  824.               .getObject(fieldName), types[0]) };
  825.           m.invoke(obj, args);
  826.         }
  827.       }
  828.       catch (Exception e) {
  829.         Utils.error("JDBCTemplate.EXECUTE_ERROR", e);
  830.         thrownewJerchException("JDBCTemplate.EXECUTE_ERROR",e);
  831.       }
  832.       return obj;
  833.     }
  834.   }
  835. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值