springboot基本配置
1,开发工具idea,新建项目,添加web和mysql,创建数据库,创建表,配置pom.xml。
<?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.mysql</groupId>
<artifactId>springcurd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcurd</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2,新建application.yml。
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/springcurd?characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
jpa:
database: mysql
show-sql: true
hibernate:
naming:
strategy: org.hibernate.cfg.ImprovedNamingStrategy
ddl-auto: update
3,新建UserEntity。
package com.mysql.springcurd;
import javax.persistence.*;
@Entity
@Table(name = "user")
public class UserEntity {
private Integer id;
private String username;
private String password;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4,新建接口UserJPA。
package com.mysql.springcurd;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserJPA extends JpaRepository<UserEntity,Integer> {
}
5,新建UserController测试。
package com.mysql.springcurd;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Autowired UserJPA userJPA;
@RequestMapping(value = "/query")
public List<UserEntity> query(){
return userJPA.findAll();
}
@RequestMapping(value = "/delete")
public List<UserEntity> delete(Integer id){
userJPA.delete(id);
return userJPA.findAll();
}
@RequestMapping(value = "/add")
public List<UserEntity> add(UserEntity userEntity){
userJPA.save(userEntity);
return userJPA.findAll();
}
@RequestMapping(value = "/update")
public List<UserEntity> update(UserEntity user){
userJPA.saveAndFlush(user);
return userJPA.findAll();
}
}
还没有评论,来说两句吧...