创建项目
配置gradle project 添加 spring_web mysql jpa
创建数据库
mysql> create database db_example; -- Creates the new database
mysql> create user 'springuser'@'%' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'@'%'; -- Gives all privileges to the new user on the newly created database
编写文件配置application.properties
src/main/resources/application.properties
#数据库方言
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
#数据库创建方式,第一次运行可修改为create,可根据实体类自动创建数据库表
spring.jpa.hibernate.ddl-auto=update
#mysqlurl连接字符串
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
# 用户名
spring.datasource.username=springuser
# 密码
spring.datasource.password=ThePassword
创建实体类
package com.example.demo2.mysql;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity // 这告诉Hibernate从这个类中创建一个表
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
创建储存库
package com.example.demo2.mysql;
import org.springframework.data.repository.CrudRepository;
// 这将由Spring自动实现到一个名为userRepository的Bean中
// CRUD指创建、读取、更新、删除
public interface UserRepository extends CrudRepository<User, Integer> {
}
创建控制器
package com.example.demo2.mysql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller // 这意味着这个类是一个控制器
@RequestMapping(path="/demo") // 这意味着URL以/demo开始(在应用程序路径之后)
public class MainController {
@Autowired // 这意味着获得名为userRepository的bean
// 哪个是Spring自动生成的,我们会用它来处理数据
private UserRepository userRepository;
@PostMapping(path="/add") // 仅映射POST请求
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody表示返回的字符串是响应,而不是视图名
// @RequestParam表示它是来自GET或POST请求的一个参数
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody
Iterable<User> getAllUsers() {
// 这将返回带有用户的JSON或XML
return userRepository.findAll();
}
}
创建启动类
package com.example.demo2.mysql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AccessingDataMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(AccessingDataMysqlApplication.class, args);
}
}
运行测试,测试工具postman
localhost:8080/demo/all #查看所有信息
localhost:8080/demo/add?name=First2&email=someemail@someemailprovider.com #添加用户信息