zoukankan      html  css  js  c++  java
  • jdbcTemplate insert 封装

    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);  
                                    
        }
    }
  • 相关阅读:
    CSS3实战之新增的选择器
    前端攻城狮学习笔记八:自己实现string的substring方法,人民币小写转大写,数字反转,正则优化
    负值之美:负margin在页面布局中的应用
    关于vs2015无法启动iis服务
    ado.net中的 sqlconnection sqlcommand datareader dataset SqlDataAdapter之间的关系
    C# Lambda表达式详细总结
    Response.cookies和Request.cookies
    Quartz CronTrigger最完整配置说明 陀螺
    浅析如何去除Strings中的C#空格 陀螺
    C#Winform中WebBrowser控件的特性和详细调用方法 陀螺
  • 原文地址:https://www.cnblogs.com/JAYIT/p/12522642.html
Copyright © 2011-2022 走看看