當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
1、SpringBoot整合JPA
生活随笔
收集整理的這篇文章主要介紹了
1、SpringBoot整合JPA
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
1、創建工程spring-boot-jpa
2、配置pom.xml
3、配置application.yml
4、創建User實體類
5、創建數據庫及數據表
6、編寫一個Dao接口
7、創建UserController
8、測試
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=UTCusername: rootpassword: rootdriver-class-name: com.mysql.jdbc.Driverjpa:hibernate:#更新或創建表結構ddl-auto: update#控制臺顯示sqlshow-sql: truedatabase-platform: org.hibernate.dialect.MySQLDialect4、創建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 {@AutowiredUserRepository 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"}?
總結
以上是生活随笔為你收集整理的1、SpringBoot整合JPA的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3、SpringBoot整合MyBati
- 下一篇: 1、在Linux虚拟机上安装 docke