Mybatis通过注解方式实现CRUD

Mybatis通过注解方式实现CRUD

完成了MySql的学习后,我们确定了库中有adminuser与document两个数据表,document包含所有的公文,但是通过userId属性对应adminuser中的项,adminuser只能读userid为自己id的项,这样我们在两个数据表的情况下,完成多个用户的公文存储。

接下来就只要同后端人员沟通好要使用哪些属性来完成事件即可,也就是文件的增删查改等步骤。由于sql语句比较简单,我们使用注解的方式实现了CRUD,参考了how2j中mybatisJDBC的教程。

以公文上传为例

在前端请求上传公文时,post向api发送调用请求,请求内容是/document/addDocument.api会把请求定位addDocument方法里。其中Document d即为前端传来的json化的document对象。

@PostMapping("/document/addDocument")
public String addDocument(@RequestBody Document d){
	documentServiceImpl.addDocument(d);
	return "success";
}

之后会执行实现类DocumentUserServicelmpl的addDocument方法,会先按pojo中的属性一一赋值,建立后端中的document对象.

public void addDocument(Document d){
	documentMapper.addDocument(d);
}

之后实现类DocumentUserServicelmpl又会调用documentMapper的addDocument,通过Mysql语句实现对公文表项的添加.

@Insert("insert into document(title,content,committee,wordsize,classfication,timelimit,date,security,notread,userid) values(#{title},#{content},#{authorities},#{fontSize},#{category},#{timeLimit},#{date},#{level},#{notRead},#{userId})")
public void addDocument(Document d);

以下是部分后端代码

api

AdminUserAPI

@PostMapping("/adminUser/login/{userName}/{password}")
public AdminUser login(@PathVariable String userName, @PathVariable String password){
	return adminUserServiceImpl.login(userName, password);
}

@GetMapping("/logins/{userName}/{password}")
public AdminUser logins(@PathVariable String userName, @PathVariable String password){
	return adminUserServiceImpl.login(userName, password);
}

@PutMapping("/adminUser/resetPassword/{password}")
public String resetPassword(@PathVariable String password){
	adminUserServiceImpl.resetPassword(password);
	return "success";
}

@PutMapping("/adminUser/stopUser/{userId}")
public String stopUser(@PathVariable int userId){
	adminUserServiceImpl.stopUser(userId);
	return "sucess";
}

@GetMapping("/adminUser/listUser")
public List<AdminUser> listUser(){
	return adminUserServiceImpl.listUser();
}

DocumentAPI

@GetMapping("/document/notRead/{userId}")
public List<Document> notReadDocumentList(@PathVariable int userId){
	return documentServiceImpl.notReadDocumentList(userId);
}

@GetMapping("/document/read/{userId}")
public List<Document> readDocumentList(@PathVariable int userId){
	return documentServiceImpl.readDocumentList(userId);
}

@GetMapping("/document/rubbish/{userId}")
public List<Document> rubbishDocumentList(@PathVariable int userId){
	return documentServiceImpl.isRubbishDocumentList(userId);
}

@GetMapping("/document/collect/{userId}")
public List<Document> collectDocumentList(@PathVariable int userId){
	return documentServiceImpl.isCollectDocumentList(userId);
}

@PostMapping("/document/addDocument")
public String addDocument(@RequestBody Document d){
	documentServiceImpl.addDocument(d);
	return "success";
}

@PutMapping("/document/delete/{id}")
public String delete(@PathVariable int id){
	documentServiceImpl.delete(id);
	return "success";
}

@PutMapping("/document/isRead/{id}")
public String isRead(@PathVariable int id){
	documentServiceImpl.read(id);
	return "success";
}

@PutMapping("/document/isCollect/{id}")
public String isCollect(@PathVariable int id){
	documentServiceImpl.collect(id);
	return "success";
}

pojo

pojo部分会接收前端和数据库传过来的数值,把这些数值转化为java里的对象

AdminUser

