1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| @Test public void insertTest(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql = "insert into users values(null,?,?)"; int i = jdbcTemplate.update(sql, "Jackasher", 21); System.out.println(i);
} @Test public void updateTest(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql = "update users set name = ? where id = ?"; int i = jdbcTemplate.update(sql, "Flp", 2); System.out.println(i);
}
@Test public void deleteTest(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql = "delete from users where id = ?"; int i = jdbcTemplate.update(sql, 2); System.out.println(i);
}
@Test public void selectTest(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml"); JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); String sql = "select * from users where id = ?"; User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 1); System.out.println(user);
String sql2 = "select * from users;"; List<User> users = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<>(User.class)); users.forEach(user1 -> { System.out.println(user1); });
|