1 16 17 package org.springframework.jdbc.core.simple; 18 19 import java.util.List ; 20 import java.util.Map ; 21 22 import javax.sql.DataSource ; 23 24 import org.springframework.dao.DataAccessException; 25 import org.springframework.jdbc.core.JdbcOperations; 26 import org.springframework.jdbc.core.JdbcTemplate; 27 28 47 public class SimpleJdbcTemplate implements SimpleJdbcOperations { 48 49 50 private final JdbcOperations classicJdbcTemplate; 51 52 53 58 public SimpleJdbcTemplate(DataSource dataSource) { 59 this.classicJdbcTemplate = new JdbcTemplate(dataSource); 60 } 61 62 66 public SimpleJdbcTemplate(JdbcOperations classicJdbcTemplate) { 67 this.classicJdbcTemplate = classicJdbcTemplate; 68 } 69 70 74 public JdbcOperations getJdbcOperations() { 75 return this.classicJdbcTemplate; 76 } 77 78 79 public int queryForInt(String sql, Object ... args) throws DataAccessException { 80 return args == null || args.length == 0 ? 81 getJdbcOperations().queryForInt(sql) : 82 getJdbcOperations().queryForInt(sql, args); 83 } 84 85 public long queryForLong(String sql, Object ... args) throws DataAccessException { 86 return (args == null || args.length == 0) ? 87 getJdbcOperations().queryForLong(sql) : 88 getJdbcOperations().queryForLong(sql, args); 89 } 90 91 @SuppressWarnings ("unchecked") 92 public <T> T queryForObject(String sql, Class <T> requiredType, Object ... args) throws DataAccessException { 93 return (T)((args == null || args.length == 0) ? 94 getJdbcOperations().queryForObject(sql, requiredType) : 95 getJdbcOperations().queryForObject(sql, args, requiredType)); 96 } 97 98 @SuppressWarnings ("unchecked") 99 public <T> T queryForObject(String sql, ParameterizedRowMapper<T> rm, Object ... args) throws DataAccessException { 100 return (T)((args == null || args.length == 0)? 101 getJdbcOperations().queryForObject(sql, rm): 102 getJdbcOperations().queryForObject(sql, args, rm)); 103 } 104 105 @SuppressWarnings ("unchecked") 106 public <T> List <T> query(String sql, ParameterizedRowMapper<T> rm, Object ... args) throws DataAccessException { 107 return (List <T>)((args == null || args.length == 0) ? 108 getJdbcOperations().query(sql, rm) : 109 getJdbcOperations().query(sql, args, rm)); 110 } 111 112 @SuppressWarnings ("unchecked") 113 public Map <String , Object > queryForMap(String sql, Object ... args) throws DataAccessException { 114 return (args == null || args.length == 0) ? 115 getJdbcOperations().queryForMap(sql) : 116 getJdbcOperations().queryForMap(sql, args); 117 } 118 119 @SuppressWarnings ("unchecked") 120 public List <Map <String , Object >> queryForList(String sql, Object ... args) throws DataAccessException { 121 return (args == null || args.length == 0) ? 122 getJdbcOperations().queryForList(sql) : 123 getJdbcOperations().queryForList(sql, args); 124 } 125 126 public int update(String sql, Object ... args) throws DataAccessException { 127 return (args == null || args.length == 0) ? 128 getJdbcOperations().update(sql) : 129 getJdbcOperations().update(sql, args); 130 } 131 132 } 133 | Popular Tags |