@TableId("id")			//用户id(主键)
private Integer id;
@TableField("username")	//用户名
private String userName;
@TableField("password")	//密码
private String password;
@TableField("islogin")	//登录状态
private boolean isLogin;
@TableField("role")		//用户表
private String role;
@TableField("limit")	//权限级别
private boolean limit;

Document

@TableId("id")		
private Integer id;
@TableField("title")		//标题
private String title;
@TableField("content")		//密文
private String content;
@TableField("committee")	//收发单位
private String authorities;
@TableField("wordsize")		//公文字号
private Integer fontSize;
@TableField("classfication")//公文类别
private String category;
@TableField("timelimit")	//紧急程度
private String timeLimit;
@TableField("date")			//时间
private Date date;
@TableField("security")		//密级
private String level;
@TableField("notread")		//未读符
private boolean notRead;
@TableField("userid")		//上传用户
private Integer userId;
@TableField("rubbish")		//回收符
private boolean rubbish;
@TableField("collect")		//收藏符
private boolean collect;

serviceimpl

AdminUserServicelmpl

public AdminUser login(String userName,String password){
	return adminUserMapper.login(userName, password);		
}

public void resetPassword(String password){
	adminUserMapper.resetPassword(password);
}

public void stopUser(int userId){
	adminUserMapper.stopUser(userId);
}

public List<AdminUser> listUser(){
	return adminUserMapper.listUser();
}

DocumentUserServicelmpl

public List<Document> notReadDocumentList(int userId){
	return documentMapper.notReadDocumentList(userId);
}

public List<Document> readDocumentList(int userId){
	return documentMapper.readDocumentList(userId);
}

public List<Document> isRubbishDocumentList(int userId){
	return documentMapper.isRubbishDocumentList(userId);
}

public List<Document> isCollectDocumentList(int userId){
	return documentMapper.isCollectDocumentList(userId);
}

public void addDocument(Document d){
	documentMapper.addDocument(d);
}

public void delete(int id){
	documentMapper.delete(id);
}

public void read(int id){
	documentMapper.read(id);
}

public void collect(int id){
	documentMapper.collect(id);
}

mapper

DocumentMapper

  • 返回未读列表

    @Select("select * from document where userid = #{userId} and notread > 0")
    public List<Document> notReadDocumentList(int userId);
    
  • 返回已读列表

    @Select("select * from document where userid = #{userId} and notread = 0")
    public List<Document> readDocumentList(int userId);
    
  • 返回收藏列表

    @Select("select * from document where userid = #{userId} and collect > 0")
    public List<Document> isCollectDocumentList(int userId);
    
  • 返回回收列表

    @Select("select * from document where userid = #{userId} and rubbish > 0 ")
    public List<Document> isRubbishDocumentList(int userId);
    
  • 添加文件项

    @Insert("insert into document(title,content,committee,wordsize,classfication,timelimit,date,security,notread,userid) values(#{title},#{content},#{authorities},#{fontSize},#{category},#{timeLimit},#{date},#{level},#{notRead},#{userId})")
    public void addDocument(Document d);
    
  • 删除文件项

    @Update("update document set rubbish = 1")
    public void delete(int id);
    
  • 更改已读属性

    @Update("update document set notread = 0 where id = #{id}")
    public void read(int id);
    
  • 更改收藏属性

    @Update("update document set collect = 1 where id = #{id}")
    public void collect(int id);
    

AdminUserMapper

  • 返回用户名与密码

    @Select("select * from adminuser where username = #{userName} and password = #{password}")
    public AdminUser login(@Param("userName") String userName ,@Param("password") String password);
    
  • 重置密码

    @Update("update adminuser set `password` = #{password}")
    public void resetPassword(String password);
    
  • 修改权限

    @Update("update user set limit = 0 where id = #{userId}")
    public void stopUser(int userId);
    
  • 查看用户

    @Select("select * from user")
    public List<AdminUser> listUser();
    
posted @ 2020-11-29 17:42  一个李长兴  阅读(74)  评论(0编辑  收藏