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
阅读全文
0 0
- springboot系列教程(五)——整合jpa
- springboot系列教程(六)——整合jdbctemplate
- springboot系列教程(七)——整合mybatis
- springboot系列教程(八)——整合thymeleaf、freemarker
- SpringBoot入门系列: JPA mysql(五)
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
- SpringBoot非官方教程 | 第四篇:SpringBoot 整合JPA
- SpringBoot学习(五)——整合MyBatis
- SpringBoot系列2—整合Mybatis-plus
- SpringBoot 整合JPA
- springboot 整合jpa
- springboot整合jpa
- springBoot整合springData Jpa
- 4.springboot整合jpa
- SpringBoot(五):SpringBoot整合MyBatis
- Springboot教程五——tips
- SpringBoot系列(6)---SpringBoot-JPA
- Android-Studio无法显示正在运行的程序
- 为什么Dialog不能用Application的Context
- docker搭建php,nginx,mysql环境
- 阿里云容服务器产品优势及应用场景
- cocos2dx 3.x(常见的46种动作)转:
- springboot系列教程(五)——整合jpa
- J:\java资料全套\基础+就业\Spring框架2016版视频\day03视频\08-jdbcTemplate实现crud操作(查询list集合).java
- 2017-12-22
- oracle 临时表性能下降
- iOS开发系列--让你的应用“动”起来
- [转] 云服务器 ECS Windows 2012 通过 IIS 配置 FTP 操作步骤
- 课后练习题(第二章)
- linux awk命令详解
- 深度学习笔记--激活函数:sigmoid,maxout