天天看點

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

剛開始學習使用Springboot,想簡單的記錄一下自己學習的過程。同時也講我知道的分享出去,就這麼簡單;有什麼錯誤,請指正;至于概念什麼的,我就不說了,自己百度吧;

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

第一步:前期準備

1.開發工具使用的idea 2017

2.資料庫使用的是mysql

3.導包用的是maven

4.java version “1.8.0_181”

5.測試工具使用的是postman

第二步:開始建立工程

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務
從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務
從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務
從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務
從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

建立完的工程如下所示:

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

第三步:配置資料庫

在application.properties 中配置一下;

如圖所示

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

記得在本地用mysql建立個dbtest的資料庫哦;

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

第四步:寫個controller驗證一下;

建立一個包叫controller

建立一個HelloController的類

如圖所示

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

為了驗證,我們點選啟動一下項目

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

接下來我們使用postman通路一下;看看能不能通路到?

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

我的通路成功了,你的呢?這個步驟完成了;項目其實是可以使用的了,為了動态,我們需要建立個資料庫表格;

第五步:幹活之前先清楚項目的結構,我的很簡單,如圖所示吧

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

1.建立一個資料庫表,使用了jpa,不用像以前,先在資料庫建立表,然後在連接配接表,這個是可以直接建立的。一個bean生成一個表;(ps:這個是需要标簽的,具體标簽學習,後面再詳細介紹)在bean中,生成get和set方法,再重寫他的toString方法,為了我們後面列印;

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

2.下面我們要連接配接這個表,建立jpa,然後就可以對這個表中的資料進行想要的操作了;如下圖所示:

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

這就搞定了;

3.接下來建立service:

如圖所示:

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

4.下面我們開始建立controller,我了解就是提供對外通路的;

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

這裡留個問題:為什麼不直接傳回一個List 就OK了呢?

5.我們重新開機下服務,看看這個表是否生成

我的報錯了,我擦擦,翻車了,錯誤提示如下:

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

老夫檢查了下,結果是application中的jpa沒配置,增加jpa的配置如下:

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

之後老夫通路了一下,結果成功了。如圖所示:

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

這麼看不出來效果,來我們給資料庫中增加下資料,然後通路下這個網址:

從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務
從零開始搭建SpringBoot+MySql+JPA 打造簡單的背景服務

新增:Pom.xml

<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.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>FirstProject</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.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>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>


		<!-- https://mvnrepository.com/artifact/org.web3j/core -->
		<dependency>
			<groupId>org.web3j</groupId>
			<artifactId>core</artifactId>
			<version>3.1.1</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

           

到這裡看到我們想要的資料了,這個簡單的工程就完成了;是不是很簡單?

總結:springboot 我個人感覺,這個jpa挺好用的,少寫了很多sql,相比之下,我認為這個東西現在在小型項目中的使用應該是沒問題的,别用到複雜的工程中,會亂掉;