spring boot jpa逆向生成表 简单实例:

第一步:pom文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.payease</groupId>
  7. <artifactId>girl</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>girl</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.5.8.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26.  
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <!-- 模版引擎 -->
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  42. </dependency>
  43.  
  44. <!-- spring data jpa -->
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-data-jpa</artifactId>
  48. </dependency>
  49.  
  50. <!-- mysql 组件 -->
  51. <dependency>
  52. <groupId>mysql</groupId>
  53. <artifactId>mysql-connector-java</artifactId>
  54. </dependency>
  55. <dependency>
  56. <groupId>javax.persistence</groupId>
  57. <artifactId>persistence-api</artifactId>
  58. <version>1.0</version>
  59. </dependency>
  60. <dependency>
  61.  
  62. <groupId>org.springframework.boot</groupId>
  63.  
  64. <artifactId>spring-boot-configuration-processor</artifactId>
  65.  
  66. <optional>true</optional>
  67.  
  68. </dependency>
  69.  
  70. </dependencies>
  71.  
  72. <build>
  73. <plugins>
  74. <plugin>
  75. <groupId>org.springframework.boot</groupId>
  76. <artifactId>spring-boot-maven-plugin</artifactId>
  77. </plugin>
  78. </plugins>
  79. </build>
  80.  
  81. </project>

第二步:配置文件application.yml。

  1. spring:
  2. profiles:
  3. active: dev
  4. datasource:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://127.0.0.1:3306/dbgirl
  7. username: root
  8. password: 1234
  9. jpa:
  10. hibernate:
  11. ddl-auto: update
  12. show-sql: true

update第一次运行会创建表 若原来有表且有数据会保留原来的数据

第三步:配置问件application-dev.yml

  1. server:
  2. port: 8080
  3. girl:
  4. cupSize: B
  5. age: 18

第四步:创建数据库(navigate)

第五步:创建实体类

  1. package com.payease.entity;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.Id;
  6.  
  7. /**
  8. * Created by liuxiaoming on 2017/11/6.
  9. */
  10. @Entity
  11. public class Girl {
  12. public int getId() {
  13. return id;
  14. }
  15.  
  16. public void setId(int id) {
  17. this.id = id;
  18. }
  19.  
  20. public String getCupSize() {
  21. return cupSize;
  22. }
  23.  
  24. public void setCupSize(String cupSize) {
  25. this.cupSize = cupSize;
  26. }
  27.  
  28. public int getAge() {
  29. return age;
  30. }
  31.  
  32. public void setAge(int age) {
  33. this.age = age;
  34. }
  35.  
  36. @Id
  37. @GeneratedValue
  38.  
  39. private int id;
  40. private String cupSize;
  41. private int age;
  42.  
  43. }

第六步:创建启动文件 并 启动项目

  1. package com.payease;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.ComponentScan;
  6.  
  7. @SpringBootApplication
  8. //@ComponentScan(basePackages={"com.payease.entity"})
  9. public class GirlApplication {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(GirlApplication.class, args);
  13. }
  14. }

根据所创建的表实现简单的增删改查功能:

 实现第一个接口:

第一步:编写service

  1. package com.payease.service;
  2.  
  3. import com.payease.entity.Girl;
  4. import org.springframework.data.jpa.repository.JpaRepository;
  5. import org.springframework.stereotype.Repository;
  6.  
  7. /**
  8. * Created by liuxiaoming on 2017/11/6.
  9. */
  10. @Repository
  11. public interface GirlService extends JpaRepository<Girl,Integer>{
  12. }

第二步:编写controller

  1. package com.payease.controller;
  2.  
  3. import com.payease.entity.Girl;
  4. import com.payease.service.GirlService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. import java.util.List;
  10.  
  11. /**
  12. * Created by liuxiaoming on 2017/11/1.
  13. */
  14. @RestController
  15. //@RequestMapping("/hello")
  16. public class GirlController {
  17.  
  18. @Autowired
  19. private GirlService girlService;
  20.  
  21. @GetMapping("/girls")
  22. public List<Girl> girls(){
  23. return girlService.findAll();
  24. }
  25. }

第三步:启动项目:

第四步:postman提交

其他接口:

