springboot整合JDBC [数据库教程]

database

前提:配置数据库连接(见前面)

一、步骤

1、导包

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

2、操作

    @Autowired

private JdbcTemplate jdbcTemplate;

@Test

//

publicvoid jdbcTest(){

String sql = "select * from blog";

List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

System.out.println(maps);

}

// 插入

@Test

publicvoid insertJDBC(){

String sql = "insert into blog(id, title, author, views) values(20, ‘ak‘, ‘tt‘, 100)";

jdbcTemplate.update(sql);

}

@Test

// 修改

publicvoid updateJDBC(){

String sql = "update blog set title = ?, author = ?, views =? where id=" + 20;

Object[] objects = new Object[3];

objects[0] = "wt";

objects[1] = "wt";

objects[2] = 100000;

jdbcTemplate.update(sql, objects);

}

@Test

// 删除

publicvoid deleteJDBC(){

String sql = "delete from blog where id = ?";

Object[] objects = new Object[1];

objects[0] = 20;

jdbcTemplate.update(sql, objects);

}

 

springboot 整合JDBC

以上是 springboot整合JDBC [数据库教程] 的全部内容, 来源链接: utcz.com/z/534847.html

回到顶部