JdbcTemplate
导入依赖
简单使用
查询-RowMapper返回自定义对象
public static void test06() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
// 查询数据的SQL语句
String sql = "SELECT * FROM product;";
List<Product> query = jdbcTemplate.query(sql, new RowMapper<Product>() {
@Override
public Product mapRow(ResultSet arg0, int arg1) throws SQLException {
Product p = new Product();
p.setPid(arg0.getInt("pid"));
p.setPname(arg0.getString("pname"));
p.setPrice(arg0.getDouble("price"));
return p;
}
});
for (Product product : query) {
System.out.println(product);
}
}
查询-BeanPropertyRowMapper返回自定义对象
// query使用BeanPropertyRowMapper做映射返回对象
public static void test07() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
// 查询数据的SQL语句
String sql = "SELECT * FROM product;";
List<Product> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Product.class));
for (Product product : list) {
System.out.println(product);
}
}
查询-queryForList返回一个List集合
public static void test05() throws Exception {
String sql = "SELECT * FROM product WHERE pid<?;";
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, 8);
for (Map<String, Object> map : list) {
System.out.println(map);
}
}
查询-queryForMap返回一个Map集合
public static void test04() throws Exception {
String sql = "SELECT * FROM product WHERE pid=?;";
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
Map<String, Object> map = jdbcTemplate.queryForMap(sql, 6);
System.out.println(map);
}
查询-queryForObject返回String
public static void test03() throws Exception {
String sql = "SELECT pname FROM product WHERE price=7777;";
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String str = jdbcTemplate.queryForObject(sql, String.class);
System.out.println(str);
}
查询-queryForInt返回一个int整数
查询-queryForLong返回一个long整数
实现增删改
public class Demo05 {
public static void main(String[] args) throws Exception {
// test01();
// test02();
// test03();
}
// JDBCTemplate添加数据
public static void test01() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String sql = "INSERT INTO product VALUES (NULL, ?, ?);";
jdbcTemplate.update(sql, "iPhone3GS", 3333);
}
// JDBCTemplate修改数据
public static void test02() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String sql = "UPDATE product SET pname=?, price=? WHERE pid=?;";
int i = jdbcTemplate.update(sql, "XVIII", 18888, 10);
System.out.println("影响的行数: " + i);
}
// JDBCTemplate删除数据
public static void test03() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String sql = "DELETE FROM product WHERE pid=?;";
int i = jdbcTemplate.update(sql, 7);
System.out.println("影响的行数: " + i);
}
}
= “DELETE FROM product WHERE pid=?;”;
int i = jdbcTemplate.update(sql, 7);
System.out.println("影响的行数: " + i);
}
}