本片主要描述了基于osp平台的简单的增删改查的小案例。便于记忆。
以table表格的前后端拿数据为例子!
在table中展示数据
@Controller
@RequestMapping("/getMessage")
public class TestController {
@Autowired
private IDBAccess dbacess; // osp平台封装的数据库工具
// 方法一:拿取数据到前端表格
@RequestMapping(value = "/showList")
@ResponseBody
public JSONObject showList(Integer page,Integer limit){
if(page==null){
page = 1;
}
if(limit==null){
limit = 10;
}
JSONObject jsonObject = new JSONObject();
String[] cols = {"UUID","SEND_TIME","ISSUCCESSFUL","VERSIONNUM","SENDNOTES"};
String listSql = " SELECT UUID,SEND_TIME,ISSUCCESSFUL,VERSIONNUM,SENDNOTES FROM sg_his_zpy_testtable";
List<Object> list = dbacess.executeSqlQuery(listSql);
String result = DeviceUtil.getJsonResult(cols, list);
JSONArray rows = JSONArray.parseArray(result);
try {
jsonObject.put("code", 0);
jsonObject.put("msg", "");
jsonObject.put("data", rows);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
/* 作用:删除单条信息
* 参数:uuid
* 返回值:
* */
@RequestMapping("/deleteOne")
@ResponseBody
public JSONObject deleteData(@RequestParam String uuid){
JSONObject jsonObject = new JSONObject();
String deleteSql = "DELETE FROM SG_HIS_ZPY_TESTTABLE WHERE UUID = '"+uuid+"'";
int executeSqlUpdate = dbacess.executeSqlUpdate(deleteSql);
if(executeSqlUpdate>0){
jsonObject.put("result", true);
}else{
jsonObject.put("result", false);
}
return jsonObject;
}
/* 作用:
* 参数:uuid
* 返回值:
* */
@RequestMapping("deleteChecked")
@ResponseBody
public JSONObject deleteByUUID(String[] uuids){
JSONObject jsonObject = new JSONObject();
//String deleteSql = "DELETE FROM SG_HIS_ZPY_TESTTABLE WHERE UUID in (";
StringBuffer stringBuffer = new StringBuffer("DELETE FROM SG_HIS_ZPY_TESTTABLE WHERE UUID in (");
for (int i = 0; i < uuids.length; i++) {
if (i==uuids.length-1){
stringBuffer.append("\'"+uuids[i]+"\'"+")");
}else{
stringBuffer.append("\'"+uuids[i]+"\'"+",");
}
}
int executeSqlUpdate = dbacess.executeSqlUpdate(stringBuffer.toString());
if(executeSqlUpdate>0){
jsonObject.put("result",true);
}else{
jsonObject.put("result",false);
}
return jsonObject;
}
/* 作用:添加数据
* 参数:uuid,sendTime,issuccessful,versionnum,sendnates
* 返回值:
* */
@RequestMapping("/addData")
@ResponseBody
public JSONObject addData(String uuid,String sendTime,String issuccessful,String versionnum,String sendnates){
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = sendTime;
try {
ts = Timestamp.valueOf(tsStr);
} catch (Exception e) {
e.printStackTrace();
}
String insertSql = "insert into sg_his_zpy_testtable" +"(UUID,SEND_TIME,ISSUCCESSFUL,VERSIONNUM,SENDNOTES,OPERATIONIP)"
+"values(?,?,?,?,?,'192.168.1.1')";
JSONObject jsonObject = new JSONObject();
int i = dbacess.executeSqlUpdate(insertSql,new Object[]{uuid,ts,issuccessful,versionnum,sendnates});//executeSqlUpdate添加方法
if(i>0){
jsonObject.put("result", true);
}else{
jsonObject.put("result", false);
}
System.out.println(jsonObject);
return jsonObject;
}
/* 作用:修改数据
* 参数:uuid,sendTime,issuccessful,versionnum,sendnates
* 返回值:
* */
@RequestMapping("/editData")
@ResponseBody
public JSONObject editData(String uuid,String sendTime,String issuccessful,String versionnum,String sendnates){
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = sendTime;
try {
ts = Timestamp.valueOf(tsStr);
} catch (Exception e) {
e.printStackTrace();
}
String updateSql = "update sg_his_zpy_testtable set SEND_TIME = ?,ISSUCCESSFUL = ?,VERSIONNUM = ?,SENDNOTES = ? where uuid = ?";
JSONObject jsonObject = new JSONObject();
int executeSqlUpdate = dbacess.executeSqlUpdate(updateSql, new Object[]{ts,issuccessful,versionnum,sendnates,uuid});
if(executeSqlUpdate>0){
jsonObject.put("result", true);
}else{
jsonObject.put("result", false);
}
return jsonObject;
}
}