Spring Boot访问 mySql

xiaoxiao2021-02-28  35

1 总体

本文介绍Spring Boot 如何访问MySql, MySql 使用 5.0 以上版本,并且使用 Navicat for MySQL访问工具。

1.1 配置

1.1.1 gradle配置

修改build.gradle, 增加以下两行:

// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) compile 'org.springframework.boot:spring-boot-starter-data-jpa' // Use MySQL Connector-J compile 'mysql:mysql-connector-java'

1.1.2 application.properties

建立文件 src\main\resources\application.properties, 内容如下:

spring.jpa.hibernate.ddl-auto=create spring.datasource.url=jdbc:mysql://localhost:3306/demo spring.datasource.username=root spring.datasource.password=root spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto 的值可以是 none , create, update 等。第一次使用 create, 后面可以调整为 none 或者 update。

1.2 编写代码

1.2.1 实体

建立实体 src\main\java\hello\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 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; } }

1.2.2 资源库

建立 src\main\java\hello\UserRepository.java ,继承 CrudRepository。

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<User, Long> { }

1.2.3 控制器

新建 src\main\java\hello\MainController.java

package hello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; 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; @GetMapping(path="/add") // Map ONLY GET 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<User> getAllUsers() { // This returns a JSON or XML with the users return userRepository.findAll(); } }

1.2.4 应用

建立 src\main\java\hello\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); } }

1.3 运行

1.3.1 新增用户

http://localhost:8080/demo/add?name=First&email=someemail@someemailprovider.com

1.3.2 数据库查看

使用 Navicat for MySQL 访问 mysql, 查看结果如下:

1.3.3 浏览器查看

http://localhost:8080/demo/all

1.3.4 运行中显示的sql

2018-02-18 12:21:41.246 INFO 3700 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 33 ms Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: insert into user (email, name, id) values (?, ?, ?) Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: insert into user (email, name, id) values (?, ?, ?) 2018-02-18 12:45:22.187 INFO 3700 --- [nio-8080-exec-5] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory Hibernate: select user0_.id as id1_0_, user0_.email as email2_0_, user0_.name as name3_0_ from user user0_ Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: insert into user (email, name, id) values (?, ?, ?)
转载请注明原文地址: https://www.6miu.com/read-2624280.html

最新回复(0)