org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.slf4j
slf4j-api
application.yml
spring:
profiles:
active: a
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_person?useSSL=false
username: root
password: root
jpa:
hibernate:
ddl-auto: update # 自动更新
show-sql: true # 控制台打印SQl语句
/**
* 使用@Entity注解,创建数据表
*/
@Entity
public class Person {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
//必须要有构造函数
public Person() {
}
---省略getter和setter方法---
}
public interface PersonRepository extends JpaRepository {
/**
*
* @param age
* @return 通过年龄查找返回一个列表
*/
public List findByAge(Integer age);
/**
*
* @param id
* @return 通过id查询返回一个对象
*/
public Person findById(Integer id);
}
/**
* Created by HP on 2018/7/30.
*/
@RestController
public class PersonController {
private final static Logger logger = LoggerFactory.getLogger(PersonController.class);
@Autowired
private PersonRepository personRepository;
@Autowired
private PersonService personService;
/**
* 1.查询列表
*/
@GetMapping(value = "/find/personList")
public List findAllPerson() {
logger.info("查询列表");
return personRepository.findAll();
}
/**
* 2.添加一位员工信息
*/
@PostMapping(value="/add/onePerson")
public Person addPerson(@RequestParam("name") String name, @RequestParam("age") Integer age){
logger.info("添加一位员工信息");
Person person = new Person();
person.setName(name);
person.setAge(age);
return personRepository.save(person);
}
/**
* 3.查询一个人
*/
@GetMapping(value = "/person/{id}")
public Person findPerson(@PathVariable("id") Integer id){
logger.info("查询一个人");
//使用此方法
return personRepository.findById(id);
}
@GetMapping(value = "/person/age/{age}")
public List findListPerson(@PathVariable("age") Integer age){
logger.info("通过年龄查询");
return personRepository.findByAge(age);
}
/**
* 4.删除一条记录
*/
@DeleteMapping(value = "/delete/person/{id}")
public void delPerson(@PathVariable("id") Integer id){
logger.info("删除一条记录");
personRepository.delete(id);
}
/**
* 5.更新数据
*/
@PostMapping(value = "/update/person/{id}")
public Person updatePerson(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age){
logger.info("更新个人数据");
Person person=new Person();
person.setId(id);
person.setName(name);
person.setAge(age);
return personRepository.save(person);
}
}
由于Spring Boot默认是开启事务的,所有我们只需要在自己数据库操作上添加@Transactional注解即可!
注:在Service层添加事务注解@Transactional
PersonService.java
@Service
public class PersonService {
@Autowired
private PersonRepository personRepository;
/**
* Spring boot开启事务只需要添加@Transactional即可!
*
*/
@Transactional
public void insertTwo() {
Person person1 = new Person();
person1.setAge(18);
person1.setName("李斯");
personRepository.save(person1);
//将数据库字段设置为2个字节,超过就报错
Person person2 = new Person();
person2.setAge(28);
person2.setName("曹操666");
personRepository.save(person2);
}
}
参考:SpringBoot事务注解@Transactional
org.springframework.boot
spring-boot-starter-aop
AspectDemo.java
@Aspect //声明一个切面
@Component
public class AspectDemo {
//定义切点
@Pointcut("execution(* com.wang.controller.*.*(..))")
public void qieru(){
}
//在切点前执行以下代码
@Before("qieru()")
public void qianzhi(){
System.out.println("前置通知");
}
@After("qieru()")
public void houzhi(){
System.out.println("后置通知");
}
@AfterReturning("qieru()")
public void qianzhifanhui(){
System.out.println("后置返回 ");
}
@AfterThrowing("qieru()")
public void qianzhiYichang(){
System.out.println("后置异常");
}
@Around("qieru()")
public void huanrao(ProceedingJoinPoint poin) throws Throwable {
System.out.println("环绕通知"+logger.getClass().getName());
poin.proceed();
}
}
/**
* Created by HP on 2018/7/30.
*/
@RestController
public class PersonController {
private final static Logger logger = LoggerFactory.getLogger(PersonController.class);
@Autowired
private PersonRepository personRepository;
@Autowired
private PersonService personService;
/**
* 1.查询列表
*/
@GetMapping(value = "/find/personList")
public List findAllPerson() {
logger.info("查询列表");
return personRepository.findAll();
}
/**
* 2.添加一位员工信息
*/
@PostMapping(value="/add/onePerson")
public Person addPerson(@RequestParam("name") String name, @RequestParam("age") Integer age){
logger.info("添加一位员工信息");
Person person = new Person();
person.setName(name);
person.setAge(age);
return personRepository.save(person);
}
/**
* 3.查询一个人
*/
@GetMapping(value = "/person/{id}")
public Person findPerson(@PathVariable("id") Integer id){
logger.info("查询一个人");
//使用此方法
return personRepository.findById(id);
}
@GetMapping(value = "/person/age/{age}")
public List findListPerson(@PathVariable("age") Integer age){
logger.info("通过年龄查询");
return personRepository.findByAge(age);
}
/**
* 4.删除一条记录
*/
@DeleteMapping(value = "/delete/person/{id}")
public void delPerson(@PathVariable("id") Integer id){
logger.info("删除一条记录");
personRepository.delete(id);
}
/**
* 5.更新数据
*/
@PostMapping(value = "/update/person/{id}")
public Person updatePerson(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age){
logger.info("更新个人数据");
Person person=new Person();
person.setId(id);
person.setName(name);
person.setAge(age);
return personRepository.save(person);
}
}