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

第一步:pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.payease</groupId>
<artifactId>girl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>girl</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 模版引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <!-- spring data 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>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

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

spring:
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbgirl
username: root
password: 1234
jpa:
hibernate:
ddl-auto: update
show-sql: true

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

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

server:
port: 8080
girl:
cupSize: B
age: 18

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

第五步:创建实体类

package com.payease.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; /**
* Created by liuxiaoming on 2017/11/6.
*/
@Entity
public class Girl {
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getCupSize() {
return cupSize;
} public void setCupSize(String cupSize) {
this.cupSize = cupSize;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Id
@GeneratedValue private int id;
private String cupSize;
private int age; }

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

package com.payease;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
//@ComponentScan(basePackages={"com.payease.entity"})
public class GirlApplication { public static void main(String[] args) {
SpringApplication.run(GirlApplication.class, args);
}
}

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

 实现第一个接口:

第一步:编写service

package com.payease.service;

import com.payease.entity.Girl;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; /**
* Created by liuxiaoming on 2017/11/6.
*/
@Repository
public interface GirlService extends JpaRepository<Girl,Integer>{
}

第二步:编写controller

package com.payease.controller;

import com.payease.entity.Girl;
import com.payease.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; /**
* Created by liuxiaoming on 2017/11/1.
*/
@RestController
//@RequestMapping("/hello")
public class GirlController { @Autowired
private GirlService girlService; @GetMapping("/girls")
public List<Girl> girls(){
return girlService.findAll();
}
}

第三步:启动项目:

第四步:postman提交

其他接口:

第一步controller:

package com.payease.controller;

import com.payease.entity.Girl;
import com.payease.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; /**
* 查询女生列表
* Created by liuxiaoming on 2017/11/1.
*/
@RestController
//@RequestMapping("/hello")
public class GirlController { @Autowired
private GirlService girlService; @GetMapping("/girls")
public List<Girl> girls(){
return girlService.findAll();
} /**
* 创建一个女生
*/
@PostMapping("/girls")
public Girl girlAdd(@RequestParam("cupSize")String cupSize,
@RequestParam("age")Integer age){
Girl girl = new Girl();
girl.setCupSize(cupSize);
girl.setAge(age);
return girlService.save(girl);
} /**
* 通过ID查询一个女生
*/
@PostMapping("/girls/{id}")
public Girl getgirl(@PathVariable("id")Integer id){
return girlService.findOne(id);
} /**
* 通过ID更新一个女生
*/
@PutMapping("/girls/{id}")
public Girl girlUpdate (@PathVariable("id")Integer id,
@RequestParam("cupSize") String cupSize,
@RequestParam("age") Integer age){
Girl girl = new Girl();
girl.setId(id);
girl.setCupSize(cupSize);
girl.setAge(age);
return girlService.save(girl);
} /**
* 通过ID删除一个女生
*/
@DeleteMapping("/girls/{id}")
public void girlDelete(@PathVariable("id")Integer id){
girlService.delete(id);
} /**
* 通过年龄查询女生列表
*/
@GetMapping("/girls/age/{age}")
public List<Girl> girlListByAge(@PathVariable("age")Integer age){
return girlService.findByAge(age);
}
}

第二步:扩展方法service:

package com.payease.service;

import com.payease.entity.Girl;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; /**
* Created by liuxiaoming on 2017/11/6.
*/
public interface GirlService extends JpaRepository<Girl,Integer>{ //条件查询: 通过年龄来查询
public List<Girl> findByAge(Integer age);
}

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

数据库的sql文件:

/*
Navicat Premium Data Transfer Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50718
Source Host : localhost
Source Database : dbgirl Target Server Type : MySQL
Target Server Version : 50718
File Encoding : utf-8 Date: 11/07/2017 09:45:34 AM
*/ SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for `girl`
-- ----------------------------
DROP TABLE IF EXISTS `girl`;
CREATE TABLE `girl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`age` int(11) NOT NULL,
`cup_size` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `girl`
-- ----------------------------
BEGIN;
INSERT INTO `girl` VALUES ('1', '18', 'B'), ('2', '20', 'D'), ('3', '20', 'F');
COMMIT; SET FOREIGN_KEY_CHECKS = 1;

postman提交:

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

通过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. shllter自动和手动实例

    加壳: wineconsole shellter A,选自动 将putty.exe移到/usr/share/shllter/目录,PE设置为putty.exe LHOST,LPORT 监视: use ...

  2. SpringMVC源码解析 - HandlerAdapter - @SessionAttributes注解处理

    使用SpringMVC开发时,可以使用@SessionAttributes注解缓存信息.这样业务开发时,就不需要一次次手动操作session保存,读数据. @Controller @RequestMa ...

  3. handsontable-utilities

    搜索值 鼠标右键 讲了四个功能:1.row header是否可以右键(rowheader:true):2.删除右键列表的某些值(通过数组定义):3.自定义右键列表和功能(callback,item两个 ...

  4. python 中为什么不需要重载

    函数重载主要是为了解决两个问题. (1)可变参数类型. (2) 可变参数个数. 另外,一个基本的设计原则是,仅仅当两个函数除了参数类型和参数个数不同以外,其功能是完全相同的,此时才使用函数重载,如果两 ...

  5. 清除浏览器缓存meta标签

    <meta http-equiv="Cache-Control" content="no-cache" /> <meta http-equiv ...

  6. .net core执行dotnet ef migrations createmodel等命令出错

    .net core执行dotnet ef migrations createmodel等命令出错 执行dotnet ef migrations createmodel.dotnet ef migrat ...

  7. bootstrap table 的searchParam参数传递

    bootstrap table 的searchParam自定义参数传递 Bootstrap Table返回的数据为value 和 rows Long total代表的是多少条(总数)  List< ...

  8. UWP开发入门(三)——{x:Bind}扩展标记

    上周打炉石打得太晚……忘记更新了,本周补上.本篇我们讲一下{x:Bind}扩展标记.{x:Bind}扩展标记也是Windows 10 Uinversal 新增的内容,按官方的说法是 {Binding} ...

  9. 逻辑编程入门--clojure.core.logic

    此文已由作者张佃鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1. 逻辑编程思维: 逻辑编程(逻辑程序设计)是种编程范型,它设置答案须匹配的规则来解决问题,而非设置步骤来 ...

  10. Android 获取 content layout

    if (findViewById(android.R.id.content) instanceof ViewGroup) { ViewGroup mainView = ((ViewGroup)find ...