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日
*/
@Entity
public 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测试