spring boot快速入门 4: jpa数据库操作 实现增删改查
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数据库操作 实现增删改查的更多相关文章
- 基于renren-fast的快速入门项目实战(实现报表增删改查)
基于renren-fast的快速入门项目实战(实现报表增删改查) 说明:renren-fast是一个开源的基于springboot的前后端分离手脚架,当前版本是3.0 官方开发文档需付费,对于新手而言 ...
- Java Maven:spring boot + Mybatis连接MySQL,通用mapper的增删改查,映射实现多表查询
1. MySQL自带库test添加表user.role 角色表role 用户表user 2. 添加依赖,配置属性 相关依赖:百度即可,此处略 application.properties spring ...
- Linq 数据库操作(增删改查)
Linq数据库增删改查 Linq是一种查询语言,集成包含在formwork中,包含在C#语言中,它的作用是降低查询的门槛,提高开发效率,是我们必须掌握的技术之一,下面是我自己对linq数据库操作的方法 ...
- 初次尝试PHP——一个简单的对数据库操作的增删改查例子
第一次学习PHP,很多人说PHP是最好的语言,学习了一点点,还不敢说这样的话,不过确实蛮好用的. 做了一个简单的对数据库的增删改查的操作,主要是将四种操作写成了独立的函数,之后直接调用函数.以下是代码 ...
- Spring Boot 知识笔记(整合Mybatis续-补充增删改查)
续上篇,补充数据库增删改查的其他场景. 一.Mapper中添加其他场景操作 package net.Eleven.demo.Mapper; import net.Eleven.demo.domain. ...
- flask 数据库操作(增删改查)
数据库操作 现在我们创建了模型,生成了数据库和表,下面来学习常用的数据库操作,数据库操作主要是CRUD,即Create(创建).Read(读取/查询).Update(更新)和Delete(删除). S ...
- MySQL数据库操作:“增删改查”,忘记密码重置等。
[注] 数据库的“增删查改”,参考原作者Wid:http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#d11.感谢大佬们的技术分享 ...
- ThinkPHP 数据库操作(二) : 增删改查
基本使用 可以直接使用数据库运行原生SQL操作了,支持 query (查询操作)和 execute (写入操作)方法,并且支持参数绑定. Db::query('select * from think_ ...
- Java 数据库操作oracle增删改查,通用封装基于hashmap
pt1:首先安装oracle连接驱动 下载地址:https://pan.baidu.com/s/1jW_ofgU4eJmAn7Y2J5B46A 密码:epkz 1.将ojdbc6.jar导入项目中 ...
随机推荐
- struct 和union的区别
union ( 共用体):构造数据类型,也叫联合体 用途:使几个不同类型的变量共占一段内存(相互覆盖) struct ( 结构体 ):是一种构造类型 用途: 把不同的数据组合成一个整体——自定义数据 ...
- mdk3攻击实例
Authentication Flood,mdk3下参数为a: mdk3 mon0 a –a AP的MAC地址(BSSID) -c来对指定的频道进行攻击 -a固定bssid进行攻击 -s控制发包速率. ...
- 用word2016 写CSDN 博客
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- .Net工程师面试笔试宝典
.Net工程师面试笔试宝典 传智播客.Net培训班内部资料 http://net.itcast.cn 这套面试笔试宝典是传智播客在多年的教学和学生就业指导过程中积累下来的宝贵资料,大部分来自于学员从面 ...
- subprocess.Popen命令如何隐藏弹框
在用PYQT编写GUI界面时,代码中有用到subprocess.Popen(),打包exe后每次遇到subprocess语句是就会弹出命令框,很是头疼, 下面是解决的办法 import subproc ...
- SelectOnCheck
1.checkOnSelect 如果为true,当用户点击行的时候该复选框就会被选中或取消选中. 如果为false,当用户仅在点击该复选框的时候才会呗选中或取消. 2.selectOnCheck 如果 ...
- C#发送邮件及注意事项
//参数配置 static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings[&qu ...
- C# 收银机顾显(客显)及打印小票(58热敏打印机)
最近做winform收银机,设计顾显及打印小票总结. 1.顾显(串口COM1) 只涉及到总计,所以只是简单的功能. public static ClientDisplayResult Display( ...
- python--类与类之间的关系,(魔术方法)特殊成员方法
1.依赖关系 类与类之间存在这几种关系:1.依赖关系 2.关联关系 3.组合关系 4.聚合关系 5.继承关系 6.实现关系 我们来设置下面两个场景 玩电脑和打僵尸 class Person: def ...
- clickonce联机模式
发布时选择该程序只能联机使用,这样本地就不会进行安装. 参考地址:https://blog.csdn.net/dqs78833488/article/details/52513948