本期代码量较大,且涉及多表操作逻辑复杂建议配合目录食用!每个功能的编写顺序都是
Service
=>ServiceImpl
=>Dao
=>DaoImpl
最后再用Controller
调用Service
完成接口的实现
前期准备
代码语言:javascript复制本篇的验证都是基于token进行操作,所以需要先准备一个token生成器的test方法!最后将生成的token保存到请求头的
token
中!
class TokenUtilsTest {
@Test
void sign() {
// 如果不能使用builder方法,请查看pom文件中是否存在lombok依赖,若依赖存在请检查User实体类上是否添加@Builder注解
User user = User.builder()
.openid("9IWM51WXRKD9BN21CJV SS9SXBAMV4F FEA")
.build();
System.out.println(TokenUtils.sign(user));
}
}
查看已创建的房间
Dao数据持久层
- 暴露接口
代码语言:javascript复制
VideoroomDao
根据vrid查询视频房间信息,返回数组对象
public List<Videoroom> get_videoroom(Long vrid);
代码语言:javascript复制
CreateRoomDao
根据加密后的openid查询创建房间记录表中的所以数据
public List<CreateRoom> get_createRoomList(String md5_openid);
- 数据持久层接口实现
代码语言:javascript复制
VideoroomDaoImpl
@Autowired
private VideoroomMapper videoroomMapper;
private VideoroomExample videoroomExample = new VideoroomExample();
public List<Videoroom> get_videoroom(Long vrid){
videoroomExample.createCriteria().andVrIdEqualTo(vrid);
return videoroomMapper.selectByExample(videoroomExample);
}
代码语言:javascript复制
CreateRoomDao
@Autowired
private CreateRoomMapper createRoomMapper;
private CreateRoomExample createRoomExample = new CreateRoomExample();
public List<CreateRoom> get_createRoomList(String md5_openid){
createRoomExample.createCriteria().andOpenidEqualTo(md5_openid);
return createRoomMapper.selectByExample(createRoomExample);
}
Service服务层
- 暴露接口
代码语言:javascript复制
VideoroomService
public BaseResponse get_videoroom(String token);
- 服务层接口实现
代码语言:javascript复制VideoroomServiceImpl
@Autowired
private CreateRoomDao createRoomDao;
@Autowired
private VideoroomDao videoroomDao;
/*
* @Description 根据token查看已创建的房间等信息
* @Param null
* @Return {@link null}
* @Author Ocean_IT
* @Email 14312400@qq.com
* @Date 2022/10/5 2:27
*/
public BaseResponse get_videoroom(String token){
List<CreateRoom> createRoomList = createRoomDao.get_createRoomList(TokenUtils.getOpenid(token));
if(createRoomList.size() == 0){
return RespGenerator.success("未查询到用户已创建房间信息!");
}
return RespGenerator.success(videoroomDao.get_videoroom(createRoomList.get(0).getVrId()));
}
Controller控制器层
代码语言:javascript复制
Videoroomcontroller
@Autowired
private VideoroomService videoroomService;
//查看自己创建的房间
@GetMapping("/videoroom")
public BaseResponse get_videoroom(HttpServletRequest request){
return videoroomService.get_videoroom(request.getHeader("token"));
}
调用方法
url:
http://127.0.0.1:8080/videoroomcontroller/videoroom
Methed:GET Header:token return:json
创建房间
Mapper编写
代码语言:javascript复制UserMapper.xml
<update id="updateByAddCount_create_room" parameterType="map">
<!--
create_room 数量 1
-->
update user
set create_room = create_room 1
where openid = #{record.openid,jdbcType=VARCHAR}
</update>
<update id="updateByDelCount_create_room" parameterType="map">
<!--
create_room 数量 - 1
-->
update user
set create_room = create_room - 1
where openid = #{record.openid,jdbcType=VARCHAR}
</update>
Mapper接口暴露
代码语言:javascript复制UserMapper.java
int updateByAddCount_create_room(String md5_openid);
int updateByDelCount_create_room(String md5_openid);java
Dao数据持久层
- 暴露接口
代码语言:javascript复制
VideoDao
根据vid查询视频表信息,返回对象。
public Video get_vid(Long vid);
代码语言:javascript复制
VideoroomDao
传入一个Videoroom对象并将他插入数据库,返回布尔类型
public Boolean post_videoroom(Videoroom videoroom);
代码语言:javascript复制
UserDao
根据openid查询user表数据,并根据type执行createVideo字段的自增和自减,返回布尔类型
public Boolean put_createVideo_num(String md5_openid,Boolean type);
代码语言:javascript复制
CreateRoomDao
根据主键查询表中是否超出创建房间的数量再执行创建。返回布尔类型
public Boolean Post_createRoom(CreateRoom createRoom);
- 数据持久层接口实现
代码语言:javascript复制
VideoDaoImpl
@Autowired
private VideoMapper videoMapper;
public Video get_vid(Long vid){
return videoMapper.selectByPrimaryKey(vid);
}
代码语言:javascript复制
VideoroomDaoImpl
public Boolean post_videoroom(Videoroom videoroom){
return videoroomMapper.insert(videoroom) == 1?true:false;
}
代码语言:javascript复制
UserDaoImpl
public Boolean put_createVideo_num(String md5_openid,Boolean type){
if(type){
return userMapper.updateByAddCount_create_room(md5_openid)==1?true:false;
}else{
return userMapper.updateByDelCount_create_room(md5_openid)==1?true:false;
}
}
代码语言:javascript复制
CreateRoomDaoImpl
public Boolean Post_createRoom(CreateRoom createRoom){
createRoomExample.createCriteria().andOpenidEqualTo(createRoom.getOpenid());
return createRoomMapper.selectByExample(createRoomExample).size()<1 ?createRoomMapper.insert(createRoom) ==1?true:false:false;
}
Service服务层
- 暴露接口
代码语言:javascript复制
VideoroomService
public BaseResponse post_videoroom(Videoroom videoroom,Long vid,String token);
- 服务层接口实现
代码语言:javascript复制
VideoroomServiceImpl
@Autowired
private CreateRoomDao createRoomDao;
@Autowired
private VideoroomDao videoroomDao;
@Autowired
private UserDao userDao;
@Autowired
private VideoDao videoDao;
/*
* @Description 根据token创建房间
* @Param null
* @Return {@link null}
* @Author Ocean_IT
* @Email 14312400@qq.com
* @Date 2022/10/5 2:28
*/
@Transactional
public BaseResponse post_videoroom(Videoroom videoroom,Long vid,String token){
Video video = videoDao.get_vid(vid);
Assert.notNull(video,"未查询到视频信息,请重新选择视频创建房间");
videoroom.setVrId(IdWorker.getId());
Assert.isTrue(videoroomDao.post_videoroom(videoroom),"房间创建失败");
CreateRoom createRoom = CreateRoom.builder()
.openid(TokenUtils.getOpenid(token))
.crId(IdWorker.getId())
.vrId(videoroom.getVrId())
.vid(vid).build();
Assert.isTrue(userDao.put_createVideo_num(TokenUtils.getOpenid(token),true),"房间数更新失败!");
return createRoomDao.Post_createRoom(createRoom)?RespGenerator.success(createRoom):RespGenerator.fail("400","房间创建失败");
}
Controller控制器层
代码语言:javascript复制
videoroomcontroller
//创建房间
@PostMapping("/videoroom/{vid}")
public BaseResponse post_videoroom(@PathVariable("vid") Long vid,@RequestBody Videoroom videoroom, HttpServletRequest request){
return videoroomService.post_videoroom(videoroom,vid,request.getHeader("token"));
}
调用方法
url:http://127.0.0.1:8080/videoroomcontroller/videoroom/{vid} Methed:POST Header:token DATA:
{ "vrTitle":"标题", "vrDescribe":"描述信息" }
return:json
修改房间信息
Dao数据持久层
- 暴露接口
代码语言:javascript复制
VideoroomDao
根据vrid查询并修改数据为videoroom对象中的值
public Boolean put_videoroom(Long vrid,Videoroom videoroom);
- 数据持久层接口实现
代码语言:javascript复制
VideoroomDaoImpl
public Boolean put_videoroom(Long vrid,Videoroom videoroom){
videoroomExample.createCriteria().andVrIdEqualTo(vrid);
videoroom.setVrId(null);
return videoroomMapper.updateByExampleSelective(videoroom,videoroomExample)==1?true:false;
}
Service服务层
- 暴露接口
代码语言:javascript复制
VideoroomService
public BaseResponse put_videoroom(Videoroom videoroom,String token);
- 服务层接口实现
代码语言:javascript复制
VideoroomServiceImpl
/*
* @Description 根据token等信息修改房间信息
* @Param null
* @Return {@link null}
* @Author Ocean_IT
* @Email 14312400@qq.com
* @Date 2022/10/5 6:20
*/
@Transactional
public BaseResponse put_videoroom(Videoroom videoroom,String token){
List<CreateRoom> createRoomList = createRoomDao.get_createRoomList(TokenUtils.getOpenid(token));
if(createRoomList.size() == 0){
return RespGenerator.fail("400","未查询到你要修改房间的信息!");
}else{
Assert.isTrue(videoroomDao.put_videoroom(createRoomList.get(0).getVrId(),videoroom),"修改房间信息失败!");
return RespGenerator.success_noData("房间信息修改完成!");
}
}
Controller控制器层
代码语言:javascript复制
Videoroomcontroller
//修改房间标题等信息
@PutMapping("/videoroom")
public BaseResponse put_videoroom(@RequestBody Videoroom videoroom,HttpServletRequest request){
return videoroomService.put_videoroom(videoroom,request.getHeader("token"));
}
调用方法
url:http://127.0.0.1:8080/videoroomcontroller/videoroom Methed:PUT Header:token Data:
{ "vrTitle":"sss打赏", "vrDescribe":"dasd", "vrId":"0" }
return:json
删除房间
Dao数据持久层
- 暴露接口
代码语言:javascript复制
CreateRoomDao
根据vrid删除表信息
public Boolean del_createRoom(Long vrid);
代码语言:javascript复制
VideoroomDao
根据vrid删除表信息
public Boolean del_videoroom(Long vrid);
- 数据持久层接口实现
代码语言:javascript复制
CreateRoomDaoImpl
public Boolean del_createRoom(Long vrid){
createRoomExample.createCriteria().andVrIdEqualTo(vrid);
return createRoomMapper.deleteByExample(createRoomExample) == 1?true:false;
}
代码语言:javascript复制
VideoroomDaoImpl
public Boolean del_videoroom(Long vrid){
return videoroomMapper.deleteByPrimaryKey(vrid) ==1?true:false;
}
Service服务层
- 暴露接口
代码语言:javascript复制
VideoroomService
public BaseResponse del_videoroom(String token);
- 服务层接口实现
代码语言:javascript复制
VideoroomServiceImpl
/*
* @Description 根据token删除房间
* @Param null
* @Return {@link null}
* @Author Ocean_IT
* @Email 14312400@qq.com
* @Date 2022/10/5 14:27
*/
@Transactional
public BaseResponse del_videoroom(String token){
List<CreateRoom> createRoomList = createRoomDao.get_createRoomList(TokenUtils.getOpenid(token));
if(createRoomList.size() == 0){
return RespGenerator.fail("400","未查询到你要删除房间的信息!");
}
Long vrid = createRoomList.get(0).getVrId();
Assert.isTrue(createRoomDao.del_createRoom(vrid),"删除房间记录失败");
Assert.isTrue(videoroomDao.del_videoroom(vrid),"删除房间信息失败");
Assert.isTrue(userDao.put_createVideo_num(TokenUtils.getOpenid(token),false),"房间数更新失败!");
return RespGenerator.success_noData("房间删除成功!");
}
Controller控制器层
代码语言:javascript复制
VideoroomController
//删除房间信息
@DeleteMapping("/videoroom")
public BaseResponse del_videoroom(HttpServletRequest request){
return videoroomService.del_videoroom(request.getHeader("token"));
}
调用方法
url:http://127.0.0.1:8080/videoroomcontroller/videoroom/{vid} Methed:DELETE Header:token return:json