一、说明
- 本文环境需要:gradle idea jdk1.8+ mysql
二、创建项目
1. idea新建gradle项目
gradle 引用 mysql spring-boot
plugins { id 'org.springframework.boot' version '2.1.7.RELEASE' id 'io.spring.dependency-management' version '1.0.8.RELEASE' id 'java'}group 'com.test'version '1.0-SNAPSHOT'sourceCompatibility = 1.8repositories { mavenCentral()}dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' runtimeOnly 'mysql:mysql-connector-java' testImplementation 'org.springframework.boot:spring-boot-starter-test'}
2. main/resources/application.properties文件
spring.jpa.hibernate.ddl-auto=updatespring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/testspring.datasource.username=springuserspring.datasource.password=ThePassword
说明:
spring.jpa.hibernate.ddl-auto
- ddl-auto:create----每次运行该程序,没有表格会新建表格,表内有数据会清空
- ddl-auto:create-drop----每次程序结束的时候会清空表
- ddl-auto:update----每次运行程序,没有表格会新建表格,表内有数据不会清空,只会更新
- ddl-auto:validate----运行程序会校验数据与数据库的字段类型是否相同,不同会报错
3. 实体类 User.java
package com.example.accessingdatamysql;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 classpublic 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; }}
4. 创建Repository
package com.example.accessingdatamysql;import org.springframework.data.repository.CrudRepository;import com.example.accessingdatamysql.User;// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository// CRUD refers Create, Read, Update, Deletepublic interface UserRepository extends CrudRepository {}
Controller里可以使用userRepository来进行数据库操作。
5. 创建MainController
package com.example.accessingdatamysql;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@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") // Map ONLY POST Requests public @ResponseBody String addNewUser (@RequestParam String name , @RequestParam String email) { // @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.setEmail(email); userRepository.save(n); return "Saved"; } @GetMapping(path="/all") public @ResponseBody Iterable getAllUsers() { // This returns a JSON or XML with the users return userRepository.findAll(); }}
6. 程序入口 AccessingDataMysqlApplication
package com.example.accessingdatamysql;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class AccessingDataMysqlApplication { public static void main(String[] args) { SpringApplication.run(AccessingDataMysqlApplication.class, args); }}
创建好的项目结构:
三、MySQL 新建测试数据库
四、测试
新增数据
curl localhost:8080/demo/add -d name=First -d email=someemail@someemailprovider.com
查看数据
在浏览器打开: http://localhost:8080/demo/all
显示:
[{"id":10,"name":"First","email":"someemail@someemailprovider.com"}]