JavaEE進(jìn)階知識學(xué)習(xí)-----SpringBoot基礎(chǔ)知識-4-數(shù)據(jù)庫知識

下面學(xué)習(xí)的是數(shù)據(jù)庫相關(guān)知識

數(shù)據(jù)庫使用的是MySQL,持久化技術(shù)使用的就是spring-data-jpa,RESTFul API如下


image

1.pom文件中添加依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2.application.yml文件中配置數(shù)據(jù)庫信息和jpa

spring:
  profiles:
    active: prod
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dbspringboot
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true

其中的 ddl-auto: create表示每次都會刪除原先存在的表,就是說如果表中存在數(shù)據(jù),運(yùn)行程序數(shù)據(jù)就不存在了。
也可以是 update:會創(chuàng)建表,如果表中有數(shù)據(jù),不會刪除表。

3.創(chuàng)建一個實(shí)體類User,如下所示

@Entity
public class User {
    @Id
    @GeneratedValue
    private Integer id;

    private String userName;

    private Integer age;

    public User(){

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

當(dāng)我們再次啟動程序的時候時候,dbspringboot數(shù)據(jù)庫中就會多一個user表,其中表字段就是實(shí)體類所對應(yīng)的字段,這是jpa相關(guān)的知識,具體將在以后仔細(xì)學(xué)習(xí)和記錄。

4.創(chuàng)建UserController

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping(value = "/users")
    public List<User> userList(){
        return userRepository.findAll();
    }
}

5.UserRepository接口的代碼如下

public interface UserRepository extends JpaRepository<User,Integer> {
}

6.訪問

使用http://localhost:8082/gire/users訪問,如下:

image

7.restful風(fēng)格的完整代碼如下:

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    /**
     * 查詢用戶列表
     * @return
     */
    @GetMapping(value = "/users")
    public List<User> userList(){
        return userRepository.findAll();
    }

    /**
     * 添加一個用戶
     * @param userName
     * @param age
     * @return
     */
    @PostMapping(value = "/users")
    public User addUser(@RequestParam("userName")String userName,@RequestParam("age")Integer age){
        User user = new User();
        user.setUserName(userName);
        user.setAge(15);
        return userRepository.save(user);
    }

    /**
     * 根據(jù)id查詢用戶
     * @return
     */
    @GetMapping(value = "/users/{id}")
    public User getUserById(@PathVariable("id") Integer id){
        return userRepository.findOne(id);
    }

    /**
     * 根基id修改用戶
     * @param id
     * @param userName
     * @param age
     * @return
     */
    @PutMapping(value = "/users/{id}")
    public User updateUserById(@PathVariable("id") Integer id,@RequestParam("userName")String userName,@RequestParam("age")Integer age){
        User user = new User();
        user.setId(id);
        user.setAge(age);
        user.setUserName(userName);
        return userRepository.save(user);
    }

    /**
     * 根據(jù)id刪除用戶
     * @param id
     */
    @DeleteMapping(value = "/users{id}")
    public void deleteUserById(@PathVariable("id") Integer id){
        userRepository.delete(id);
    }

    @GetMapping(value = "users/age/{age}")
    public List<User> listUserByAge(@PathVariable("age") Integer age){
        return userRepository.findByAge(age);
    }
}

上述代碼中我們也擴(kuò)展了使用年齡來查詢用戶,在UserRepository接口中我們擴(kuò)展了這個方法如下所示:

//通過年齡查詢,方法名有規(guī)定
public List<User> findByAge(Integer age);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,946評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,886評論 18 139
  • PHP_EOL 相當(dāng)于換行符 windows平臺相當(dāng)于 echo "\r\n"; unix\linux...
    小蛤閱讀 862評論 0 0
  • 銀龍 2015.03.25 月華初升契仙濃。 蟲蟬早鳴草坡東。 落霞明滅盡孤鴻。 紅塵茫茫余白雪。 丹涯萬里好憑風(fēng)...
    醉仙王子閱讀 113評論 0 0