gistfile1.txt @Componentpublic class BaseJdbcTemplate {@Autowiredprivate JdbcTemplate jdbcTemplate;static final Object[] NULL_PARA_ARRAY = new Object[0];private ModelBuilder modelBuilder = ModelBuilder.me();public List findAll(String sql,Cl
@Component public class BaseJdbcTemplate { @Autowired private JdbcTemplate jdbcTemplate; static final Object[] NULL_PARA_ARRAY = new Object[0]; private ModelBuilder modelBuilder = ModelBuilder.me(); publicList findAll(String sql,Class cla,Object... paras){ Connection connection; try { connection = jdbcTemplate.getDataSource().getConnection(); return find(connection, sql, cla, paras); } catch (SQLException e) { e.printStackTrace(); } return null; } public List findAll(String sql,Class cla){ Connection connection; try { connection = jdbcTemplate.getDataSource().getConnection(); return find(connection, sql,cla); } catch (SQLException e) { e.printStackTrace(); } return null; } private List find(Connection connection, String sql,Class cla) { PreparedStatement preparedStatement; try { preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery(); List modelList = modelBuilder.builderModelList(resultSet,cla); connection.close(); return modelList; } catch (SQLException e) { e.printStackTrace(); } return null; } public T findOne(String sql,Class cla){ List result = findAll(sql, cla,NULL_PARA_ARRAY); return result.size()> 0 ? result.get(0) : null; } public T findOne(String sql,Class cla,Object... paras) { List result = findAll(sql, cla,paras); return result.size()> 0 ? result.get(0) : null; } private List find(Connection connection, String sql,Class cla,Object... paras) { try { PreparedStatement preparedStatement = connection.prepareStatement(sql); modelBuilder.fillStatement(preparedStatement,sql,paras); ResultSet resultSet = preparedStatement.executeQuery(); List modelList = modelBuilder.builderModelList(resultSet,cla); connection.close(); return modelList; } catch (SQLException e) { e.printStackTrace(); } return null; } public int update(String sql){ Connection connection = null; try { connection = jdbcTemplate.getDataSource().getConnection(); } catch (SQLException e) { e.printStackTrace(); } return update(connection, sql, NULL_PARA_ARRAY); } public int update(String sql,Object... paras){ Connection connection = null; try { connection = jdbcTemplate.getDataSource().getConnection(); } catch (SQLException e) { e.printStackTrace(); } return update(connection, sql, paras); } private int update(Connection connection,String sql,Object... paras){ PreparedStatement preparedStatement; try { preparedStatement = connection.prepareStatement(sql); modelBuilder.fillStatement(preparedStatement,sql,paras); return preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } return 0; } } public class ModelBuilder { private static final ModelBuilder modelBuilder = new ModelBuilder(); public static ModelBuilder me(){ return modelBuilder; } private ModelBuilder() { } public void fillStatement(PreparedStatement preparedStatement, String sql, Object... paras) throws SQLException{ for(int i=0;i List builderModelList(ResultSet resultSet,Class cla) throws SQLException{ List list = new ArrayList (); try { while (resultSet.next()) { T t = cla.newInstance(); Field[] fieldield = cla.getDeclaredFields(); for (Field field : fieldield) { try { field.setAccessible(true); Column column = field.getAnnotation(Column.class); if(StrKit.isNotEmpoty(column) && StrKit.isNotEmpoty(column.name())){ field.set(t, resultSet.getObject(column.name())); }else{ field.set(t, resultSet.getObject(field.getName())); } } catch (SQLException e) { throw new SQLException(field.getName()+" doesn't in this table"); } } list.add((T)t); } return list; } catch (Exception e) { e.printStackTrace(); } return null; } } public class UserInfo { private int id; private String name; @Column(name="password") private String sys_password; public Date getCreated_at() { return created_at; } public void setCreated_at(Date created_at) { this.created_at = created_at; } public BigDecimal getMoney() { return money; } public void setMoney(BigDecimal money) { this.money = money; } private Date created_at; private BigDecimal money; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return sys_password; } public void setPassword(String password) { this.sys_password = password; } } //使用方式如下 UserInfo user = baseJdbcTemplate.findOne("select * from user where id=1",UserInfo.class);