第一步controller:

  1. package com.payease.controller;
  2.  
  3. import com.payease.entity.Girl;
  4. import com.payease.service.GirlService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.*;
  7.  
  8. import java.util.List;
  9.  
  10. /**
  11. * 查询女生列表
  12. * Created by liuxiaoming on 2017/11/1.
  13. */
  14. @RestController
  15. //@RequestMapping("/hello")
  16. public class GirlController {
  17.  
  18. @Autowired
  19. private GirlService girlService;
  20.  
  21. @GetMapping("/girls")
  22. public List<Girl> girls(){
  23. return girlService.findAll();
  24. }
  25.  
  26. /**
  27. * 创建一个女生
  28. */
  29. @PostMapping("/girls")
  30. public Girl girlAdd(@RequestParam("cupSize")String cupSize,
  31. @RequestParam("age")Integer age){
  32. Girl girl = new Girl();
  33. girl.setCupSize(cupSize);
  34. girl.setAge(age);
  35. return girlService.save(girl);
  36. }
  37.  
  38. /**
  39. * 通过ID查询一个女生
  40. */
  41. @PostMapping("/girls/{id}")
  42. public Girl getgirl(@PathVariable("id")Integer id){
  43. return girlService.findOne(id);
  44. }
  45.  
  46. /**
  47. * 通过ID更新一个女生
  48. */
  49. @PutMapping("/girls/{id}")
  50. public Girl girlUpdate (@PathVariable("id")Integer id,
  51. @RequestParam("cupSize") String cupSize,
  52. @RequestParam("age") Integer age){
  53. Girl girl = new Girl();
  54. girl.setId(id);
  55. girl.setCupSize(cupSize);
  56. girl.setAge(age);
  57. return girlService.save(girl);
  58. }
  59.  
  60. /**
  61. * 通过ID删除一个女生
  62. */
  63. @DeleteMapping("/girls/{id}")
  64. public void girlDelete(@PathVariable("id")Integer id){
  65. girlService.delete(id);
  66. }
  67.  
  68. /**
  69. * 通过年龄查询女生列表
  70. */
  71. @GetMapping("/girls/age/{age}")
  72. public List<Girl> girlListByAge(@PathVariable("age")Integer age){
  73. return girlService.findByAge(age);
  74. }
  75. }

第二步:扩展方法service:

  1. package com.payease.service;
  2.  
  3. import com.payease.entity.Girl;
  4. import org.springframework.data.jpa.repository.JpaRepository;
  5.  
  6. import java.util.List;
  7.  
  8. /**
  9. * Created by liuxiaoming on 2017/11/6.
  10. */
  11. public interface GirlService extends JpaRepository<Girl,Integer>{
  12.  
  13. //条件查询: 通过年龄来查询
  14. public List<Girl> findByAge(Integer age);
  15. }

第三步:项目启动 postman提交:

