application.yml配置 datasource: url: jdbc:mysql://localhost:3306/mydb?useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver pom.xml 依赖配置 <!--springboot整合jdbc框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--springboot整合mysql驱动类--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> jdbc service类 package com.mydemo.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserService { @Autowired private JdbcTemplate jdbcTemplate; @RequestMapping("/insertUser") public String insertUser(String name,Integer age){ int update = jdbcTemplate.update("INSERT into user(name,age) VALUES(?,?)",name,age); if(update>0){ return "sucess"; }else{ return "error"; } } } |
|