<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
@RestController
public class JDBCController {
@Autowired
JdbcTemplate jdbcTemplate;
//查数据库的所有信息
//没有实体类,数据库中的字段如何获取 万能的Map
@GetMapping("/stuList")
public List<Map<String,Object>> stuList(){
String sql = "select * from stu";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
return maps;
}
@GetMapping("/stuList/add")
public String addStu(){
String sql = "insert into stu(id,name,gender,age,birthday,score) values(4,'马王八','男',18,'2021-11-11',80)";
jdbcTemplate.update(sql);
return "update-ok";
}
@GetMapping("/stuList/{id}")
public String deletStu(@PathVariable("id") int id){
String sql = "update stu set id=?,name=?,gender=?,age=?,birthday=?,score=? where id="+id;
jdbcTemplate.update(sql);
return "update-ok";
}
@GetMapping("/stuList/{id}")
public String deleteStu(@PathVariable("id") int id){
String sql = "delete from stu where id=?";
jdbcTemplate.update(sql,id);
return "delete-ok";
}
}