SpringBoot连接MySql

 时间:2024-10-12 22:40:28

1、新建Spring Boot项目,依赖选择JPA(spring-boot-starter-data-jpa)和Web(spring-bootstarter-web)。

SpringBoot连接MySql

3、定义映射实体类

SpringBoot连接MySql

4、定义Controller类@RestControllerpublic class PersonCtroller { @Autowir髫潋啜缅ed PersonServer personServer; @RequestMapping("/rollback") public Person rollback(Person person){ return personServer.savePersonWithRollBack(person); } @RequestMapping("/norollback") public Person noRollback(Person person){ return personServer.savePersonWithOutRollBack(person);}}

5、定义数据访问层public interface PersonRepository extends JpaRepository<Person, Long> {}

6、定义Server层@Servicepublic cla衡痕贤伎ss PersonServerImp implements PersonServer { @Autowired PersonRepository personRepository; @Transactional(rollbackFor = {IllegalArgumentException.class}) @Override public Person savePersonWithRollBack(Person person) { Person p = personRepository.save(person); if (p.getName().equals("xxx")){ throw new IllegalArgumentException("用户已存在,数据会回滚"); } return p; } }

7、浏览器访问

SpringBoot连接MySql
  • Microsoft Word 2010如何删除已有的页码
  • 神仙道乾坤套装材料大全 100级紫装材料掉落出处
  • 网址大全夜间模式亮度怎么调到最弱
  • 雨雪天气建筑施工需要注意什么
  • 热门搜索
    孔向东蒲剧大全 寒冰射手怎么玩 生日贺词大全 鱼的做法大全家常 成人网大全 柳宗元简介 年糕的做法大全 7座商务车大全 周记50字大全 香港恐怖片大全