![[springboot笔记三]解释相关创建文件-后端](http://pic.xiahunao.cn/yaotu/[springboot笔记三]解释相关创建文件-后端)
一、application.yaml1.1 在application.yaml文件中设置端口为9090server:port:90901.2 用官方spring链接数据库spring:datasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://${ip}:3306/base?useSSLfalseuseUnicodetruecharacterEncodingUTF-8serverTimezoneGMT%2b8allowPublicKeyRetrievaltrueusername:rootpassword:rootip:127.0.0.11.3通过控制台查看返回结果 标记1本质:控制台中打印log日志mybatis-plus:mapper-locations:classpath:mapper/*.xmlconfiguration:log-impl:org.apache.ibatis.logging.stdout.StdOutImpl二、WebControllerRestControllerRequestMapping(/)publicclassWebController{}RequestMapping(“/”)WebController接口前置在application.yaml文件中设置端口为9090server:port:9090RestControllerRequestMapping(/web)publicclassWebController{GetMapping(/hello)privateStringhello(){returnHello World;}}后端地址:访问localhost:9090/web/hello 返回Hello Worldtest1:先引入接口mapperResourceprivateUserMapperuserMapper;2.1 模糊查询基于Mybatis-plusGetMapping(/1/{keyword})privateListUserQueryGetuser(PathVariableStringkeyword){LambdaQueryWrapperUserqueryWrappernewLambdaQueryWrapper();queryWrapper.like(User::getUsername,keyword);returnuserService.list(queryWrapper);}2.2 ID查询基于Mybatis-plusGetMapping(/{id})privateUsergetUserId(PathVariableIntegerid){returnuserService.getById(id);}三、User(实现类)映射数据库的表TableName(valuesys_user)//表名称publicclassUser{TableId(valueid,typeIdType.AUTO)//标明主键,自增属性privateIntegerid;privateStringusername;privateStringpassword;privateStringnickname;privateStringavatarUrl;}利用mybatis映射数据库表中的每一个数据PS:下划线的遵循驼峰命名法,即大写四、UserMapper(接口interface)Select(select * from sys_user) ListUser getUserlist();此方法在WebController中直接返回为两个空数组在控制台中打印查看类型ListUseruserlistuserMapper.getUserlist();System.err.println(userlist);returnnull;发现其返回类型为地址目的:变为data数据手段1:generate重写User实现类中的tostring()方法OverridepublicStringtoString(){returnUser{idid, usernameusername\, passwordpassword\, nicknamenickname\, avatarUrlavatarUrl\};}手段2:引入依赖lombok自动重构 to string,get,set方法在实现类前加上DataDataTableName(valuesys_user)publicclassUser{TableId(valueid,typeIdType.AUTO)privateIntegerid;privateStringusername;privateStringpassword;privateStringnickname;privateStringavatarUrl;}此时WebController中返回的能正常显示数据即return userMapper.getUserlist();五、MybatisPlusConfigConfigurationMapperScan(com.example.springboot.mapper)publicclassMybatisPlusConfig{BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptorinterceptornewMybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));returninterceptor;}使用MapperScan扫描mapper文件,引入MyBatis分页六、UserServiceImpl(实现类) IUserService(接口)IUserService: 接口声明我要提供哪些业务方法不写具体实现。UserServiceImpl: 必须实现接口里定义的所有方法。七、Result | Constants (Interface)Resultimportlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;/** * 接口统一返回包装类 */DataNoArgsConstructorAllArgsConstructorpublicclassResult{privateStringcode;privateStringmsg;privateObjectdata;publicstaticResultsuccess(){returnnewResult(Constants.CODE_200,,null);}publicstaticResultsuccess(Objectdata){returnnewResult(Constants.CODE_200,,data);}publicstaticResulterror(){returnnewResult(Constants.CODE_500,系统错误,null);}publicstaticResulterror(Stringcode,Stringmsg){returnnewResult(code,msg,null);}}ConstantspublicinterfaceConstants{StringCODE_200200;// 请求成功StringCODE_401401;// 权限不足StringCODE_400400;// 参数错误StringCODE_500500;// 系统错误StringCODE_605605;// 业务异常}统一改造WebController中return返回类型为ResultGetMapping(/list)privateResultlist(){returnResult.success(userService.list());}GetMapping(/{id})privateResultgetUserId(PathVariableIntegerid){returnResult.success(userService.getById(id));}GetMapping(/1/{keyword})privateResultQueryGetuser(PathVariableStringkeyword){LambdaQueryWrapperUserqueryWrappernewLambdaQueryWrapper();queryWrapper.like(User::getUsername,keyword);returnResult.success(userService.list(queryWrapper));}DeleteMapping(/delete/{id})privateResultdeleteUser(PathVariableIntegerid){returnResult.success(userService.removeById(id));}