springboot 整合JDBC

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

一、步骤

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 id=" + 20;
        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);
    }

 

springboot 整合JDBC

上一篇:dbgen工具使用方法


下一篇:C指针和数组的关系详解