springboot系列教程(五)——整合jpa

来源:互联网 发布:稳的网络意思是什么 编辑:程序博客网 时间:2024/05/21 17:48

整合jpa

pom

        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-jpa</artifactId>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <scope>runtime</scope>        </dependency>

配置文件

spring:  datasource:    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8    username: root    password: 123456  jpa:    hibernate:      ddl-auto: create  # 第一次建表create  后面用update    show-sql: true

新建model类

package com.example.demo.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * @author :小虎 * @date :2017/12/22 */@Entitypublic class User {    @Id    @GeneratedValue    private int id ;    private String name ;    private double money;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getMoney() {        return money;    }    public void setMoney(double money) {        this.money = money;    }    @Override    public String toString() {        return "User{" +                "id=" + id +                ", name='" + name + '\'' +                ", money=" + money +                '}';    }}

新建UserDao

package com.example.demo.dao;import com.example.demo.model.User;import org.springframework.data.jpa.repository.JpaRepository;/** * @author :小虎 * @date :2017/12/22 */public interface UserDao extends JpaRepository<User,Integer> {}

新建UserController

package com.example.demo.web;import com.example.demo.dao.UserDao;import com.example.demo.model.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;/** * @author :小虎 * @date :2017/12/22 */@RestController@RequestMapping("/user")public class UserController {    @Autowired    UserDao userDao;    @RequestMapping(value = "/list", method = RequestMethod.GET)    public List<User> getUsers() {        return userDao.findAll();    }    @RequestMapping(value = "/{id}", method = RequestMethod.GET)    public User getUserDaoById(@PathVariable("id") int id) {        return userDao.findOne(id);    }    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)    public String updateUserDao(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,                                @RequestParam(value = "money", required = true) double money) {        User user = new User();        user.setMoney(money);        user.setName(name);        user.setId(id);        User user1 = userDao.saveAndFlush(user);        return user1.toString();    }    @RequestMapping(value = "", method = RequestMethod.POST)    public String postUserDao(@RequestParam(value = "name") String name,                              @RequestParam(value = "money") double money) {        User user = new User();        user.setMoney(money);        user.setName(name);        User UserDao1 = userDao.save(user);        return UserDao1.toString();    }}

源码地址

https://github.com/itmybaby/springboot/tree/master/study5