package com.example.first.hello;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DbController {
@RequestMapping("/users/demos")
public String data() {
String result = “\r\n” +
“<html lang=“zh-CN”>\r\n” +
“\r\n” +
“\r\n” +
" <meta charset=“utf-8”>\r\n" +
" <meta http-equiv=“x-ua-compatible” content=“ie=edge”>\r\n" +
"
" <meta name=“viewport” content=“width=device-width, initial-scale=1”>\r\n" +
“\r\n” +
“\r\n” +
“\r\n” +
" \r\n" +
" <tr style=“color:red”>\r\n" +
" \r\n" +
" \r\n" +
" \r\n" +
" \r\n" +
" \r\n" +
"
1 | 1 | 12 | 1 |
“\r\n” +
“\r\n” +
“\r\n” +
“”;
return result;
}
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/getUsers")
public List<Map<String, Object>> getDbType(){
String sql = "select * from users";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : list) {
Set<Entry<String, Object>> entries = map.entrySet( );
if(entries != null) {
Iterator<Entry<String, Object>> iterator = entries.iterator( );
while(iterator.hasNext( )) {
Entry<String, Object> entry =(Entry<String, Object>) iterator.next( );
Object key = entry.getKey( );
Object value = entry.getValue();
System.out.println(key+":"+value);
}
}
}
return list;
}
@RequestMapping("/user/{id}")
public Map<String,Object> getUser(@PathVariable String id){
Map<String,Object> map = null;
List<Map<String, Object>> list = getDbType();
for (Map<String, Object> dbmap : list) {
Set<String> set = dbmap.keySet();
for (String key : set) {
if(key.equals("id")){
if(dbmap.get(key).equals(id)){
map = dbmap;
}
}
}
}
if(map==null)
map = list.get(0);
return map;
}
@RequestMapping("/list")
public List<Map<String, Object>> itemsList() {
String sql = "select * from casetable";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
return list;
}
@RequestMapping("/add")
public @ResponseBody String addUser(User user) {
String sql = "insert into casetable values (?,?,?,?,?)";
Object args[] = {3,"金融科技","sxf",3,0};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章新增成功";
}
return "新增出现错误";
}
@RequestMapping("/del")
public @ResponseBody String delItems(User items) {
String sql = "delete from casetable where id = ?";
Object args[] = {1};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章删除成功";
}
return "删除出现错误";
}
@RequestMapping("/upd")
public @ResponseBody String updItems(User items) {
String sql = "update casetable set casename = ?,testbug = ? where id = ?";
Object args[] = {"edc",55,1};
int temp = jdbcTemplate.update(sql, args);
if(temp > 0) {
return "文章修改成功";
}
return "修改出现错误";
}
@RequestMapping("delete/user/{id}")
public Map<String,Object> deleteUser(@PathVariable String id){
Map<String,Object> map = null;
List<Map<String, Object>> list = getDbType();
for (Map<String, Object> dbmap : list) {
Set<String> set = dbmap.keySet();
for (String key : set) {
if(key.equals("id")){
if(dbmap.get(key).equals(id)){
map = dbmap;
}
}
}
}
if(map==null)
map = list.get(0);
return map;
}
}