Spring SimpleJdbcOperations 批量更新

1.控制台代码

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.core.RowMapper;

import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import org.springframework.jdbc.core.simple.SimpleJdbcOperations;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import org.springframework.jdbc.support.GeneratedKeyHolder;

import org.springframework.dao.DataAccessException;

import java.sql.*;



public class MySQL {



    public static void main(String[] args) {

        org.logicalcobwebs.proxool.ProxoolDataSource ds = new org.logicalcobwebs.proxool.ProxoolDataSource();

        ds.setDriver("com.mysql.jdbc.Driver");

        ds.setDriverUrl("jdbc:mysql://192.168.1.49/test?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull");

        ds.setUser("test");

        ds.setPassword("123456");

        ds.setTestBeforeUse(true);

        ds.setTrace(true);

        ds.setHouseKeepingTestSql("select 1");



        String sql = "insert into Goods(id,good_name) values(:id,:good_name)";



        Goods obj = new Goods();

        obj.setId(7);

        obj.setGood_name("yes3");



        Goods obj2 = new Goods();

        obj2.setId(6);

        obj2.setGood_name("yes2");

        SimpleJdbcOperations m = new SimpleJdbcTemplate(ds);



        SqlParameterSource[] params = new SqlParameterSource[2];

        params[0] = new BeanPropertySqlParameterSource(obj);

        params[1] = new BeanPropertySqlParameterSource(obj2);

        m.batchUpdate(sql, params);



        // m.getNamedParameterJdbcOperations().update(sql,

        // new BeanPropertySqlParameterSource(obj));



        // Connection con= ds.getConnection();

        // Statement stmt=con.createStatement();

        // stmt.executeUpdate("insert into Goods(id,good_name) values(1,'niunai')");

        // con.close();



    }



}

2.实体类

import java.io.Serializable;





public class Goods implements Serializable {



    private static final long serialVersionUID = 5951566786306525049L;

   private int id;

   /**

 * @return the id

 */

public int getId() {

    return id;

}

/**

 * @param id the id to set

 */

public void setId(int id) {

    this.id = id;

}

/**

 * @return the good_name

 */

public String getGood_name() {

    return good_name;

}

/**

 * @param good_name the good_name to set

 */

public void setGood_name(String good_name) {

    this.good_name = good_name;

}

private String good_name;

}

3.数据库

create table Goods

(

  id int ,

  good_name varchar(20)

)

 4.需要的jar文件

org.springframework.jdbc-3.0.3.RELEASE.jar
proxool-cglib.jar
proxool-0.9.1.jar
commons-logging-1.1.1.jar
log4j-1.2.16.jar
org.springframework.context-3.0.3.RELEASE.jar
org.springframework.context.support-3.0.3.RELEASE.jar
org.springframework.beans-3.0.3.RELEASE.jar
org.springframework.aspects-3.0.3.RELEASE.jar
org.springframework.asm-3.0.3.RELEASE.jar
org.springframework.aop-3.0.3.RELEASE.jar
org.springframework.expression-3.0.3.RELEASE.jar
org.springframework.transaction-3.0.3.RELEASE.jar
mysql-connector-java-5.1.15-bin.jar

你可能感兴趣的:(spring)