这个项目主要是利用SpringBoot+Hibernate实现对数据库的增、查。项目看起来虽然简单,但配置起来还是比较吃力,有许多的细节需要注意,现在我做一个笔记以纪念我遇到的所有问题。一个号称五分钟的项目(严重怀疑雷童鞋说的话可信度为0) 活活做了两小时,自己搭建环境、在小伙伴的帮助下排错,学习IDEA调试、使用postman软件进行前端模拟,收获满满~在排错上感觉自己进步了,打卡~
感谢在学习路上给我指导的所有伙伴~
官方参考链接
目的:在SpringBoot框架下使用MySQL访问数据
本指南将引导您完成创建与MySQL数据库连接的Spring应用程序的过程,而不是内存中的嵌入式数据库,所有其他指南和许多示例应用程序都使用该数据库。 它使用Spring Data JPA来访问数据库,但这只是众多可能选择中的一种(例如,您可以使用普通的Spring JDBC)。
创建一个MySQL数据库,构建一个Spring应用程序并将其与新创建的数据库连接。
Maven Gradle 区别,本文使用maven构建项目。
代码如下:
Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
MainController.java
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import hello.User;
import hello.UserRepository;
@Controller // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@PostMapping(path="/add") // if use @GetMapping, Map ONLY GET Requests.
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam Integer age) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setAge(age);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
User.java
package hello;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity // This tells Hibernate to make a table out of this class
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
UserRepository.java
package hello;
import org.springframework.data.repository.CrudRepository;
import hello.User;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository {
}
application.properties
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/ytt?3useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.hibernate.use-new-id-generator-mappings=false
#禁用OSIV(open Session in view)
spring.jpa.open-in-view=false
pom.xml
4.0.0
org.springframework
gs-mysql-data
0.1.0
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
test
1.8
org.springframework.boot
spring-boot-maven-plugin
至此,项目的所有开发步骤已经完成。
jdbc:mysql://localhost:3306/ytt
useUnicode=true&characterEncoding=utf8
配置编码方式
serverTimezone=GMT%2B8
配置时区防止使用MySQL时报错
报错类型:The server time zone value ‘�й���ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
spring.jpa.hibernate.use-new-id-generator-mappings=false
springboot 1.5.9.RELEASE 升级至 2.0.5.RELEASE时,spring-boot-starter-data-jpa使用了hibernate5。解决 Table ‘ytt.hibernate_sequence’ doesn’t exist的错误。
#禁用OSIV(open Session in view)spring.jpa.open-in-view=false
http://localhost:8080/demo/add?name=First&age=11
并回车@PostMapping(path="/add")
造成提交数据的方式有问题,浏览器这样的提交方式属于get方法。@PostMapping(path="/add")
改为@GetMapping(path="/add")
。完美解决
注意: 将@GetMapping(path="/add")
改为@PostMapping(path="/add")
。