本章实现的功能
- 新数据库的建立
- 数据库的增删改查
- 前端的调试
新数据库的建立
在数据库中创建表bot
表中包含的列:
id: int
:非空、自动增加、唯一、主键
user_id: int
:非空
注意:在pojo中需要定义成userId(驼峰命名法),在queryWrapper中的名称仍然为user_id
title
: varchar(100)
description
: varchar(300)
content
:varchar(10000)
rating: int
:默认值为1500
createtime: datetime
pojo中定义日期格式的注解:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
modifytime: datetime
pojo中定义日期格式的注解:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
将数据库转化成一个可操作的类
pojo下定义Bot类
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(type = IdType.AUTO)
private Integer id;
private Integer userId;
private 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下定义BotMapper接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.backend.pojo.Bot;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BotMapper extends BaseMapper<Bot> {
}
数据库的增删改查
service中实现业务
- ‘service/user/bot’下创建 增删改查 四种接口
- ‘service/impl/user/bot’下创建 增删改查 四种类实现接口
- ‘controller/user/bot’下创建 增删改查 四种类调度service
创建 增删改查 四种接口
AddService
import java.util.Map;
public interface AddService {
Map<String,String>add(Map<String,String> data);
}
RemoveService
import java.util.Map;
public interface RemoveService {
Map<String,String>remove(Map<String,String> data);
}
UpdateService
import java.util.Map;
public interface UpdateService {
Map<String,String>update(Map<String,String> data);
}
GetListService
import java.util.List;
public interface GetListService {
List<Bot>getList();//每个人返回的是自己的bot,所以不需要传入参数
}
创建 增删改查 四种类实现接口
AddServiceImpl
import com.example.backend.mapper.BotMapper;
import com.example.backend.pojo.Bot;
import com.example.backend.pojo.User;
import com.example.backend.service.impl.utils.UserDetailsImpl;
import com.example.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 {
@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 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","标题长度不能超过100");
return map;
}
if(description==null||description.length()==0){
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()>10000){
map.put("error_message","代码长度不能超过10000");
return map;
}
Date now = new Date();//引入现在时间
Bot bot = new Bot(null, user.getId(), title, description, content, 1500, now, now);
//该Bot是pojo下的Bot
botMapper.insert(bot);//从mapper中插入该行
map.put("error_message", "success");
return map;
}
}
RemoveService
import com.example.backend.mapper.BotMapper;
import com.example.backend.pojo.Bot;
import com.example.backend.pojo.User;
import com.example.backend.service.impl.utils.UserDetailsImpl;
import com.example.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 {
@Autowired
private 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"));//需要从前端输入机器人的id
Bot bot = botMapper.selectById(bot_id);//表示查询该机器人的id
Map<String, String> map = new HashMap<>();
if (bot == null) {
map.put("error_message", "Bot不存在或已被删除");
return map;
}
if (!bot.getUserId().equals(user.getId())) {
map.put("error_message", "没有权限删除该Bot");
return map;
}
botMapper.deleteById(bot_id);//从mapper中删除该id
map.put("error_message", "success");
return map;
}
}
UpdateServiceImpl
import com.example.backend.mapper.BotMapper;
import com.example.backend.pojo.Bot;
import com.example.backend.pojo.User;
import com.example.backend.service.impl.utils.UserDetailsImpl;
import com.example.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 {
@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","标题长度不能大于100");
return map;
}
if(description==null||description.length()==0){
map.put("error_message","Bot描述内容不能为空");
return map;
}
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()>10000){
map.put("error_message","代码长度不能超过10000");
return map;
}
Bot bot=botMapper.selectById(bot_id);
if(bot==null){
map.put("error_message","Bot不存在或已删除");
return map;
}
if(!bot.getUserId().equals(user.getId())){
map.put("error_message","没有权限修改该Bot");
return map;
}
//通过创造一个新的bot来进行更新
Bot new_bot=new Bot(
bot.getId(),
user.getId(),
title,
description,
content,
bot.getRating(),
bot.getCreatetime(),
new Date()
);
botMapper.updateById(new_bot);//在mapper中更新
map.put("error_message","success");
return map;
}
}
GetListServiceImpl
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.backend.mapper.BotMapper;
import com.example.backend.pojo.Bot;
import com.example.backend.pojo.User;
import com.example.backend.service.impl.utils.UserDetailsImpl;
import com.example.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 {
@Autowired
private BotMapper botMapper;
@Override
public List<Bot> getList() {
UsernamePasswordAuthenticationToken authenticationToken =
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
UserDetailsImpl loginUser=(UserDetailsImpl) authenticationToken.getPrincipal();
User user=loginUser.getUser();
QueryWrapper<Bot> queryWrapper=new QueryWrapper<>();//通过创造一个表的方式来进行查找一个表
queryWrapper.eq("user_id",user.getId());//判断该表的id是否符合当前使用的id
return botMapper.selectList(queryWrapper);//返回mapper中的表
}
}
创建 增删改查 四种类调度service
AddController
import com.example.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 {
@Autowired
private AddService addService;
@PostMapping("/user/bot/add/")//通过注释添加一个路径
//先理解相对应的页面传入对应的值
public Map<String, String> add(@RequestParam Map<String, String> data) {
return addService.add(data);
}
}
RemoveController
import com.example.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 {
@Autowired
private RemoveService removeService;
@PostMapping("/user/bot/remove/")
public Map<String,String> remove(@RequestParam Map<String,String> data){
return removeService.remove(data);
}
}
UpdateController
import com.example.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 {
@Autowired
private UpdateService updateService;
@PostMapping("/user/bot/update/")
public Map<String,String> update(@RequestParam Map<String,String> data){
return updateService.update(data);
}
}
GetController
import com.example.backend.pojo.Bot;
import com.example.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 GetController {
@Autowired
private GetListService getListService;
@GetMapping("/user/bot/getlist/")
public List<Bot>getList(){
return getListService.getList();
}
}
前端的调试
setup
(){
const store = useStore();
//对添加的调试
$.ajax({
url: "http://127.0.0.1:3000/user/bot/add/",
type: "POST",
data: {
title: "Bot的标题",
description : "Bot的描述",
content: "Bot的代码",
},
headers: {
Authorization: "Bearer " + store.state.user.token,
},
success(resp) {
console.log(resp);
},
error(resp) {
console.log(resp);
}
})
//对删除的调试
$.ajax({
url: "http://127.0.0.1:3000/user/bot/remove/",
type: "post",
data: {
bot_id: 1,
},
headers: {
Authorization: "Bearer " + store.state.user.token,
},
success(resp) {
console.log(resp);
},
error(resp) {
console.log(resp);
}
})
//对查找的调试
$.ajax({
url: "http://127.0.0.1:3000/user/bot/getlist/",
type: "get",
headers: {
Authorization: "Bearer " + store.state.user.token,
},
success(resp) {
console.log(resp);
},
error(resp) {
console.log(resp);
}
})
//对更新的调试
$.ajax({
url: "http://127.0.0.1:3000/user/bot/update/",
type: "POST",
data: {
bot_id: 3,
title: "Bot的标题7",
description : "Bot的描述7",
content: "Bot的代码7",
},
headers: {
Authorization: "Bearer " + store.state.user.token,
},
success(resp) {
console.log(resp);
},
error(resp) {
console.log(resp);
}
})
}