首页 > 数据库技术 > 详细

jdbcTemplate insert 封装

时间:2020-03-19 11:18:45      阅读:69      评论:0      收藏:0      [点我收藏+]
public <T> void insert(String sql, List<T> objlist) {
        final List<T> list=objlist;
        BatchPreparedStatementSetter setter=new BatchPreparedStatementSetter(){  
                                        
                public int getBatchSize(){  
                    return list.size();  
                }  
                public void setValues(PreparedStatement ps,int index) throws SQLException{  
                    T t=list.get(index);  
                    Field fields[]=t.getClass().getDeclaredFields();
                    try {
                        for(int i=0;i<fields.length;i++){
                            PropertyDescriptor  prop=new PropertyDescriptor(fields[i].getName(),t.getClass());
                            Method getmethod=prop.getReadMethod();
                            if(fields[i].getType().getCanonicalName().equalsIgnoreCase("java.lang.String")){
                                ps.setString(i+1, String.valueOf(getmethod.invoke(t)));
                                //System.out.println(ps.getResultSet().getString(i+1));
                            }
                            else if(fields[i].getType().getCanonicalName().equalsIgnoreCase("int")){
                                ps.setInt(i+1, (Integer)getmethod.invoke(t));
                                //System.out.println(ps.getResultSet().getInt(i+1));
                            }
                            else if(fields[i].getType().getCanonicalName().equalsIgnoreCase("long")){
                                ps.setLong(i+1, (Long) getmethod.invoke(t));
                                //System.out.println(ps.getResultSet().getLong(i+1));
                            }
                            else if(fields[i].getType().getCanonicalName().equalsIgnoreCase("double")){
                                ps.setDouble(i+1, (Double) getmethod.invoke(t));
                                //System.out.println(ps.getResultSet().getDouble(i+1));
                            }
                            else if(fields[i].getType().getCanonicalName().equalsIgnoreCase("java.lang.Integer")){
                                ps.setInt(i+1, (Integer)getmethod.invoke(t));
                                //System.out.println(ps.getResultSet().getInt(i+1));
                            }
                            else if(fields[i].getType().getCanonicalName().equalsIgnoreCase("java.lang.Long")){
                                ps.setLong(i+1, (Long) getmethod.invoke(t));
                                //System.out.println(ps.getResultSet().getLong(i+1));
                            }
                            else if(fields[i].getType().getCanonicalName().equalsIgnoreCase("java.lang.Double")){
                                ps.setDouble(i+1, (Double) getmethod.invoke(t));
                                //System.out.println(ps.getResultSet().getDouble(i+1));
                            }
                            else if(fields[i].getType().getCanonicalName().equalsIgnoreCase("java.util.Date")){
                                ps.setDate(i+1,   new java.sql.Date(((Date)getmethod.invoke(t)).getTime()));
                                //System.out.println(ps.getResultSet().getDate(i+1));
                            }
                        }
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IntrospectionException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } 
            };  
            jdbcTemplate.batchUpdate(sql,setter);  
                                
    }
}

jdbcTemplate insert 封装

原文:https://www.cnblogs.com/JAYIT/p/12522642.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!