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

一、步骤

1、导包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

2、操作

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    //
    public void jdbcTest(){
        String sql = "select * from blog";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        System.out.println(maps);
    }
    //  插入
    @Test
    public void insertJDBC(){
        String sql = "insert into blog(id, title, author, views) values(20, 'ak', 'tt', 100)";
        jdbcTemplate.update(sql);
    }

    @Test
    // 修改
    public void updateJDBC(){
        String sql = "update blog set title = ?, author = ?, views =?  where >;
        Object[] objects = new Object[3];
        objects[0] = "wt";
        objects[1] = "wt";
        objects[2] = 100000;
        jdbcTemplate.update(sql, objects);
    }

    @Test
    // 删除
    public void deleteJDBC(){
        String sql = "delete from blog where id = ?";
        Object[] objects = new Object[1];
        objects[0] = 20;
        jdbcTemplate.update(sql, objects);
    }

 

相关文章:

  • 2021-11-28
  • 2021-07-09
  • 2021-05-31
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
猜你喜欢
  • 2021-08-16
  • 2021-06-14
  • 2022-01-13
  • 2021-09-08
  • 2021-09-16
  • 2022-01-17
相关资源
相似解决方案