CRM ProjectTest
一、基础模块
1、返回状态类
public class ResultInfo {
private Integer code=200; //code==200 没有异常
private String msg="success";
private Object result;
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2、全局异常处理:
@Component
public class ExceptionResolver implements HandlerExceptionResolver {
/**
* @param request request 对象
* @param response response 对象
* @param handler 请求方法对象
* @param ex :异常对象
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
/**
* 非法请求拦截
* 判断是否抛出未登陆异常
* 如果抛出未登陆异常,重定向跳转到登陆页面
*/
if (ex instanceof NoLoginException) {
//重定向跳转到登陆页面
ModelAndView mv = new ModelAndView("redirect:/index");
return mv;
}
//默认异常处理 (返回视图)
ModelAndView modelAndView = new ModelAndView("error");
//设置异常信息
modelAndView.addObject("code", 500);
modelAndView.addObject("msg", "异常错误,请重试...");
if (handler instanceof HandlerMethod) {
//类型转换
HandlerMethod handlerMethod = (HandlerMethod) handler;
//获取方法上声明的responseBody注解
ResponseBody responseBody = handlerMethod.getMethod().getDeclaredAnnotation(ResponseBody.class);
//判断responseBody对象是否为空 如果为空表示返回视图,否则是json数据
if (responseBody == null) {
//返回视图
if (ex instanceof ParamsException) {
ParamsException p = (ParamsException) ex;
modelAndView.addObject("code", p.getCode());
modelAndView.addObject("msg", p.getMsg());
}
return modelAndView;
} else {
//返回json数据
//默认异常处理
ResultInfo resultInfo = new ResultInfo();
resultInfo.setCode(500);
resultInfo.setMsg("异常错误!请重试");
//判断异常类型是否是自定义异常
if (ex instanceof ParamsException) {
ParamsException p = (ParamsException) ex;
resultInfo.setCode(p.getCode());
resultInfo.setMsg(p.getMsg());
}
//设置响应类型 及编码格式(json)
response.setContentType("application/json;charSet=UTF-8");
PrintWriter out =null;
try {
//输出流
out = response.getWriter();
//将返回的对象转成Json格式
String json = JSON.toJSONString(resultInfo);
//返回数据
out.write(json);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(out!=null){
out.close();
}
}
return null;
}
}
return null;
}
}
3、非法请求拦截
1.创建拦截器类
public class NologinInterceptor extends HandlerInterceptorAdapter {
@Resource
private UserMapper userMapper;
/**
* 拦截用户是否登录
* 在目标方法执行前、执行
*
* @param request
* @param response
* @param handler
* @return true目标方法可执行,false目标方法不可执行
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//从cookie中获取用户id
Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
if(userId==null|| userMapper.selectByPrimaryKey(userId)==null){
//抛出未登陆异常
throw new NoLoginException();
}
return true;
}
}
2.创建拦截器配置类
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Bean //将返回值交给IOC维护
public NologinInterceptor nologinInterceptor(){
return new NologinInterceptor();
}
//添加拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(nologinInterceptor())
.addPathPatterns("/**") //拦截路径 默认拦截所有
.excludePathPatterns("/css/**","/js/**","/lib/**","/image/**","/index","/user/login") ; //放行的路径
}
}
3.在全局异常类里处理非登陆异常
/**
* 非法请求拦截
* 判断是否抛出未登陆异常
* 如果抛出未登陆异常,重定向跳转到登陆页面
*/
if (ex instanceof NoLoginException) {
//重定向跳转到登陆页面
ModelAndView mv = new ModelAndView("redirect:/index");
return mv;
}
4、 记住我功能
//判断记住密码复选框是否选中(如果选中设置cookie7天时效)
if ($("#rememberMe").prop("checked")) {
$.cookie('userId', result.result.userId, {expires: 7});
$.cookie('userName', result.result.userName, {expires: 7});
$.cookie('trueName', result.result.trueName, {expires: 7});
}
二、营销管理模块
1.日期数据格式化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date date; //实体类字段
分页方法:
public Map<String, Object> querySaleChanceByparams(SaleChanceQuery saleChanceQuery) {
Map<String, Object> map = new HashMap<>();
//开启分页
PageHelper.startPage(saleChanceQuery.getPage(),saleChanceQuery.getLimit());
//创建分页对象
PageInfo<SaleChance> pageInfo =new PageInfo<>(saleChanceMapper.selectByParams(saleChanceQuery));
//设置map对象
map.put("code",0);
map.put("msg","success");
map.put("count",pageInfo.getTotal()); //总条数
map.put("data",pageInfo.getList()); //查询结果集合
return map;
}
三、用户角色模块
1.insert sql语句 返回主键
<!--添加操作默认返回受影响行数
可以返回主键
keyProperty 实体类中字段名
keyColumn 数据库中字段名
-->
<insert id="insertSelective" parameterType="com.xxxx.crm.vo.User" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
</insert>
定时任务:
1.创建定时任务类 交给容器管理
@Component
public class JobTask {
@Resource
private CustomerService customerService;
/**
* 两秒执行一次
* @Scheduled(cron = "0/秒 分 时 月 年 ?")
*/
//@Scheduled(cron = "0/2 * * * * ?")
public void job() {
System.out.println("定时任务开始执行————>"
+ new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(new Date()));
customerService.updateCustomerState();
}
}
2.在启动类上加上注解
@SpringBootApplication
@MapperScan("com.xxxx.crm")
@EnableScheduling //启用定时任务
public class StartClass {
public static void main(String[] args) {
SpringApplication.run(StartClass.class);
}
}