MVC-Service示例
Bitgeek 2022-08-15 springspringmvc请求
# 代码包结构
cn.bitgeek.springboot.jdbc
├── rest // rest包专门放置Controller接口
├──── ClassController.java
├── service // service包专门放置Service服务类
├──── ClassService.java
├── dto // dto包放传输对象
├──── ClassDTO.java
├── entity // entity包放实体类对象
├──── ClassEntity.java
├── mapper // mapper包专门放置和数据库交互的接口类
├──── ClassMapper.java
├──── ClassMapper.xml
└── JdbcApplication.java // 服务启动类入口
# ClassService代码示例
package cn.bitgeek.springboot.jdbc.service;
import cn.bitgeek.springboot.jdbc.dto.ClassDTO;
import cn.bitgeek.springboot.jdbc.entity.Class1;
import cn.bitgeek.springboot.jdbc.mapper.ClassMapper;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Slf4j
@Service
public class ClassService {
@Autowired
private ClassMapper classMapper;
/**
* <p>新增课程</p>
* @author bitgeek
* @date 2022/6/26 15:09
* @exception
* @return
* @param
*
*/
public HashMap<String, Object> insert(ClassDTO dto) {
// 推荐使用 copyProperties 方式来实现
HashMap<String, Object> map = new HashMap<>();
dto.setStatus(1);
dto.setHighlights(StringUtils.strip(dto.getHighlightss().toString(),"[]"));
if (dto.getDescriptionStr().equals("2")){
dto.setDescription(2);
}else if (dto.getDescriptionStr().equals("1")){
dto.setDescription(1);
}
Class1 class1 =new Class1();
BeanUtils.copyProperties(dto,class1);
Integer count = classMapper.insert(class1);
try {
if(count>0){
//插入成功
map.put("success",true);
map.put("data",count);
map.put("msg","添加成功");
}else{
//一条都没有插入
map.put("success",false);
map.put("data",count);
map.put("msg","添加了0条数据");
}
} catch (Exception e) {
map.put("msg","添加失败"+e.getMessage());
map.put("success",false);
log.error("出现异常,参数是", JSONObject.toJSONString(dto));
log.error("出现异常",e);
}
return map;
}
/**
* <p>删除</p>
* @author bitgeek
* @date 2022/6/26 15:09
* @exception
* @return
* @param
*
*/
public HashMap<String, Object> delete(int id) {
HashMap<String, Object> map = new HashMap<>();
map.put("success",false);
try {
Integer count = classMapper.delete(id);
map.put("success",true);
map.put("data",count);
map.put("msg","删除成功");
} catch (Exception e) {
log.error("出现异常,参数是", id);
log.error("出现异常",e);
}
return map;
}
/**
* <p>更新</p>
* @author bitgeek
* @date 2022/6/26 15:09
* @exception
* @return
* @param
*
*/
public HashMap<String, Object> update(ClassDTO dto) {
HashMap<String, Object> map = new HashMap<>();
map.put("success",false);
try {
dto.setHighlights(StringUtils.strip(dto.getHighlightss().toString(),"[]"));
if (dto.getDescriptionStr().equals("2")){
dto.setDescription(2);
}else {
dto.setDescription(1);
}
Class1 class1=new Class1();
BeanUtils.copyProperties(dto,class1);
Integer count = classMapper.update(class1);
map.put("success",true);
map.put("data",count);
map.put("msg","修改成功");
} catch (BeansException e) {
log.error("出现异常,参数是", JSONObject.toJSONString(dto));
log.error("出现异常",e);
}
return map;
}
/**
* <p>根据ID查询</p>
* @author bitgeek
* @date 2022/6/26 15:09
* @exception
* @return
* @param
*
*/
public HashMap<String, Object> load(Long id) {
HashMap<String, Object> map = new HashMap<>();
map.put("success",false);
try {
// TODO: 2022/6/24 修改成返回dto
ClassDTO result= classMapper.load(id);
String[] a = result.getHighlights().split(",");
ArrayList<String> strings = new ArrayList<>();
for (String string : a) {
strings.add(string.trim());
}
result.setHighlightss(strings);
Preconditions.checkNotNull(result,"没有此人信息");
if (result.getDescriptionStr().equals("2")){
result.setDescriptionStr("不是线上课程");
result.setDescription(2);
}else {
result.setDescriptionStr("是线上课程");
result.setDescription(1);
}
map.put("success",true);
map.put("data",result);
map.put("msg","查询成功");
} catch (Exception e) {
map.put("success",true);
map.put("msg",e.getMessage());
log.error("出现异常,参数是", id);
log.error("出现异常",e);
}
return map;
}
/**
* <p>分页查询</p>
* @author bitgeek
* @date 2022/6/26 15:09
* @exception
* @return
* @param
*
*/
public HashMap<String,Object> pageList(int offset, int pagesize, Class1 class1) {
ClassDTO dto = new ClassDTO();
BeanUtils.copyProperties(class1,dto);
// TODO: 2022/6/24 使用pagehelper 的方式来处理
PageHelper.startPage(offset,pagesize);
List<ClassDTO> newsList = classMapper.pageList(class1);
for (ClassDTO newsDTO : newsList) {
if ("1".equals(newsDTO.getDescriptionStr())){
newsDTO.setDescriptionStr("是线上课程");
}else if ("2".equals(newsDTO.getDescriptionStr())){
newsDTO.setDescriptionStr("不是线上课程");
}
}
Page pageInfo = (Page) newsList;
long total = pageInfo.getTotal();
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("total",total);
result.put("items",newsList);
result.put("success",true);
return result;
}
}