项目地址: Tongering
Spring boot中利用Maven添加依赖
选择Pom.xml
,在dependencies中添加一行行依赖即可
Spring boot 项目中操控数据的类结构
- 结构:
- pojo:实例化每张表
- mapper:实现针对单张表的CURD
- server:调用mapper实现对多张表的CURD
- controller:调用相关server
在Mapper下的接口中
- 加@Mapper注解
可以在spring boot启动中不写mapperscan
- 不加@Mapper注解
在spring boot启动中写mapperscan以及其位置@MapperScan("com.tongering.mybatislearning.mapper")
get/post
@GetMapping("/user/all/")
即该访问(get方法)链接可以访问紧接着的方法
@GetMapping("/user/all/")
public List<User> getAll(){
return userMapper.selectList(null);
}
@PostMapping("user/id/")
发送Post请求即可访问紧接着的方法
@RequestMapping("user/person/")
无论post或者get都可以访问紧接着的方法
获取链接参数
@PathVariable与@PathParam的区别
@PathVariable
获取链接中的位置的参数,即http://localhost:3000/blog/1,即获取1,@PathVariable String blog
@PathParam
获取链接中指定的参数http://localhost:3000/blog/?id=1,即若想获取id则可得到1,@PathParam ("id")
Spring Security
-
只要依赖中加入
Spring Security
整个Spring boot所有接口都需要登录才可以使用 -
未接入数据库时,用户名为
user
,密码为临时生成的BCryptPasswordEncoder
类似长这样$2a$10$daOoTMUh3yhsU8BcGGZG8O4mN8/Zyv61Ax5ZDo0tnVyEfoor9YK8e
相同字符串生成出来的密码都不一样,所以用到passwordEncoder.matches(未加密密码,加密密码)
-
对接数据库则创建一个
UserDetailsServiceImpl
的类,并继承UserDetailsService
,向UserDetailsImpl
传入user -
创建一个工具类
UserDetailsImpl
,并implement method即可,将大量false
修改成true
,密码以及用户名则通过pojo类来获取 -
数据库读取的密码必须是加密的,简单粗暴密码前面加
{noop}
或者直接把加密的密码放置进去即可