本章介绍SpringBoot与JPA整合
整合流程
1、新建一个SpringBoot Web项目
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-jpa</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>2.1.8.RELEASE</version> 16 </parent> 17 18 <properties> 19 20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 22 <java.version>1.8</java.version> 23 </properties> 24 25 <dependencies> 26 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-web</artifactId> 30 </dependency> 31 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-data-jpa</artifactId> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-jdbc</artifactId> 41 </dependency> 42 43 <!-- mysql --> 44 <dependency> 45 <groupId>mysql</groupId> 46 <artifactId>mysql-connector-java</artifactId> 47 <version>8.0.12</version> 48 </dependency> 49 50 <dependency> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-starter-test</artifactId> 53 <scope>test</scope> 54 </dependency> 55 56 </dependencies> 57 58 59 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 60 <build> 61 <plugins> 62 <plugin> 63 <groupId>org.springframework.boot</groupId> 64 <artifactId>spring-boot-maven-plugin</artifactId> 65 </plugin> 66 </plugins> 67 </build> 68 </project>
2、引入JPA依赖和JDBC依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-jpa</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-jdbc</artifactId> 9 </dependency>
3、添加配置信息
1 spring: 2 datasource: 3 username: admin 4 password: 123456 5 url: jdbc:mysql://127.0.0.1:3306/test_jpa 6 driver-class-name: com.mysql.jdbc.Driver 7 jpa: 8 # 底层通过hibernate来实现的 9 hibernate: 10 # 更新或者创建数据表结构 11 ddl-auto: update 12 # 控制台显示SQL 13 show-sql: true
4、编写一个User实体类
1 package com.test.springboot.entity; 2 3 import org.springframework.context.annotation.Configuration; 4 5 import javax.persistence.*; 6 7 // 使用JAP注解配置映射关系 8 9 // 告诉JPA这是一个实体类(和数据表映射的类) 10 @Entity 11 // @Table来指定和那个数据表对应;如果省略默认表名就是user; 12 @Table(name="jpa_user") 13 public class User { 14 15 // 这是一个主键 16 @Id 17 @GeneratedValue(strategy = GenerationType.IDENTITY) 18 private Integer id; 19 20 @Column(name = "last_name", length = 50) 21 private String lastName; 22 23 // 省略默认列名就是属性名 24 @Column 25 private String email; 26 27 public Integer getId() { 28 return id; 29 } 30 31 public void setId(Integer id) { 32 this.id = id; 33 } 34 35 public String getLastName() { 36 return lastName; 37 } 38 39 public void setLastName(String lastName) { 40 this.lastName = lastName; 41 } 42 43 public String getEmail() { 44 return email; 45 } 46 47 public void setEmail(String email) { 48 this.email = email; 49 } 50 }
5、编写一个UserRepository类
package com.test.springboot.repository; import com.test.springboot.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Integer> { }
6、编写一个UserController类
1 package com.test.springboot.controller; 2 3 import com.test.springboot.entity.User; 4 import com.test.springboot.repository.UserRepository; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.PathVariable; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import java.util.Optional; 11 12 @RestController 13 public class UserController { 14 15 @Autowired 16 UserRepository userRepository; 17 18 @GetMapping("/user/{id}") 19 public User getUser(@PathVariable("id") Integer id) { 20 Optional<User> optional = userRepository.findById(id); 21 if(optional.isPresent()) { 22 return optional.get(); 23 } 24 return null; 25 } 26 27 28 @GetMapping("/user") 29 public User insertUser(User user){ 30 User user1 = userRepository.save(user); 31 return user1; 32 } 33 }
7、启动SpringBoot Web项目进行测试,查看启动日志,可以看到它自动生成了表
浏览器输入地址http://localhost:8080/user?lastName=%E6%9D%8E%E5%9B%9B&email=xx@163.com,新增用户
查看用户,输入地址:http://localhost:8080/user/1