菜鸟springboot 学习之旅三
来源:互联网 发布:nginx windows 重启 编辑:程序博客网 时间:2024/06/06 01:34
Springboot 之MySQL
添加依赖
在pom.xml添加jpa、MySQL依赖
<!-- 添加数据库组件 --> <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>
数据库配置
Springboot配置数据库可以在application.properties中配置。不过我选择使用application.yml进行配置
配置如下:
#定义端口,项目URL入口server: port: 8081 context-path: /learn0829#数据库配置spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/opendb username: root password: mysql jpa: hibernate: ddl-auto: update show-sql: true#配置页面默认前缀目录;响应页面默认后缀 mvc: view: prefix: /WEB-INF/ suffix: .jsp
Restful接口设计
Get(select)----------------从服务器获取资源Post(create)---------------在服务器新建资源,可以理解为新增数据Put(update)---------------在服务器更新资源(客户端提供改变后的完整资源)Patch(update)-------------在服务器更新资源(客户端提供改变的属性)Delete(delete)-------------从服务器删除资源HEAD:(不常用)------------获取资源元数据Option(不常用)-----------获取信息,关于资源的哪些属性是客户端可以改变的。
Model 新建表模型
package com.sttx.open.hello.model;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * @author open[yuleikai@sttxtech.com] * @date 2017年9月1日 */@Entitypublic class test0901 { @Id @GeneratedValue private Integer testSeq; private String name; private Integer sex; private Date birthday; private String mobile; public test0901() { } public Integer getTestSeq() { return testSeq; } public void setTestSeq(Integer testSeq) { this.testSeq = testSeq; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } @Override public String toString() { return "test0901 [testSeq=" + testSeq + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", mobile=" + mobile + "]"; }}
Jpa接口
创建接口继承jpa
public interface Itest0901 extends JpaRepository<test0901,Integer>{}
Controller
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.sttx.open.hello.model.test0901;@RestController@RequestMapping(value="/test0901")public class test0901Controller { @Autowired private Itest0901 repository; @GetMapping(value="/findAll") public List<test0901> findAll(){ return repository.findAll(); } @PostMapping(value="/addOnce") public test0901 create(test0901 req){ return repository.save(req); } @PutMapping(value="/updateOnce") public test0901 updateOnce(test0901 req){ System.out.println(req.toString()); if(req.getTestSeq()==null) return null; return repository.save(req); } @DeleteMapping(value="/deleteOnce") public void deleteOnce(Integer testSeq){ repository.delete(testSeq); }}
启动测试
启动springboot,在使用postman测试
阅读全文
0 0
- 菜鸟springboot 学习之旅三
- 菜鸟springboot学习之旅之@RequestBody
- 菜鸟springboot学习之旅一
- 菜鸟springboot学习之旅二
- 菜鸟springboot学习之旅四
- Spring Boot学习之旅:(三)springboot热部署
- springboot学习之旅
- Java-菜鸟学习之路(三)
- 机器学习sklearn之菜鸟入门三
- springboot学习笔记三
- Springboot学习笔记(三)
- JAVA菜鸟学习之旅
- 菜鸟之jpa三
- 菜鸟学QT之三-------------QT学习资源
- windows API 菜鸟学习之路(三)
- JAVAEE菜鸟学习之路(三)-------快餐文化
- 菜鸟之学习51单片机(三)点亮LED灯
- SpringBoot 学习记录(三): jpa
- Android播放器框架分析之AwesomePlayer
- Spring 框架
- Flume框架及应用
- Java多线程详解(二)
- C# out 参数修饰符
- 菜鸟springboot 学习之旅三
- Java多线程--Lock接口
- paint的色彩矩阵以及滤镜效果
- Android核心服务解析篇(一)——下载Android源代码
- POI导入excel时,长串数字变为科学计数法的解决办法
- Android线程管理之Thread使用总结
- scaleType分析
- Python从入门到实践 习题15-6
- 调用铃声及震动