spring的JdbcTemplate配置连接数据库
XML文件的配置:
需要导入jar包:
Java代码
测试代码:
public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("spring5.xml"); JdbcTemplate jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate"); RowMapper<User> mapper = new BeanPropertyRowMapper<>(User.class); //查询单个记录 User user1 = jdbcTemplate.queryForObject("select * from user where id=?", mapper,1); System.out.println(user1); //查询多行记录 List<User> list = jdbcTemplate.query("select * from user", mapper); for (User user : list) { System.out.println(user); } //增删改 int row = jdbcTemplate.update("update user set role=? where name=?",1,"ykq"); System.out.println(row); //查询结果为一列值 int count = jdbcTemplate.queryForObject("select count(*) from user",Integer.class); System.out.println(count); }
本文来自博客园踩坑狭,作者:韩若明瞳,转载请注明原文链接:https://www.cnblogs.com/han-guang-xue/p/9769077.html