springboot实现增删改查

package com.example.first.hello;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class DbController {
//@RequestMapping(method=RequestMethod.GET)
public String sayHello() {
return “index”;
}
@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" +
" HTML模板文件\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” +
“\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(value="/list",method=RequestMethod.POST)
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[] = {37,"金","sxfhhhh",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[] = {2};  
	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;
}

@RequestMapping(value = "/getInvoice",method = RequestMethod.POST)
public String getInvoice(@RequestBody String data)
{  BufferedInputStream bin = null;
  FileOutputStream fout = null;
  BufferedOutputStream bout = null;	
  try {
    //将base64编码的字符串解码成字节数组
    byte[] bytes = Base64.decodeBase64(data);
    //创建一个将bytes作为其缓冲区的ByteArrayInputStream对象
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    //创建从底层输入流中读取数据的缓冲输入流对象
    bin = new BufferedInputStream(bais);
    File file = new File("C:\\Users\\20433\\Desktop\\shixinfa.pdf");
    //创建到指定文件的输出流
    fout  = new FileOutputStream(file);
    //为文件输出流对接缓冲输出流对象
    bout = new BufferedOutputStream(fout);
    byte[] buffers = new byte[1024];
    int len = bin.read(buffers);
    while(len != -1){
      bout.write(buffers, 0, len);
      len = bin.read(buffers);
    }
    //刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
    bout.flush();
  } catch (IOException e) {
    e.printStackTrace();
  }finally{
    try {
      bin.close();
      fout.close();
      bout.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  return "good";
}

}

猜你喜欢

转载自blog.csdn.net/weixin_37565521/article/details/87933659