数据库的sql文件:

  1. /*
  2. Navicat Premium Data Transfer
  3.  
  4. Source Server : localhost
  5. Source Server Type : MySQL
  6. Source Server Version : 50718
  7. Source Host : localhost
  8. Source Database : dbgirl
  9.  
  10. Target Server Type : MySQL
  11. Target Server Version : 50718
  12. File Encoding : utf-8
  13.  
  14. Date: 11/07/2017 09:45:34 AM
  15. */
  16.  
  17. SET NAMES utf8mb4;
  18. SET FOREIGN_KEY_CHECKS = 0;
  19.  
  20. -- ----------------------------
  21. -- Table structure for `girl`
  22. -- ----------------------------
  23. DROP TABLE IF EXISTS `girl`;
  24. CREATE TABLE `girl` (
  25. `id` int(11) NOT NULL AUTO_INCREMENT,
  26. `age` int(11) NOT NULL,
  27. `cup_size` varchar(255) DEFAULT NULL,
  28. PRIMARY KEY (`id`)
  29. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
  30.  
  31. -- ----------------------------
  32. -- Records of `girl`
  33. -- ----------------------------
  34. BEGIN;
  35. INSERT INTO `girl` VALUES ('1', '18', 'B'), ('2', '20', 'D'), ('3', '20', 'F');
  36. COMMIT;
  37.  
  38. SET FOREIGN_KEY_CHECKS = 1;

postman提交:

创建一个女生:http://127.0.0.1:8080/girls

  1. 通过ID查询一个女生:http://127.0.0.1:8080/girls/1

通过ID更新一个女生:http://127.0.0.1:8080/girls/1

通过ID删除一个女生:http://127.0.0.1:8080/girls/4

数据库原数据:

调用方法后的数据:

根据年龄来查询女生列表:http://127.0.0.1:8080/girls/age/20

spring boot快速入门 4: jpa数据库操作 实现增删改查的更多相关文章

  1. 基于renren-fast的快速入门项目实战(实现报表增删改查)

    基于renren-fast的快速入门项目实战(实现报表增删改查) 说明:renren-fast是一个开源的基于springboot的前后端分离手脚架,当前版本是3.0 官方开发文档需付费,对于新手而言 ...

  2. Java Maven:spring boot + Mybatis连接MySQL,通用mapper的增删改查,映射实现多表查询

    1. MySQL自带库test添加表user.role 角色表role 用户表user 2. 添加依赖,配置属性 相关依赖:百度即可,此处略 application.properties spring ...

  3. Linq 数据库操作(增删改查)

    Linq数据库增删改查 Linq是一种查询语言,集成包含在formwork中,包含在C#语言中,它的作用是降低查询的门槛,提高开发效率,是我们必须掌握的技术之一,下面是我自己对linq数据库操作的方法 ...

  4. 初次尝试PHP——一个简单的对数据库操作的增删改查例子

    第一次学习PHP,很多人说PHP是最好的语言,学习了一点点,还不敢说这样的话,不过确实蛮好用的. 做了一个简单的对数据库的增删改查的操作,主要是将四种操作写成了独立的函数,之后直接调用函数.以下是代码 ...

  5. Spring Boot 知识笔记(整合Mybatis续-补充增删改查)

    续上篇,补充数据库增删改查的其他场景. 一.Mapper中添加其他场景操作 package net.Eleven.demo.Mapper; import net.Eleven.demo.domain. ...

  6. flask 数据库操作(增删改查)

    数据库操作 现在我们创建了模型,生成了数据库和表,下面来学习常用的数据库操作,数据库操作主要是CRUD,即Create(创建).Read(读取/查询).Update(更新)和Delete(删除). S ...

  7. MySQL数据库操作:“增删改查”,忘记密码重置等。

    [注] 数据库的“增删查改”,参考原作者Wid:http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#d11.感谢大佬们的技术分享 ...

  8. ThinkPHP 数据库操作(二) : 增删改查

    基本使用 可以直接使用数据库运行原生SQL操作了,支持 query (查询操作)和 execute (写入操作)方法,并且支持参数绑定. Db::query('select * from think_ ...

  9. Java 数据库操作oracle增删改查,通用封装基于hashmap

    pt1:首先安装oracle连接驱动 下载地址:https://pan.baidu.com/s/1jW_ofgU4eJmAn7Y2J5B46A  密码:epkz 1.将ojdbc6.jar导入项目中 ...

随机推荐

  1. 项目中使用WCF替换asmx Web service总结

    以前项目解决方案中,用http协议的asmx Web service作服务器数据访问入口,在SoapHeader中写入用户名和加盐密码进行身份认证. http asmx服务是明文传输,传输过程中数据很 ...

  2. php的循环与引用的一个坑

    上代码 $arr = array( 'a'=> 'a11', 'b'=> 'b22', 'c'=> 'c33', ); foreach ($arr as $k=>&$v ...

  3. WebApi 插件式构建方案:重写的控制器获取工厂

    body { border: 1px solid #ddd; outline: 1300px solid #fff; margin: 16px auto; } body .markdown-body ...

  4. 朋友,请待你的朋友——BUG好一点!

    程序猿嘛,难免会被BUG缠身,我相信,没有一个程序猿在被BUG缠身时是感觉轻松的,消灭BUG一定是你最大的愿望.本周,我们团队的项目进入调试阶段,各种BUG层出不穷,眼看下个周就要进行项目答辩会,所以 ...

  5. c# 利用t4模板,自动生成Model类

    我们在用ORM(比如dapper)的时候,很多时候都需要自己写Model层(当然也有很多orm框架自带了这种功能,比如ef),特别是表里字段比较多的时候,一个Model要写半天,而且Model如果用于 ...

  6. ASP.NET基于Aspose.Words插入Word水印以及多个水印

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  7. python网络编程--TCP连接的三次握手(三报文握手)与四次挥手

    一.TCP连接 运输连接有三个阶段: 连接建立.数据传送和连接释放. 在TCP连接建立过程中要解决以下三个问题: 1,要使每一方能够确知对方的存在. 2.要允许双方协商一些参数(如最大窗口之,是否使用 ...

  8. 【文文殿下】[BZOJ4327] JSOI2012 玄武密码

    SAM裸题.这道题卡空间.要小心数组别开炸了. #include<cstdio> #include<cstring> typedef long long ll; const i ...

  9. Django 项目拆分配置文件settings.py

    使用Django命令生成一个项目的基本结构时, 配置信息默认保存在和项目目录同名的目录下的settings.py文件里, 对于一个项目而言, 这样往往是不合适的, 在实际的开发中,需要将配置文件拆分为 ...

  10. 删除标注关联仿dda命令DIMDISASSOCIATE

    static void sk_ARXTestXDatamydimassoc(void) { // Add your code for command sk_ARXTestXData.mydimasso ...