💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
增删改用的是同一个方法`update`,只是需要改变 SQL 语句即可。下面以增为例,介绍几个常用的`update`方法。 <br/> **1. `public int update(String sql, @Nullable Object... args)`** ```java String sql = "insert into student(name, age, gender) values(?,?,?)"; Object[] data = new Object[]{"李四", 25, "男"}; int rows = jdbcTemplate.update(sql, data); ``` **2.`public int update(String sql, @Nullable PreparedStatementSetter pss)`** ```java String sql = "insert into student(name, age, gender) values(?,?,?)"; int rows = jdbcTemplate.update(sql, new PreparedStatementSetter() { @Override public void setValues(PreparedStatement ps) throws SQLException { ps.setString(1, "张三"); ps.setInt(2, 25); ps.setString(3, "男"); } }); ``` **3. 批量操作:`public int[] batchUpdate(String sql, List<Object[]> batchArgs)`** ```java String sql = "insert into student(name, age, gender) values(?,?,?)"; List<Object[]> dataList = new ArrayList<>(); dataList.add(new Object[]{"张三", 25, "男"}); dataList.add(new Object[]{"李四", 30, "男"}); int[] rows = jdbcTemplate.batchUpdate(sql, dataList); ``` **4. 批量操作:`public int[] batchUpdate(String sql, BatchPreparedStatementSetter pss)`** ```java String sql = "insert into student(name, age, gender) values(?,?,?)"; List<Object[]> dataList = new ArrayList<>(); dataList.add(new Object[]{"张三", 25, "男"}); dataList.add(new Object[]{"李四", 30, "男"}); int[] rows = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setString(1, (String) dataList.get(i)[0]); ps.setInt(2, (Integer) dataList.get(i)[1]); ps.setString(3, (String) dataList.get(i)[2]); } @Override public int getBatchSize() { return dataList.size(); } }); ```