JdbcTemplate的使用
前段时间由于工作原因,需要编写一个制造测试数据(MySQL)的工具类,网上搜了下,说JdbcTemplate不错,于是乎就准备使用下。为方便调用者无需了解JdbcTemplate,于是在此之上封装了一层,分别实现了增、删、查,且可批量操作,在封装批量新增方法时,费了点劲,最后用反射实现的。代码如下:
接口DataBaseDAO:

创新互联专注于中大型企业的成都网站制作、网站设计和网站改版、网站营销服务,追求商业策划与数据分析、创意艺术与技术开发的融合,累计客户超过千家,服务满意度达97%。帮助广大客户顺利对接上互联网浪潮,准确优选出符合自己需要的互联网运用,我们将一直专注成都品牌网站建设和互联网程序开发,在前进的路上,与客户一起成长!
package com.wb.hz.test.util;
import java.util.List;
public interface DataBaseDAO {
public void delete(String tableName,String fieldName,Object id);
public void delete(String tableName,String fieldName,List> ids);
public int statisticsById(String tableName,String fieldName,Object id);
public List> queryById(String tableName, String fieldName, Object id,boolean desc);
public List> queryByIds(String tableName,String fieldName,Object startid,Object endid,boolean desc);
public void insert(String sql,List list);
} 实现类DataBaseDAOImpl
package com.wb.hz.test.util.impl;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.wb.hz.test.util.DataBaseDAO;
public class DataBaseDAOImpl implements DataBaseDAO {
@Resource
private JdbcTemplate jdbcTemplate;
public void delete(String tableName, String fieldName, Object id) {
String sql="delete from "+tableName+" where "+fieldName+"=?";
jdbcTemplate.update(sql, id);
}
public void delete(String tableName, String fieldName, List> ids) {
String sql="delete from "+tableName+" where "+fieldName+"=?";
for(int i=0;i List 

咨询
建站咨询
