目录
1、创建工程spring-boot-jpa
2、配置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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mi</groupId>
<artifactId>spring-boot-jpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-jpa</name>
<description>spring-boot-jpa project for Spring Boot</description>
<properties>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-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>
3、配置application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
#更新或创建表结构
ddl-auto: update
#控制台显示sql
show-sql: true
database-platform: org.hibernate.dialect.MySQLDialect
4、创建User实体类
package com.mi.entity;
import javax.persistence.*;
/**
* Created by chengwen on 2019/7/3.
*/
//使用JPA注解配置映射关系
@Entity //告诉JPA这是一个实体类(和数据表映射的类)
@Table(name = "t_user") //@Table来指定和哪个数据表对应,如果省略默认就是user
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class User {
@Id //这是一个主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //自增主键
private Integer id;
@Column(name ="last_name",length = 50) //这是和数据表对应的一个列
private String lastName;
@Column //省略默认列名就是属性名
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
5、创建数据库及数据表
CREATE TABLE `jpa`.`t_user` (
`id` INT NOT NULL AUTO_INCREMENT ,
`last_name` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) );
6、编写一个Dao接口
package com.mi.repository;
import com.mi.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Created by chengwen on 2019/7/3.
*/
//继承jpa来完成对数据库的操作
public interface UserRepository extends JpaRepository<User,Integer>{
}
7、创建UserController
package com.mi.controller;
import com.mi.entity.User;
import com.mi.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;
/**
* Created by chengwen on 2019/7/3.
*/
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id){
User user = userRepository.getOne(id);
return user;
}
@GetMapping("user")
public User insertUser(User user){
User usr = userRepository.save(user);
return usr;
}
}
8、测试
新增
http://localhost:8080/user?lastName=zhangsan&email=zhangsan@163.com
结果
{"id":1,"lastName":"zhangsan","email":"zhangsan@163.com"}
查询
http://localhost:8080/user/1
结果
{"id":1,"lastName":"zhangsan","email":"zhangsan@163.com"}