祝各位道友念头通达
GitHub Gitee 语雀 打赏

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);

}

 

posted @ 2018-10-10 21:09  韩若明瞳  阅读(2285)  评论(0编辑  收藏  举报