package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class JdbcController {
@Autowired //自动配置,因此可以直接通过@Autowired注入进来
private JdbcTemplate jdbcTemplate;
@GetMapping("/users/queryAll")
public List<Map<String,Object>> queryAll(){
List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from tb_user");
return list;
}
@GetMapping("/users/insert")
public Object insert(String name,String password){
jdbcTemplate.execute("insert into tb_user(`name`,`password`) value (\""+name+"\",\""+password+"\")");
return true;
}
}
使用JdbcTemplate操作数据库中的数据
最新推荐文章于 2023-07-15 22:13:40 发布