spring Jdbc自己主动获取主键。

学习了下springjdbc,感觉挺有用的,相对来说springjdbc 扩展性相当好了




> c = getClass(); Type type = c.getGenericSuperclass(); if (type instanceof ParameterizedType) { Type[] parameterizedType = ((ParameterizedType) type) .getActualTypeArguments(); this.entityClass = (Class<T>) parameterizedType[0]; } } @Autowired private JdbcTemplate jdbcTemplate ; private UpdateSql updateSql ; public Integer insert(T t){ updateSql = new UpdateSql(SqlType.INSERT , t) ; if(null == updateSql.getIdName()){ return jdbcTemplate.update( updateSql.getSqlBuffer() , ArrayAssistant.asArray(updateSql.getParam()) ) ; } KeyHolder holder = new GeneratedKeyHolder() ; jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = conn.prepareStatement( updateSql.getSqlBuffer() , new String[] { updateSql.getIdName() } ) ; List<Object> param = updateSql.getParam() ; int size = param.size() ; for(int x=1;x<=size;x++){ ps.setObject(x, param.get(x-1)); } return ps ; } } , holder ) ; int id = TransformUtils.toInt(holder.getKey()) ; FieldAssistant.writeField(updateSql.getIdName(), t , id, true); return TransformUtils.toInt(holder.getKey()) ; } public int update(T t){ updateSql = new UpdateSql(SqlType.UPDATE , t) ; int result = jdbcTemplate.update(updateSql.getSqlBuffer() , ArrayAssistant.asArray(updateSql.getParam())) ; return result ; } public int delete(T t){ updateSql = new UpdateSql(SqlType.DELETE , t) ; int result = jdbcTemplate.update(updateSql.getSqlBuffer() , ArrayAssistant.asArray(updateSql.getParam())) ; return result ; } public int delete(Integer id){ updateSql = new UpdateSql( entityClass ) ; int result = jdbcTemplate.update(updateSql.getSqlBuffer() , id ) ; return result ; } }







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5153239.html,如需转载请自行联系原作者
上一篇:简谈规则引擎在生产调度中的作用


下一篇:PostgreSQL字符串截取替换