CURD 的后端的API开发 (Pojo Mapper Service Controller分成四层的开发思想)
感觉这个模块主要就是实现一个C D U Q的功能
5.1上主要是介绍该模块的后端 + 然后如何在前端进行调试
5.1下的话就是实现这个前端页面了
1.Add
2.delete
3.Update
4.getlist(query)
(注解:)
首先依旧是在pojo层建立和数据库的映射关系
(注解引入@Data @NoArgsConstructor @AllArgsConstructor)
然后哦在BotMapper层使用BotMapper来extends BaseMapper<Bot>实现该接口 使得mapperr具备CURD的基础功能
(注解:@Mapper)
然后就是CURD了
在实现CURD Service层之前先在将自己要实现的业务放到一个接口上 <这里应该是为了应付可能出现其他的业务,也是为了方便Contoller层的调用Service层的业务>
然后就是开始写Servicec层的CURD了
1.AddService + AddServiceImpl
<1>
public Interface AddService {
Map<String,String> add(Map<String,String> data)
}
<2>
先Alt + Insert生成要实现的接口方法
然后先取出User
然后就是直接写if else 和添加了
@Service
public class AddServiceImpl implements AddService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> add(Map<String, String> data) {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl)(authenticationToken.getPrincipal());
User user = loginUser.getUser();
String title = data.get("title");
String content = data.get("content");
String description = data.get("description");
Map<String,String> map = new HashMap<>();
if (title == null || title.isEmpty()) {
map.put("error_message","标题不能为空");
return map;
}
if (title.length() > 100) {
map.put("error_message","标题不能大于100");
return map;
}
if (description == null || description.isEmpty()) {
description = "这个人很懒,什么也没有留下";
}
if (description.length() > 300) {
map.put("error_message","描述不能大于300");
return map;
}
if(content.length() > 10000) {
map.put("error_message","内容不能打于10000");
return map;
}
if (content.isEmpty()) {
map.put("error_message","描述的内容不能为空");
return map;
}
Date now = new Date();
Bot bot = new Bot(null, user.getId(), title,description,content,1500,now,now);
botMapper.insert(bot);
map.put("error_message","success");
return map;
}
}
下面 的逻辑差不多同理
2.removeService + removeServiceImpl
<1>
public Interface removeService {
Map<String,String> remove(Map<String,String> data);
}
<2>
@Service
public class RemoveServiceImpl implements RemoveService {
@Autowired
BotMapper botMapper;
@Override
public Map<String, String> remove(Map<String, String> data) {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
int bot_id = Integer.parseInt(data.get("bot_id"));
Bot bot = botMapper.selectById(bot_id);
Map<String,String> map = new HashMap<>();
if (bot == null) {
map.put("error_message","删除的东西不存在或者已经被删除");
return map;
}
if (!bot.getUserId().equals(user.getId())) {
map.put("error","没有权限删除该用户");
return map;
}
botMapper.deleteById(bot_id);
map.put("error_message","success");
return map;
}
}
3.UpdateService + UpdateServiceImpl
<1>
public Interface UpdateService{
Map<String,String> Update(Map<String,String> data)
}
<2>
@Service
public class UpdateServiceImpl implements UpdateService {
@Autowired
private BotMapper botMapper;
@Override
public Map<String, String> update(Map<String, String> data) {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
int bot_id = Integer.parseInt(data.get("bot_id"));
String title = data.get("title");
String description = data.get("description");
String content = data.get("content");
Map<String,String> map = new HashMap<>();
if (title == null || title.length() == 0) {
map.put("error_message","标题不能太短");
return map;
}
if (title.length() > 100) {
map.put("error_message","标题不能太长");
return map;
}
if (description == null) {
description = "这个人很难什么也没留下";
}
if (description.length() > 300) {
map.put("error_message", "Bot的描述不能超过300");
return map;
}
if (content == null || content.length() == 0) {
map.put("error_message","代码不能为空");
return map;
}
if (content.length() > 100005) {
map.put("error_message","代码不能太长");
return map;
}
Bot bot = botMapper.selectById(bot_id);
//System.out.println(bot);
if (bot == null) {
map.put("error_message","用户不存在或者已经被删除");
return map;
}
if (!bot.getUserId().equals(user.getId())) {
map.put("error_message","没有权限修改改用户");
return map;
}
Bot new_bot = new Bot(
bot.getId(),
user.getId(),
title,
description,
content,
bot.getRating(),
bot.getCreatetime(),
new Date()
);
botMapper.updateById(new_bot);
map.put("error_message","success");
return map;
}
}
4.getlistService + getlistServiceImpl
<1>
public Interface getlistService {
List<Map<String,String>> getlist();
}
<2>
@Service
public class GetListServiceImpl implements GetListService {
@Autowired
private BotMapper botMapper;
@Override
public List<Bot> getList() {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
User user = loginUser.getUser();
//封装一个查询条件 来查询DB中的信息
QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id", user.getId());
return botMapper.selectList(queryWrapper);
}
}