查询数据库返回列表(json)
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from mldn");
return list.get(6);
查询数据库返回一个list(相当于上面的list>get(1,2,3,…))
@Controller
public class lianjie {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/jdbc")
public List l() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from jingxiao");
return list;
}
}
查询数据库返回字符串/整形。。。
List<Map<String, Object>> list = jdbcTemplate.queryForList("select password1 from user where username1="+name);
String s=jdbcTemplate.queryForObject("select password1 from user where username1="+name,String.class);
增删改查
见
https://blog.csdn.net/weixin_40001125/article/details/88538576
insert实例
迷惑:不知道需要配置application.pripertiers吗,懒得试了
应该application.yml可以代替
package aaa.j2ee2.controller;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Controller
public class SwitchProduct {
@Autowired // 自动注入,spring boot会帮我们实例化一个对象
private JdbcTemplate jdbcTempl;
@RequestMapping(value = "/updata")
@ResponseBody
public Object update(HttpServletRequest request){
// String name=request.getParameter("username");
// int id=Integer.parseInt(request.getParameter("userId"));
String id=request.getParameter("userId");
System.out.println(id);
Map<String,Object> map= new HashMap<>();
// map.put("success",true);
// map.put("message","更新成功");
// System.out.println(name);
String sql = "insert into j2ee2(temperature) values("+id+")";
jdbcTempl.execute(sql);
return map;
}
}
返回字符串实例
package aa.mysql428.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class lianjie1 {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/lianjie1")
//public Map<String, Object> map(@RequestParam("username") String name , @RequestParam("userpassword") String password) {
public String map(@RequestParam("username") String name , @RequestParam("userpassword") String password) {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select password1 from user where username1="+name);
String s=jdbcTemplate.queryForObject("select password1 from user where username1="+name,String.class);
//修改数据库
return s;
}
}