springboot-curd基于mybatis项目搭建
项目结构:
文章图片
pom.xml文件:
org.springframework.boot spring-boot-starter-parent2.2.2.RELEASE com.liuyang springbootcurd0.0.1-SNAPSHOT springbootcurd Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter2.2.1 junit junit4.13 testcom.github.pagehelper pagehelper-spring-boot-starter1.2.13 mysql mysql-connector-java8.0.25 com.mchange c3p00.9.5.5 org.apache.commons commons-lang3org.springframework.boot spring-boot-devtoolstrue org.springframework.data spring-data-commons2.2.3.RELEASE org.springframework.boot spring-boot-maven-plugin
yml
## 数据源配置spring:## 热部署配置devtools:restart:enabled: true# 设置重启的目录,添加目录的文件需要restartadditional-paths: src/main/java# 解决项目自动重新编译后接口报404的问题poll-interval: 3000quiet-period: 1000datasource:type: com.mchange.v2.c3p0.ComboPooledDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/springboot_mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8username: rootpassword: 123456##视图的配置freemarker:template-loader-path: classpath*:/views/charset: UTF-8content-type: text/htmlcache: falsesuffix: .ftl## mybatis 配置mybatis:#映射文件的存放路径mapper-locations: classpath*:/mapper/*.xmltype-aliases-package: com.liuyang.bean,com.liuyang.vo,com.liuyang.queryconfiguration:## 下划线转驼峰配置map-underscore-to-camel-case: true## pageHelperpagehelper:helper-dialect: mysql## 显示dao 执行sql语句logging:level:com:xxxx:mapper: debug
bean
private Integer userId; private String userName; private String userPwd;
mapper
public interface Usermapper {public User selectuserbyid(Integer id); public User selectUserByName(String userName); //注意返回的类型public int insertUser(User user); //根据id进行删除数据public int deleteUserById(int userId); //条件查询public ListselectUserByItem(UserQuery userQuery); }
mapping.xml
select * from t_user where user_id=#{userId}select * from t_user where user_name=#{userName}select * from t_user where user_name= insert into t_user(user_name,user_pwd) values(#{userName},#{userPwd}); deletefrom t_user where user_id=#{userId} select * from t_useruser_name like "%${userName}%"
controller
@RestControllerpublic class Usercontroller {@Resourceprivate Usermapper usermapper; @GetMapping("one/{id}")public User sayUser(@PathVariable Integer id) {System.out.println( id + "<<<" ); //根据ID查询User user = usermapper.selectuserbyid(id); return user; }//没有检测重复@GetMapping("userOne/{name}")public User sayUserOne(@PathVariable String name) {System.out.println( name + "<<<" ); //根据ID查询User user = usermapper.selectUserByName( name ); //user--jsonreturn user; }@PutMapping("add")public int sayAdd(User user) {System.out.println( user + "<<<" ); //根据ID查询return usermapper.insertUser( user ); }@DeleteMapping("delete/{userId}")public int sayDel(@PathVariable Integer userId) {System.out.println( userId + "<<<" ); //根据ID查询return usermapper.deleteUserById( userId ); }@GetMapping("query")public java.util.ListsayDel(UserQuery userQuery) {System.out.println( userQuery + "<<<" ); //分页的集合数据return usermapper.selectUserByItem( userQuery ); }}
query
public class UserQuery {privateInteger pageNum=1; privateInteger pageSize=1; privateString userName;
以上只是简单的实现了增删改查 如果发生了插入或者是删除异常,我们就应该会自定义全局异常去捕获出现的问题
文章图片
新增类
service
主要进行的业务的处理Userservice
public interface Userservice {public User queryUserById(Integer userId); public User queryUserByName(String userName); public void saveUser(User user); public void changeUser(User user); //删除一条public void removeUserById(IntegeruserId); //查询用户信息public PageInfoqueryUserByPage(UserQuery userQuery); }
UserserviceImpl
@Servicepublic class UserserviceImpl implements Userservice {@Resourceprivate Usermapper usermapper; //查询都是用的User返回类型@Overridepublic User queryUserById(Integer userId) {return usermapper.selectuserbyid( userId ); }@Overridepublic User queryUserByName(String userName) {return usermapper.selectUserByName( userName ); }//增加操作/****需要用户名 AssertUtil*用户密码*用户是否存在*添加用户是否成功* @param user*/@Overridepublic void saveUser(User user) {//验证用户名AssertUtil.isTrue( StringUtils.isBlank(user.getUserName()),"用户名不能为空"); //用户密码AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"用户密码不能为空"); //用户是否存在User temp = usermapper.selectUserByName(user.getUserName()); AssertUtil.isTrue(temp!=null,"用户已经存在"); //用户添加是否成功AssertUtil.isTrue(usermapper.insertUser(user)<1,"添加失败了"); }//修改操作/***** 用户名不为空* 用户密码不为空* 用户不存在* 用户修改操作失败** @param user*/@Overridepublic void changeUser(User user) {AssertUtil.isTrue( StringUtils.isBlank( user.getUserName() ),"用户名不为空" ); AssertUtil.isTrue( StringUtils.isBlank( user.getUserPwd() ),"密码不为空" ); User temp = usermapper.selectuserbyid(user.getUserId()); AssertUtil.isTrue( temp == null,"用户不存在"); AssertUtil.isTrue( usermapper.updateUser(user)<1,"修改失败了"); }/***** 删除是的id是否还存在* 删除失败* @param userId*/@Overridepublic void removeUserById(Integer userId) {AssertUtil.isTrue( userId==null|| null==usermapper.selectuserbyid( userId ),"待删除数据不存在" ); AssertUtil.isTrue( usermapper.deleteUserById( userId )<1,"删除失败" ); }/*** 分页参数传递了两个变量 一个是起始位置 一个是一页有多行数据** @param userQuery* @return*/@Overridepublic PageInfoqueryUserByPage(UserQuery userQuery) {PageHelper.startPage( userQuery.getPageNum(),userQuery.getPageSize()); List ulist = usermapper.selectUserByItem( userQuery ); PageInfo plist =new PageInfo (ulist); return plist; }}
文章图片
新增类 用来校验数据数据是否有误
AssertUtil
public class AssertUtil {/**** @param flag 参数* @param msg 信息*flag=true*抛出异常,自定义*/public static void isTrue(Boolean flag,String msg){if(flag){throw new ParamException(msg); }}}
ParamException
自定义异常类public class ParamException extends RuntimeException{private Integer code=300; private String msg="参数异常"; public ParamException() {super("参数异常"); }public ParamException(Integer code) {super("参数异常"); this.code=code; }public ParamException(String msg) {super(msg); this.msg=msg; }public ParamException(Integer code,String msg) {super(msg); this.msg=msg; this.code=code; }public Integer getCode() {return code; }public void setCode(Integer code) {this.code = code; }public String getMsg() {return msg; }public void setMsg(String msg) {this.msg = msg; }}
文章图片
把数据状态码 状态数据存在在
Resultinfo
所创建的对象中ResultInfo
public class ResultInfo {private Integer code=200; private String msg="操作成功"; private Object object; public ResultInfo() {}public Integer getCode() {return code; }public void setCode(Integer code) {this.code = code; }public String getMsg() {return msg; }public void setMsg(String msg) {this.msg = msg; }public Object getObject() {return object; }public void setObject(Object object) {this.object = object; }@Overridepublic String toString() {return "ResultInfo{" +"code=" + code +", msg='" + msg + '\'' +", object=" + object +'}'; }}
更新传递的事
json
数据:文章图片
【springboot-curd基于mybatis项目搭建】到此这篇关于springboot-curd基于mybatis项目搭建的文章就介绍到这了,更多相关springboot-curd基于mybatis项目 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- Vue|基于vue+srpingboot的学生成绩管理系统
- 基于mybatis一对多查询内层排序的问题
- 深度学习(基于pytorch)|深度学习笔记(七)——pytorch数据处理工具箱(一)
- Spring|Spring如何创建复杂对象以及Spring整合Mybatis思路分析
- 基于SpringBoot框架和VUE的求职招聘系统
- 基于消息队列(RabbitMQ)实现延迟任务
- 基于Kubernetes/K8S构建Jenkins持续集成平台(上)-2
- ElasticSearch内部基于_version乐观锁控制机制
- 分布式|基于Docker搭建 ELK分布式日志管理解决方案
- FaE(基于符号知识的适应性和可解释的神经记忆)