全局异常与mybatisplus

  • 全局异常处理
    当前端发生异常时,前端用户无法处理
@DeleteMapping("/{id}")
public SysResult deleteUserById(@PathVariable Integer id){
    try{
        userService.deleteUserById(id);
        return SysResult.success();
    }catch (Exception e){
        e.printStackTrace();
        return SysResult.fail();
    }
}
  • Spring的全局异常处理机制
    说明:如果代码中加了大量的try-catch,则不便于维护
    要求:返回统一的业务数据Sysresult,返回值都是json串
    拦截:指定遇到某种异常实现AOP处理(Advice,通知)
    特点:只拦截Controller层抛出异常
//标识全局异常处理机制
@RestControllerAdvice
public class SystemExe {
    @ExceptionHandler({RuntimeException.class})
    public SysResult fail(Exception e){
        e.printStackTrace();
        return SysResult.fail();
    }
}
  • 事务控制
    事务特性: 一致性、原子性、隔离性、持久性
    Spring中的默认事务策略:
    解决方法:方法前加注解@Transactional
    作用:1.默认只拦截运行时异常;2.注解后的属性,rollbackFor:指定异常的类型回滚;noRollbackFor:指定异常不回滚
//依旧会删除成功
@DeleteMapping("/{id}")
@Transactional
public SysResult deleteUserById(@PathVariable Integer id){
    userService.deleteUserById(id);
    int a=1/0;
    return SysResult.success();
}
  • MybatisPlus
    mybatis:半自动化的ORM映射框架,sql手写,结果集映射自动
    mybatisplus:全自动的ORM映射框架,是mybatis的增强
    Spring+MybatisPlus
    pojo:
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@TableName("demo_user")
public class User implements Serializable {
    @TableId(type = IdType.AUTO)//主键自增
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
}

usermapper:

public interface UserMapper extends BaseMapper<User> {
    @Select("select * from demo_user")
    List<User> findAll();
}

applicaion.yml:

mybatis-plus:
  type-aliases-package: com.jt.pojo
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true
  • SpringBoot测试API
    需要在某个位置使用Spring容器中的对象
    效果: 只要@Test测试方法执行,则整个Spring容器启动,以实现依赖注入
    注意: 1.只能在测试类中使用;2.测试类的包路径必须与主启动类相同
@SpringBootTest
class SpringbootSsmApplicationTests {
    @Autowired
    private UserMapper userMapper;
    @Test
    void contextLoads() {
        userMapper.findAll();
    }
    @Test
    void insertUser(){
        User u = new User(null,"ZziQon",10,"男");
        userMapper.insert(u);
    }
}
  • MybatisPlus实例
    调用步骤:
    1 用户执行userMapper.insert(user);
    2 根据继承的关系 BaseMapper.insert(user).
    3 MP在内部生成Sql之后交给Mybatis调用最终实现数据操作.
    QueryWrapper:条件构造器,即where后面的语句,默认的关系连接符为and
    调用关键字:
    gt:>;lt:<;eq:=;ge:>=;le:<=;ne:<>;like:首尾模糊;likeLeft:首部模糊
    in:多值传递;orderByDesc:降序排
@Test
void selectById(){
    User user = userMapper.selectById(1);
    System.out.println(user);
}

@Test
void selectByNS(){
    User user = new User();
    user.setSex("男").setName("ZziQon");
    //QueryWrapper:条件构造器,即where后面的语句,默认的关系连接符为and
    QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
    List<User> userList = userMapper.selectList(queryWrapper);
    for (User u:userList) {
        System.out.println(u);
    }
}
@Test
void selectByNS2(){
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("name", "ZziQon").eq("Sex", "男");
    List<User> userList = userMapper.selectList(queryWrapper);
    for (User u:userList) {
        System.out.println(u);
    }
}
@Test
void selectByIdDesc(){
    //基本类型
    Integer[] ids = {1,3,4,5};
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.in("id", ids).orderByDesc("age");
    List<User> userList = userMapper.selectList(queryWrapper);
    for (User u:userList) {
        System.out.println(u);
    }
}

你可能感兴趣的:(spring,java,restful)