
配置基本属性 在application.properties里配置数据源和jpa的相关属性
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true
定义映射实体类
定义Controller类
@RestControllerpublic class PersonCtroller {
@AutowiredPersonServer personServer
@RequestMapping("/rollback")
public Person rollback(Person person){
return personServer.savePersonWithRollBack(person)
}
@RequestMapping("/norollback")
public Person noRollback(Person person){
return personServer.savePersonWithOutRollBack(person)
}
}
定义数据访问层
public interface PersonRepository extends JpaRepository<Person, Long>{}
定义Server层
@Servicepublic class 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
}
}
找构造方法->创建对象->依赖注入->初始化前(@PostConstruct)->初始化(afterPropertiesSet)->初始化后->放入单例map->bean
1、找构造方法说明:
只有一个构造方法,就直接调用
多个构造方法选无参构造方法,没有则报错
没有无参构造方法,找唯一一个使用@AutoWired注解的构造方法,多个则报错
2、初始化前
2、初始化
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)