当前位置: 首页 > news >正文

石家庄免费网站设计上海专门做培训的网站

石家庄免费网站设计,上海专门做培训的网站,做服装商城网站,中国石油销售公司网站建设笔记内容转载自 AcWing 的 SpringBoot 框架课讲义#xff0c;课程链接#xff1a;AcWing SpringBoot 框架课。 CONTENTS 1. 更新数据库表2. 实现后端API 本节实现存储用户 Bot 信息的数据表以及操作 Bot 数据的增删改查 API。 1. 更新数据库表 我们需要创建一个表来保存 Bo…笔记内容转载自 AcWing 的 SpringBoot 框架课讲义课程链接AcWing SpringBoot 框架课。 CONTENTS 1. 更新数据库表2. 实现后端API 本节实现存储用户 Bot 信息的数据表以及操作 Bot 数据的增删改查 API。 1. 更新数据库表 我们需要创建一个表来保存 Bot 的信息新建一个 bot 表包含以下几个列 id: int非空、自动增加、唯一、主键。user_id: int非空。注意在 pojo 中需要定义成 userId在 queryWrapper 中的名称仍然为 user_id。title: varchar(100)。description: varchar(300)。contentvarchar(10000)。rating: int默认值为1500。createtime: datetime注意在 pojo 中定义日期格式的注解JsonFormat(pattern yyyy-MM-dd HH:mm:ss)。modifytime: datetime。 可以使用如下 SQL 语句一键创建好该表 CREATE TABLE kob.bot (id int NOT NULL AUTO_INCREMENT,user_id int NOT NULL,title varchar(100) NULL,description varchar(300) NULL,content varchar(10000) NULL,rating int NULL DEFAULT 1500,createtime datetime NULL,modifytime datetime NULL,PRIMARY KEY (id) );创建好数据库表后我们需要创建一个 pojo在 pojo 目录下创建 Bot 类 package com.kob.backend.pojo;import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;import java.util.Date;Data NoArgsConstructor AllArgsConstructor public class Bot {TableId(value id, type IdType.AUTO) // 声明id为自增类型private Integer id;private Integer userId; // 注意驼峰命名userId之后会被解析为user_id别写成userID因为这样会解析成user_i_dprivate String title;private String description;private String content;private Integer rating;JsonFormat(pattern yyyy-MM-dd HH:mm:ss) // 注意日期格式的设置private Date createtime;JsonFormat(pattern yyyy-MM-dd HH:mm:ss)private Date modifytime; }然后就可以实现 mapper在 mapper 目录下创建 BotMapper 接口 package com.kob.backend.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.kob.backend.pojo.Bot; import org.apache.ibatis.annotations.Mapper;Mapper public interface BotMapper extends BaseMapperBot { }2. 实现后端API 首先在 service.user 包下创建 bot 包存放与 Bot 相关的 Service 代码然后在 service.impl.user 包下创建 bot 包存放相应的 Service 实现代码最后在 controller.user 包下创建 bot 包存放 Controller。 我们需要实现以下四个 API /user/bot/add/创建一个 Bot。/user/bot/remove/删除一个 Bot。/user/bot/update/修改一个 Bot。/user/bot/getlist/查询 Bot 列表。 在 service.user.bot 包下创建这四个 API 的 Service 接口 1AddService package com.kob.backend.service.user.bot;import java.util.Map;public interface AddService {MapString, String add(MapString, String data); }2RemoveService package com.kob.backend.service.user.bot;import java.util.Map;public interface RemoveService {MapString, String remove(MapString, String data); } 3UpdateService package com.kob.backend.service.user.bot;import java.util.Map;public interface UpdateService {MapString, String update(MapString, String data); }4GetListService package com.kob.backend.service.user.bot;import com.kob.backend.pojo.Bot;import java.util.List;public interface GetListService {ListBot getList(); // 根据用户信息获取Bot用户信息存放在令牌中因此不用传参数 }接下来在 service.impl.user.bot 包下创建这四个 Service 接口的实现 1AddServiceImpl package com.kob.backend.service.impl.user.bot;import com.kob.backend.mapper.BotMapper; import com.kob.backend.pojo.Bot; import com.kob.backend.pojo.User; import com.kob.backend.service.impl.utils.UserDetailsImpl; import com.kob.backend.service.user.bot.AddService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service;import java.util.Date; import java.util.HashMap; import java.util.Map;Service public class AddServiceImpl implements AddService {Autowiredprivate BotMapper botMapper;Overridepublic MapString, String add(MapString, String data) {UsernamePasswordAuthenticationToken authenticationToken (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();UserDetailsImpl loginUser (UserDetailsImpl) authenticationToken.getPrincipal();User user loginUser.getUser(); // 获取当前登录的用户String title data.get(title);String description data.get(description);String content data.get(content);MapString, String res new HashMap();if (title null || title.isEmpty()) {res.put(result, The title cant be empty!);return res;}if (title.length() 100) {res.put(result, Title length cant exceed 100!);return res;}if (description null || description.isEmpty()) {description 这个用户很懒什么也没留下~;}if (description.length() 300) {res.put(result, Description length cant exceed 300!);return res;}if (content null || content.isEmpty()) {res.put(result, The content cant be empty!);return res;}if (content.length() 10000) {res.put(result, Code length cant exceed 10000!);return res;}Date now new Date();Bot bot new Bot(null, user.getId(), title, description, content, 1500, now, now);botMapper.insert(bot);res.put(result, success);return res;} }2RemoveServiceImpl package com.kob.backend.service.impl.user.bot;import com.kob.backend.mapper.BotMapper; import com.kob.backend.pojo.Bot; import com.kob.backend.pojo.User; import com.kob.backend.service.impl.utils.UserDetailsImpl; import com.kob.backend.service.user.bot.RemoveService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service;import java.util.HashMap; import java.util.Map;Service public class RemoveServiceImpl implements RemoveService {Autowiredprivate BotMapper botMapper;Overridepublic MapString, String remove(MapString, String data) {UsernamePasswordAuthenticationToken authenticationToken (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();UserDetailsImpl loginUser (UserDetailsImpl) authenticationToken.getPrincipal();User user loginUser.getUser(); // 需要判断要删除的Bot是不是当前登录用户的Botint bot_id Integer.parseInt(data.get(bot_id));Bot bot botMapper.selectById(bot_id);MapString, String res new HashMap();if (bot null) {res.put(result, Bot doesnt exist!);return res;}if (!bot.getUserId().equals(user.getId())) {res.put(result, No permission to delete the bot!);return res;}botMapper.deleteById(bot_id);res.put(result, success);return res;} }3UpdateServiceImpl package com.kob.backend.service.impl.user.bot;import com.kob.backend.mapper.BotMapper; import com.kob.backend.pojo.Bot; import com.kob.backend.pojo.User; import com.kob.backend.service.impl.utils.UserDetailsImpl; import com.kob.backend.service.user.bot.UpdateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service;import java.util.Date; import java.util.HashMap; import java.util.Map;Service public class UpdateServiceImpl implements UpdateService {Autowiredprivate BotMapper botMapper;Overridepublic MapString, String update(MapString, 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);MapString, String res new HashMap();if (title null || title.isEmpty()) {res.put(result, The title cant be empty!);return res;}if (title.length() 100) {res.put(result, Title length cant exceed 100!);return res;}if (description null || description.isEmpty()) {description 这个用户很懒什么也没留下~;}if (description.length() 300) {res.put(result, Description length cant exceed 300!);return res;}if (content null || content.isEmpty()) {res.put(result, The content cant be empty!);return res;}if (content.length() 10000) {res.put(result, Code length cant exceed 10000!);return res;}Bot bot botMapper.selectById(bot_id);if (bot null) {res.put(result, Bot doesnt exist!);return res;}if (!bot.getUserId().equals(user.getId())) {res.put(result, No permission to update the bot!);return res;}Bot new_bot new Bot(bot.getId(), user.getId(), title, description, content, bot.getRating(), bot.getCreatetime(), new Date());botMapper.updateById(new_bot);res.put(result, success);return res;} }4GetListServiceImpl package com.kob.backend.service.impl.user.bot;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.kob.backend.mapper.BotMapper; import com.kob.backend.pojo.Bot; import com.kob.backend.pojo.User; import com.kob.backend.service.impl.utils.UserDetailsImpl; import com.kob.backend.service.user.bot.GetListService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service;import java.util.List;Service public class GetListServiceImpl implements GetListService {Autowiredprivate BotMapper botMapper;Overridepublic ListBot getList() {UsernamePasswordAuthenticationToken authenticationToken (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();UserDetailsImpl loginUser (UserDetailsImpl) authenticationToken.getPrincipal();User user loginUser.getUser();QueryWrapperBot queryWrapper new QueryWrapper();queryWrapper.eq(user_id, user.getId());return botMapper.selectList(queryWrapper);} }最后在 controller.user.bot 包下创建对应的 Controller 1AddController package com.kob.backend.controller.user.bot;import com.kob.backend.service.user.bot.AddService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.Map;RestController public class AddController {Autowiredprivate AddService addService;PostMapping(/user/bot/add/)public MapString, String add(RequestParam MapString, String data) {return addService.add(data);} }2RemoveController package com.kob.backend.controller.user.bot;import com.kob.backend.service.user.bot.RemoveService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.Map;RestController public class RemoveController {Autowiredprivate RemoveService removeService;PostMapping(/user/bot/remove/)public MapString, String remove(RequestParam MapString, String data) {return removeService.remove(data);} }3UpdateController package com.kob.backend.controller.user.bot;import com.kob.backend.service.user.bot.UpdateService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.Map;RestController public class UpdateController {Autowiredprivate UpdateService updateService;PostMapping(/user/bot/update/)public MapString, String update(RequestParam MapString, String data) {return updateService.update(data);} }4GetListController package com.kob.backend.controller.user.bot;import com.kob.backend.pojo.Bot; import com.kob.backend.service.user.bot.GetListService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;RestController public class GetListController {Autowiredprivate GetListService getListService;GetMapping(/user/bot/getlist/)public ListBot getList() {return getListService.getList();} }
http://www.sadfv.cn/news/235973/

