使用@Data注解,不用再写get、set方法
使用@ToString注解,不用再写tostring方法。
使用@NoArgsConstructor注解,不用再写无参构造方法。
使用@AllArgsConstructor注解,不用再写有参构造方法。
@TableName
@TableId
@TableField
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("sys_user")//数据库表(spring boot)
public class User {
@TableId(value="id",type = IdType.AUTO)//数据库主键(spring boot)
private Integer id;
private String username;
//前端返回数据不显示密码
@JsonIgnore
private String password;
private String nickname;
private String email;
private String phone;
private String address;
@TableField(value = "avatar_url")//数据库字段名(spring boot)
private String avatar;//实体变量名
}
1.2、对返回给前端的数据进行处理
1、@JsonIgnore
作用:前端返回数据不显示密码
@JsonIgnore private String password;二、Controller 2.1、与容器相关的注解
1、@RestController
添加位置:Controller类上
作用:相当于@Controller+@ResponseBody两个注解的结合,告诉容器Controller,返回json数据;不需要在方法前面加@ResponseBody注解了
但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面
1、@Autowired
作用:自动注入组件
实例
@Autowired UserService userService;2.3、与请求相关的注解
1、@RequestMapping("/user")
2、@PostMapping
3、@GetMapping
4、@DeleteMapping
5、@PutMapping
1、@RequestBody:将前端传来的json转换成Java对象
//使用@RequestBody将前端传来的json转换成Java对象
@PostMapping
public Integer save(@RequestBody User user){
//新增或者更新
return userService.save(user);
}
2、@PathVariable 获取url以/传递来的参数,例如http://localhost:8181/user/31
@DeleteMapping("/{id}")
public int deleteById(@PathVariable Integer id){
return userMapper.deleteById(id);
}
3、@RequestParam:注解获取的是url以?传递来的参数的值:/user/page?pageNum=1&pageSize=10里面的数据
//区别于@PathVariable
//RequestParam注解获取的是:/user/page?pageNum=1&pageSize=10里面的数据
@GetMapping("/page")
public Map findPage(
@RequestParam Integer pageNum,
@RequestParam Integer pageSize
){
pageNum = (pageNum-1)*pageSize;
List all=userMapper.selectPage(pageNum,pageSize);
Integer total = userMapper.selectTotal();
Map res = new HashMap<>();
res.put("data",all);
res.put("total",total);
return res;
}
三、Service
3.1、与容器相关的注解
1、@Service
作用:告诉容器Service
添加位置:Service类上
1、 @Autowired
@Autowired UserMapper userMapper;四、Dao/Mapper 4.1、与容器相关的注解
1、@Mapper
作用:告诉容器Dao/Mapper
添加位置:Dao/Mapper类上
@Insert
@Delete
@Update
@Select
实例
//删除
@Delete("delete from sys_user where id = #{id}")
int deleteById(@Param("id") Integer id);
五、Config配置类
@Configuration
@Bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlbasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
// 当前跨域请求最大有效时长。这里默认1天
private static final long MAX_AGE = 24 * 60 * 60;
@Bean
public CorsFilter corsFilter() {
UrlbasedCorsConfigurationSource source = new UrlbasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("http://localhost:8080"); //配置为*都可以访问 1 设置访问源地址
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
corsConfiguration.setMaxAge(MAX_AGE);
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
六、日志使用



