首页 > 编程知识 正文

spring rest注解,@restcontroller注解

时间:2023-05-05 06:22:54 阅读:209146 作者:2315

由于SpringBoot底层还是Spring所以只要在入口类中使用注解@EnableTransationManagement开启事务
在访问数据库的Service函数上添加注解@Transaction即可

在main函数中: @MapperScan(basePackages = "com.example.demo.dao")@EnableTransactionManagement@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }} 在Service中的实现类: @Transactional @Override public int insertOneStudentInfo(Student student) { return studentDao.insertStudentInfo(student); }

注意:@Transactional注解在异常发生或者异常发生之前使用都生效,当使用try……catch捕获异常的时候由于抛出的异常已经被try…catch捕获了所以@Transactional无法检测到异常事务就不会生效,还有注意的点是当没有使用@Transactional注解的时候,异常在Service层调用Dao层抛出的时候程序会中断,就是Dao层对数据库操作的程序不会被执行

@RestController注解是Spring4新增的注解,是@Controller和@ResponseBody的组合注解,如果一个Controller类添加了@RestController注解,那么该类下的所有函数都添加了@ReponseBody注解,该注解用于返回字符串和JSON数据

注意的是@RestController注解是不能跳转到页面的,它只能返回字符串或者JSON数据

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。