学习了下springjdbc,感觉挺实用的,相对来说springjdbc 扩展性相当好了
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.jdbc.support.KeyHolder; import com.power.sql.UpdateSql; import com.power.sql.UpdateSql.SqlType; import com.power.utils.ArrayAssistant; import com.power.utils.FieldAssistant; import com.power.utils.TransformUtils; /** * @author Gary Huang * 博客地址 ttp://blog.csdn.net/hfmbook/ * * */ public class BaseDao<T >{ @Autowired private JdbcTemplate jdbcTemplate ; private UpdateSql updateSql ; public Integer insert(T t){ updateSql = new UpdateSql(SqlType.INSERT , t) ; 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 ; } }
UpdateSql 类在:http://blog.csdn.net/hfmbook/article/details/41290641转载请表名出处:http://blog.csdn.net/hfmbook
原文地址:http://blog.csdn.net/hfmbook/article/details/41308049