引言:该文档是参考尚硅谷的关于springboot教学视屏后整理而来。当然后面还加入了一些自己从网上收集整理而来的案例!
一、SpringData JPA初步使用
1. springdata简介
2. springboot整合springdata jpa(这是一个最简单的集成案例)
pom.xml引入架包依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.atguigu</groupId> <artifactId>spring-boot-06-data-jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-06-data-jpa</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>View Code
application.yml中配置如下:
spring: datasource: url: jdbc:mysql://192.168.15.22/jpa username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: # 更新或者创建数据表结构 ddl-auto: update # 控制台显示SQL show-sql: trueView Code
实体类User:
package com.atguigu.springboot.entity; import lombok.Data; import javax.persistence.*; //使用JPA注解配置映射关系 @Entity //告诉JPA这是一个实体类(和数据表映射的类) @Table(name = "tbl_user") //@Table来指定和哪个数据表对应;如果省略默认表名就是user; @Data public class User { @Id //这是一个主键 @GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键 private Integer id; @Column(name = "last_name",length = 50) //这是和数据表对应的一个列 private String lastName; @Column //省略默认列名就是属性名 private String email; }View Code
数据层接口UserRepository:
package com.atguigu.springboot.repository; import com.atguigu.springboot.entity.User; import org.springframework.data.jpa.repository.JpaRepository; //继承JpaRepository来完成对数据库的操作 public interface UserRepository extends JpaRepository<User,Integer> { }View Code
Controller层UserController:这里省略了service,主要是为了测试集成springdata jpa
package com.atguigu.springboot.controller; import com.atguigu.springboot.entity.User; import com.atguigu.springboot.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired UserRepository userRepository; @GetMapping("/user/{id}") public User getUser(@PathVariable("id") Integer id){ User user = userRepository.findOne(id); return user; } @GetMapping("/user") public User insertUser(User user){ User save = userRepository.save(user); return save; } }View Code
测试(这里只是通过get请求简单的测试):
查询用户id=2的员工,访问网址:http://localhost:8080/user/2
返回结果:{"id":2,"lastName":"唐嫣","email":"123@qq.com"}
增加一个用户,访问网址:http://localhost:8080/user?lastName=王菲&email=aa
返回结果为:{"id":3,"lastName":"王菲","email":"aa"}