数据库的操作
- 首先增加依赖,在pom.xml中增加以下依赖
<!-- JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
- 配置数据库
修改application.yml
datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/dbgirl username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true
- 编写实体类
package com.example.demo2; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Girl { @Id @GeneratedValue private int id; private String userName; private String password; private Integer age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
- JpaRepository 引入
package com.example.demo2; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface GirlRepo extends JpaRepository<Girl,Integer> { public List<Girl> findByAge(Integer age); }
- 编写CURD
package com.example.demo2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class IndexController { @Autowired private GirlRepo girlRepo; /** * 查询所有数据 * @return */ @GetMapping(value = "/findAllGirl") public List<Girl> findAllGirl(){ return girlRepo.findAll(); } /** * 添加一个girl * @param userName * @param password * @param age * @return */ @PostMapping(value = "/addGirl") public Girl addGirl(@RequestParam("userName") String userName,@RequestParam("password") String password,@RequestParam("age") Integer age){ Girl girl = new Girl(); girl.setAge(age); girl.setUserName(userName); girl.setPassword(password); return girlRepo.save(girl); } /** * 删除一个girl * @param id */ @DeleteMapping("/delGirl") public void delGirl(@RequestParam("id") Integer id){ girlRepo.delete(id); } /** * 查询一个girl * @param id * @return */ @GetMapping("/getOneGirl") public Girl getOneGirl(@RequestParam("id") Integer id){ return girlRepo.findOne(id); } /** * 更新girl * @param id * @param userName * @param password * @param age * @return */ @PutMapping("/updateGirl") public Girl updateGirl(@RequestParam("id") Integer id, @RequestParam(value = "userName",required = false) String userName, @RequestParam(value = "password",required = false) String password, @RequestParam(value = "age",required = false) Integer age){ Girl girl = new Girl(); girl.setId(id); girl.setUserName(userName); girl.setPassword(password); girl.setAge(age); return girlRepo.save(girl); } /** * 通过年龄查找 * @param age */ @GetMapping("/findGirlByAge") public List<Girl> findGirlByAge(@RequestParam("age") Integer age){ return girlRepo.findByAge(age); } }