相关文章:

  • 住建部网站村镇建设管理平台河南手机网站建设公司
  • 如何做自己网站平台电商设计的前景
  • 简述一个网站开发流程微盟集团
  • 网站做404好处网站哪家公司做的最好
  • 易语言如何做浏网站网站建设二级菜单
  • icp备案查询网站百度小说排行榜
  • 网站添加flash访问网站速度很慢
  • 网商之窗挂北京优化网站外包公司
  • 小程序网站制作公司广西水利电力建设集团网站
  • 网站建设要知道的东莞做网站费用
  • 西安市高新区建设局网站网站开发个人技能
  • 手机网站 o2o360建筑网官方网站
  • 网站变成手机网站上饶建设局网站
  • 大型网站后台登录地址一般是如何设置的企业展厅效果图
  • iis7发布静态网站西宁网络公司网站制作
  • 环保网站可以做哪些内容湖南3合1网站建设
  • 在线看视频网站怎么做云县网站建设 云县网
  • 网站网页?问?谷歌搜索入口
  • 网站建设的步骤是什么意思建筑兼职网站
  • 网站流量统计怎么做域名被锁定网站打不开
  • c#网站开发工具在什么网站上兼职做加工中心编程
  • 重庆智能网站建设设计贵阳网站建设是什么
  • 手机营销网站制作西西美人美体
  • 行业网站建设收费明细做购物网站需要接口吗
  • 网站建设综合实训心得体会湘潭做网站的公司
  • 德阳网站开发熊掌号北京企业建站服务中企
  • 找一个网站做优化分析我要表白网站在线制作
  • 怎样做金融网站百度seo推广计划类型包含
  • wordpress建站优缺点wordpress仿喜马拉雅
  • 电大亿唐网不做网站做品牌人才招聘网最新招聘2023