這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何解析springboot整合JPA過程,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)公司、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了浙江免費(fèi)建站歡迎大家使用!
接下來具體看看是怎么弄的。
1、新建一個springboot項(xiàng)目,選擇web、data jdbc、data jpa、MySQL driver。
2、建立以下目錄及結(jié)構(gòu):
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 https://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.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gong</groupId> <artifactId>springbootjpa</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootjpa</name> <description>Demo 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-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
3、在application.yml中配置連接數(shù)據(jù)庫和jpa相關(guān)配置
spring: datasource: url: jdbc:mysql://192.168.124.22:3306/jpa username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: #更新或者創(chuàng)建數(shù)據(jù)表結(jié)構(gòu) ddl-auto: update #控制臺顯示SQL show-sql: true
4、新建一個entity包,新建實(shí)體類User.java
package com.gong.springbootjpa.entity;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;import javax.persistence.*;//使用JPA注解配置映射關(guān)系@Entity //告訴JPA這是一個實(shí)體類(和數(shù)據(jù)表映射的類)@Table(name = "tbl_user") //@Table來指定和哪個數(shù)據(jù)表對應(yīng);如果省略默認(rèn)表名就是user;@JsonIgnoreProperties(value = {"hibernateLazyInitializer","handler"})public class User { @Id //這是一個主鍵 @GeneratedValue(strategy = GenerationType.IDENTITY)//自增主鍵 private Integer id; @Column(name = "last_name",length = 50) //這是和數(shù)據(jù)表對應(yīng)的一個列 private String lastName; @Column //省略默認(rèn)列名就是屬性名 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、新建一個repository包,新建一個UserRepository.java
package com.gong.springbootjpa.repository;import com.gong.springbootjpa.entity.User;import org.springframework.data.jpa.repository.JpaRepository;//繼承JpaRepository來完成對數(shù)據(jù)庫的操作,在JdbcRepository中指定實(shí)體類,數(shù)據(jù)庫中主鍵對應(yīng)的java類型public interface UserRepository extends JpaRepository<User,Integer> {}
6、新建一個controller包,新建一個UserController.java
經(jīng)過上述配置之后,我們就可以直接利用UserRepository中的一些方法進(jìn)行數(shù)據(jù)庫的操作啦,是不是很方便。
package com.gong.springbootjpa.controller;import com.gong.springbootjpa.entity.User;import com.gong.springbootjpa.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;@RestControllerpublic 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 save = userRepository.save(user); return save; }}
7、啟動服務(wù)器
插入一條數(shù)據(jù)
查詢一條數(shù)據(jù)
上述就是小編為大家分享的如何解析springboot整合JPA過程了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:如何解析springboot整合JPA過程
瀏覽地址:http://www.chinadenli.net/article24/jcoice.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、外貿(mào)網(wǎng)站建設(shè)、做網(wǎng)站、自適應(yīng)網(wǎng)站、動態(tài)網(wǎng)站、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)