UAP 从前端到后端 详细教程 (五) 后端
程序员文章站
2022-04-28 09:54:17
...
首先配置问题,在配置文件里 添加包的扫描
把对应的注解了的类,解析为相应组件:
配置文件 命名规则为 xxxResource.xml ,我这里叫 scanResource.xml.
包扫描,是扫描你的 controller 等类所在包位置
scanResource.xml 里的 代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:module="http://www.sgcc.com.cn/schema/platform/module"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.sgcc.com.cn/schema/platform/module
http://www.sgcc.com.cn/schema/platform/module/module.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="top.jacktu.controller" />
<context:component-scan base-package="top.jacktu.bizc" />
</beans>
然后是代码结构,视图. 我这里没怎么用到 Emp 所以 有 Emp 字样的 java类 你可以忽略它
bizc 是 业务逻辑层,controller 是对请求的处理方法. po 是放 javaBean 的.
===========================================================================================
================================== 分割线===================================================
实体类,Holiday.java
package top.jacktu.po;
import java.io.Serializable;
/** Holiday 实体类(JavaBean) */
public class Holiday implements Serializable {
private static final long serialVersionUID = -2861040546555145433L;
private int ID;
private int USERID;
private String REQUEST_DATE;
private String REQUEST_REASON;
private int REQUEST_DAYS;
private int APPROVER_UID;
private String STATE;
// .... 后面构造方法,toStrig,getter ,setter 什么的省略了...
}
HolidayControllr.java 控制器
package top.jacktu.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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.RequestParam;
import top.jacktu.bizc.IHolidayBizc;
import top.jacktu.po.Holiday;
import com.sgcc.uap.rest.annotation.ItemResponseBody;
import com.sgcc.uap.rest.annotation.QueryRequestParam;
import com.sgcc.uap.rest.annotation.VoidResponseBody;
import com.sgcc.uap.rest.support.QueryResultObject;
import com.sgcc.uap.rest.support.RequestCondition;
import com.sgcc.uap.rest.utils.RestUtils;
@Controller
@RequestMapping("/holiday")
public class HolidayController {
@Autowired
IHolidayBizc holidayBizc;
/**
* 向前端返回 列表 有关数据
* 其中 PageIndex 和 PageSize 前端会自动反馈回来
* 这样接收,反馈对应 页面的 数据就可以了 前端 (就是做一个分页查询)
* 每次 翻页 都会 向这里请求一次
*/
@RequestMapping("/list")
public @ItemResponseBody
Object getHolidayList(HttpServletRequest reqeust,
@QueryRequestParam("params") RequestCondition param) {
System.out.println(param.getPageIndex());
System.out.println(param.getPageSize());
System.out.println(param.getColumns());
return holidayBizc.getHolidayList();
}
/** 前端加载元数据的请求地址,我这里是空实现 和返回 */
@RequestMapping("/list/meta")
public @ItemResponseBody
Object getHolidayListMeta(HttpServletRequest reqeust) {
return "";
}
// {"ids":[2,3,4]} id 传递过来的形式
// 通过这个删除数据库 里的一些数据,实现操作
@RequestMapping("/list/delete")
public @VoidResponseBody
Object delHoliday(HttpServletRequest reqeust,
@RequestBody Map<String, List<String>> values) {
holidayBizc.DeleteHolidayByIds(values.get("ids"));
return null;
}
/** 新建一条记录 时的预填数据,也就是弹出 新建 表单时的 ,数据返回 */
@RequestMapping("/info/new")
public @ItemResponseBody
QueryResultObject newHoliday(HttpServletRequest reqeust,
@ModelAttribute Holiday holiday) {
Map<String, Object> map = holidayBizc.getNewInfo();
// return RestUtils.wrappQueryResult(map);
return RestUtils.wrappQueryResult(map);
}
/** 插入一条记录 */
@RequestMapping("/new")
// ModelAttribute
public @VoidResponseBody
Object insertNewInfo(HttpServletRequest request,
@ModelAttribute Holiday holiday) {
holidayBizc.insertOneHoliday(holiday);
return null;
}
/** 点击编辑时的事件,弹出表单请求数据 时 ,数据返回 */
@RequestMapping("/info/{id}")
public @ItemResponseBody
QueryResultObject getHolidayInfo(HttpServletRequest reqeust,
@PathVariable Integer id, @RequestParam("params") String param) {
Map<String, Object> map = holidayBizc.getInfoById(id);
return RestUtils.wrappQueryResult(map);
}
/** 保存对 假期的修改 */
@RequestMapping("/saveEdit")
// ModelAttribute
public @VoidResponseBody
Object saveEdit(HttpServletRequest request, @ModelAttribute Holiday holiday) {
holidayBizc.saveEdit(holiday);
return null;
}
}
HolidayBizc.java
package top.jacktu.bizc;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.jacktu.po.Holiday;
import com.sgcc.uap.persistence.IHibernateDao;
import com.sgcc.uap.rest.support.QueryResultObject;
import com.sgcc.uap.rest.utils.RestUtils;
/**
* 这个时 业务层(逻辑实现),又可以直接调用一些API 但是我没用.
* 作为演示,很多sql 语句都是自己编写的.
* @author jacktu
*
*/
@Service
public class HolidayBizc implements IHolidayBizc{
@Autowired
IHibernateDao hibernateDao;
// 获取表单数据,这里要做一个 分页查询.
//我这里没有写,需要自己 根据 pageIndex 与 pageSize实现
@Override
public QueryResultObject getHolidayList() {
String sql = "select " +
"ID,"+
"USERID,"+
"TO_CHAR(REQUEST_DATE,'YYYY-MM-DD') REQUEST_DATE,"+
"REQUEST_REASON,"+
"REQUEST_DAYS,"+
"APPROVER_UID,"+
"STATE"+
" from holiday";
@SuppressWarnings("unchecked")
List<Map<String, Object>> list =
hibernateDao.queryForListWithSql(sql);
/** list后面那个10 是分页查询的一个重要信息.前端会根据这个显示有多少页*/
return RestUtils.wrappQueryResult(list,10);
}
@Override
public void DeleteHolidayByIds(List<String> ids) {
String sql = "DELETE FROM HOLIDAY WHERE ID IN (?)";
sql = sql.replace("?",ids.toString().replace("[", "").replace("]", ""));
hibernateDao.updateWithSql(sql);
}
// 保存编辑后的数据 时,逻辑实现
@Override
public void saveEdit(Holiday holiday) {
String sql = "UPDATE holiday SET " +
"USERID=?"+
",REQUEST_DATE=TO_DATE(?,'YYYY-MM-DD')"+
",REQUEST_REASON=?"+
",REQUEST_DAYS=?"+
",APPROVER_UID=?"+
",STATE=?"+
" WHERE id=?";
Object data [] = {
holiday.getUSERID(),
holiday.getREQUEST_DATE().replace("00:00:00", ""),
holiday.getREQUEST_REASON(),
holiday.getREQUEST_DAYS(),
holiday.getAPPROVER_UID(),
holiday.getSTATE(),
holiday.getID()};
try {
System.out.println("开始执行sql");
hibernateDao.updateWithSql(sql,data);
System.out.println("执行完毕,over");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// 编辑时 的数据返回
@Override
public Map<String, Object> getInfoById(Integer id) {
String sql = "SELECT * FROM holiday WHERE ID=?";
@SuppressWarnings("unchecked")
List<Map<String, Object>> list =
hibernateDao.queryForListWithSql(sql,new Object[]{id});
return list.get(0);
}
/** 插入一条数据的 逻辑实现 */
@Override
public void insertOneHoliday(Holiday h) {
System.out.println("we do nothing but we pretend to do so much thing.");
String sql = "insert into holiday values(?,?,TO_DATE('"
+h.getREQUEST_DATE().substring(0, 10)+"','YYYY-MM-DD'),?,?,?,?)";
Object [] aobj = new Object[]{
h.getID(),h.getUSERID(),h.getREQUEST_REASON(),h.getREQUEST_DAYS()
,h.getAPPROVER_UID(),h.getSTATE()
};
hibernateDao.updateWithSql(sql, aobj);
}
// 新建时反馈的信息
@Override
public Map<String, Object> getNewInfo() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("ID", getMaxID("holiday")+1);
map.put("USERID", "2");
map.put("REQUEST_DATE", new Date());
map.put("REQUEST_REASON", "例:身体不舒服,想去医院看看.");
map.put("REQUEST_DAYS", "3");
map.put("APPROVER_UID", "1");
map.put("STATE", "待批准");
return map;
}
/** 获取最大 的 ID,实现自增. 但是我们组不是这样的,这里只是示范 */
private int getMaxID(String string) {
String sql = "select max(ID) from holiday";
int maxInt = hibernateDao.queryForIntWithSql(sql);
return hibernateDao.queryForIntWithSql(sql);
}
}
基本就这些,代码粘过去哪里不能实现.自行解决吧,应该也不会有什么大问题.