springboot中连接jdbc

LocaleResolver:国际化
在国际化时可以继承该接口

在springboot连接jdbc时:
不要加hikari

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/aa?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&userSSL=false
    driver-class-name: com.mysql.cj.jdbc.Driver

连接jdbc后再controller中进行crud,
注入JdbcTemplate jdbcTemplate;
jdbcTemplate.query(sql):实现查
jdbcTemplate.update(sql):实现增删改

@RestController
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @RequestMapping("/query")
    public List queryall(){
        System.out.println(jdbcTemplate+"--->");
        String sql="select * from books";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }
    @RequestMapping("/adduser")
    public String addUser(){
        String sql="insert into books values(5,'背景',12,'bad')";
        jdbcTemplate.update(sql);
        return "adduser-ok";
    }
    @RequestMapping("/updateUser/{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql="update books set bookname=? where id="+id;
        Object[] o = new Object[1];
        o[0]="数学";
        jdbcTemplate.update(sql,o);
        return "ipdateuser ok";

    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id){
        String sql="delete from books where id=?";
        jdbcTemplate.update(sql,id);
        return "delete-id";
    }
}

你可能感兴趣的:(spring,boot,java,mysql)