ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

[springboot笔记三]解释相关创建文件-后端

[springboot笔记三]解释相关创建文件-后端

一、application.yaml

1.1 在application.yaml文件中设置端口为9090

server:port:9090

1.2 用官方spring链接数据库

spring:datasource:driver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://${ip}:3306/base?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=trueusername:rootpassword:rootip:127.0.0.1

1.3通过控制台查看返回结果 `标记1

本质:控制台中打印log日志
mybatis-plus:mapper-locations:classpath:mapper/*.xmlconfiguration:log-impl:org.apache.ibatis.logging.stdout.StdOutImpl

二、WebController

@RestController@RequestMapping("/")publicclassWebController{}

@RequestMapping(“/”):WebController接口前置


application.yaml文件中设置端口为9090

server:port:9090

@RestController@RequestMapping("/web")publicclassWebController{@GetMapping("/hello")privateStringhello(){return"Hello World";}}

后端地址:访问localhost:9090/web/hello 返回Hello World




test1:先引入接口mapper

@ResourceprivateUserMapperuserMapper;

2.1 模糊查询基于Mybatis-plus

@GetMapping("/1/{keyword}")privateList<User>QueryGetuser(@PathVariableStringkeyword){LambdaQueryWrapper<User>queryWrapper=newLambdaQueryWrapper<>();queryWrapper.like(User::getUsername,keyword);returnuserService.list(queryWrapper);}

2.2 ID查询基于Mybatis-plus

@GetMapping("/{id}")privateUsergetUserId(@PathVariableIntegerid){returnuserService.getById(id);}

三、User(实现类)

映射数据库的表

@TableName(value="sys_user")//表名称publicclassUser{@TableId(value="id",type=IdType.AUTO)//标明主键,自增属性privateIntegerid;privateStringusername;privateStringpassword;privateStringnickname;privateStringavatarUrl;}
利用mybatis映射数据库表中的每一个数据
PS:下划线的遵循驼峰命名法,即大写

四、UserMapper(接口interface)

@Select("select * from sys_user") List<User> getUserlist();
此方法在WebController中直接返回为两个空数组
在控制台中打印查看类型

List<User>userlist=userMapper.getUserlist();System.err.println(userlist);returnnull;


发现其返回类型为地址

目的:变为data数据
手段1:generate重写User实现类中的tostring()方法
@OverridepublicStringtoString(){return"User{"+"id="+id+", username='"+username+'\''+", password='"+password+'\''+", nickname='"+nickname+'\''+", avatarUrl='"+avatarUrl+'\''+'}';}

手段2:引入依赖lombok

自动重构 to string,get,set方法
实现类前加上@Data

@Data@TableName(value="sys_user")publicclassUser{@TableId(value="id",type=IdType.AUTO)privateIntegerid;privateStringusername;privateStringpassword;privateStringnickname;privateStringavatarUrl;}

此时WebController中返回的能正常显示数据
return userMapper.getUserlist();

五、MybatisPlusConfig

@Configuration@MapperScan("com.example.springboot.mapper")publicclassMybatisPlusConfig{@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptorinterceptor=newMybatisPlusInterceptor();interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));returninterceptor;}
使用MapperScan扫描mapper文件,引入MyBatis分页

六、UserServiceImpl(实现类) IUserService(接口)

IUserService: 接口声明我要提供哪些业务方法,不写具体实现。

UserServiceImpl: 必须实现接口里定义的所有方法。

七、Result | Constants (Interface)

Result

importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;/** * 接口统一返回包装类 */@Data@NoArgsConstructor@AllArgsConstructorpublicclassResult{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);}}

Constants

publicinterfaceConstants{StringCODE_200="200";// 请求成功StringCODE_401="401";// 权限不足StringCODE_400="400";// 参数错误StringCODE_500="500";// 系统错误StringCODE_605="605";// 业务异常}

统一改造WebController中return返回类型为Result

@GetMapping("/list")privateResultlist(){returnResult.success(userService.list());}@GetMapping("/{id}")privateResultgetUserId(@PathVariableIntegerid){returnResult.success(userService.getById(id));}@GetMapping("/1/{keyword}")privateResultQueryGetuser(@PathVariableStringkeyword){LambdaQueryWrapper<User>queryWrapper=newLambdaQueryWrapper<>();queryWrapper.like(User::getUsername,keyword);returnResult.success(userService.list(queryWrapper));}@DeleteMapping("/delete/{id}")privateResultdeleteUser(@PathVariableIntegerid){returnResult.success(userService.removeById(id));}
返回列表