PersonController 是接受请求并使用对应方法来处理请求操作;
PersonRepository 这里使用了JPA接口,JPA封装了底层的数据库操作,PersonRepository使用JPA声明的方法就可以直接进行数据库操作;
Person 是实体类,这里进行数据类型声明,实现get(),set()方法,类名要与对应的数据库表名一致;
application.properties 是配置数据库连接,相对于spring,springboot会直接读取这里的配置连接数据库,但不适用于多数据源。
pom.xml 在这里添加依赖,导入对应jar包
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-data-jpa</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-jdbc</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 15 <dependency> 16 <groupId>mysql</groupId> 17 <artifactId>mysql-connector-java</artifactId> 18 <scope>runtime</scope> 19 </dependency> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-test</artifactId> 23 <scope>test</scope> 24 </dependency> 25 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-configuration-processor</artifactId> 29 <optional>true</optional> 30 </dependency> 31 </dependencies> 32
注意:这里的localhost:3306是你的数据库连接,test是数据库名
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root
另外,这里使用的@Entity 是声明映射Person这个实体类,@Id标注用于声明一个实体类的属性映射为数据库的主键列。,@GeneratedValue用于标注主键的生成策略
注意:因为这里使用了@Id标签,所以进行数据添加操作时,不能随意创建记录
1 package com.example.ademo.Person; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 10 @Entity 11 public class Person { 12 @Id 13 @GeneratedValue 14 private int id; 15 private int name; 16 17 public Person(){ 18 19 } 20 public Person(int id,int name){ 21 this.id = id; 22 this.name = name; 23 } 24 25 public int getId() { 26 return id; 27 } 28 29 public void setId(int id) { 30 this.id = id; 31 } 32 33 public int getName() { 34 return name; 35 } 36 37 public void setName(int name) { 38 this.name = name; 39 } 40 }
1 package com.example.ademo.dao; 2 3 import com.example.ademo.Person.Person; 4 import org.springframework.data.jpa.repository.JpaRepository; 5 6 public interface PersonRepository extends JpaRepository<Person,Integer> { 7 }
1 package com.example.ademo.controller; 2 3 @RestController 4 public class PersonController { 5 @Autowired 6 private PersonRepository personRepository; 7 8 9 //查看全部 10 @RequestMapping(value="/hello",method = RequestMethod.GET) 11 public void sau(HttpServletResponse response) throws IOException { 12 13 List<Person> list = personRepository.findAll(); 14 Map<Integer,Integer> map = new HashMap<Integer,Integer>(); 15 for(Person p:list) 16 map.put(p.getId(),p.getName()); 17 ObjectMapper json = new ObjectMapper(); 18 String ss = json.writeValueAsString(map); 19 PrintWriter out = response.getWriter(); 20 out.print(ss); 21 out.flush(); 22 out.close(); 23 } 24 //添加一个 25 @PostMapping(value = "/hello") 26 public Person addPerson(@RequestParam("id") Integer id,@RequestParam("name") Integer name){ 27 Person person = new Person(); 28 person.setId(id); 29 person.setName(name); 30 return personRepository.save(person); 31 } 32 @GetMapping(value = "/hello/add") 33 public Person addPerson1(@RequestParam("id") Integer id,@RequestParam("name") Integer name){ 34 Person person = new Person(); 35 person.setId(id); 36 person.setName(name); 37 return personRepository.save(person); 38 } 39 40 41 //修改一个 42 @PutMapping(value = "/hello/{id}") 43 public Person updatePerson(@PathVariable("id") Integer id,@RequestParam("name") Integer name){ 44 Person person = new Person(); 45 person.setId(id); 46 person.setName(name); 47 return personRepository.save(person); 48 } 49 @GetMapping(value = "/hello/upd") 50 public Person updPerson(@RequestParam("id") Integer id,@RequestParam("name") int name){//@PathVariable("id") Integer id){ 51 Person person = findOne(id); 52 person.setName(name); 53 return personRepository.save(person); 54 } 55 56 //查找一个 57 @GetMapping(value = "/hello/{id}") 58 public Person findOne(@PathVariable("id") Integer id){ 59 return personRepository.findById(id).orElse(null); 60 } 61 62 //删除 63 @GetMapping(value = "/hello/del/{id}") 64 public void deletePerson(@PathVariable("id") Integer id){ 65 Person person = findOne(id); 66 personRepository.delete(person); 67 } 68 }
查看全部数据
查看ID是3的数据
添加一个ID是5的记录
把name改成99
删除id是3的数据
ok,没有问题
springboot 整合mybatis 进行数据库操作(JPA)
原文:https://www.cnblogs.com/sanfenzZ/p/9424518.html