天天看点

从零开始搭建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,相比之下,我认为这个东西现在在小型项目中的使用应该是没问题的,别用到复杂的工程中,会乱掉;