SpringCloud微服务系列博客:

SpringCloud微服务之快速搭建EurekaServer:
https://blog.csdn.net/egg1996911/article/details/78787540
<https://blog.csdn.net/egg1996911/article/details/78787540>
SpringCloud微服务之注册服务至EurekaServer:
https://blog.csdn.net/egg1996911/article/details/78859200
<https://blog.csdn.net/egg1996911/article/details/78859200>
SpringCloud微服务之集成thymeleaf访问html页面/静态页面&热部署:
https://blog.csdn.net/egg1996911/article/details/78885045
<https://blog.csdn.net/egg1996911/article/details/78885045>
SpringCloud微服务之部署SpringBoot项目至Linux服务器(CentOS):
https://blog.csdn.net/egg1996911/article/details/78975945
<https://blog.csdn.net/egg1996911/article/details/78975945>
SpringCloud微服务之跨服务调用后端接口:
https://blog.csdn.net/egg1996911/article/details/80369182
<https://blog.csdn.net/egg1996911/article/details/80369182>

本文介绍如何使用spring boot框架搭建一个基本的后端服务,以从前端加载后端图片这个功能为例。

1、使用intellij idea建立一个spring boot项目。(如果有多个微服务项目,同样推荐使用new
module的方式建立项目,这样可以在一个工程里同时查看所有项目)

修改基本信息就好,dependencies的引用可以直接next跳过,在第二步中修改pom.xml就好,记得第三步中要注意项目路径:


多个Module在一个工程中:


2、修改pom.xml,添加必要dependencies:

* MyBatis:使用mysql数据库+mybatis框架
* Eureka:用来将微服务注册到Eureka Server上 <?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> <groupId>com.deng</groupId> <artifactId>image
</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name
>microimage</name> <description>Image microservice</description> <properties> <
project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <
project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <
java.version>1.8</java.version> </properties> <parent> <groupId>
org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</
artifactId> <version>Camden.SR7</version> </parent> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</
artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</
groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <
dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>
mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </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、配置数据库和实现DAO层(数据层):

1)
修改appilcation.properties。如果将所有微服务放在一台服务器上部署或者在本地运行时,注意端口号(server.port)不要重复,否则会报端口被占用的错误:
server.port=8089 spring.application.name=microimage eureka.client.serviceUrl
.defaultZone=http://127.0.0.1:8761/eureka/ #database spring.datasource.url
=jdbc:mysql://localhost:3306
/site_microimage?useUnicode=true&characterEncoding=utf-8 spring.datasource
.username=root spring.datasource.password=123456 spring.datasource.driver
-class-name=com.mysql.jdbc.Driver
2) 根据业务需求设计实体bean,并在数据库中手动建立bean对应的表:
设计Image实体类,用来保存图片的描述和图片文件名,图片id使用自增方式生成。因此Image的Bean代码和数据库表设计如下所示:
package com.deng.bean; public class Image { private int id; private String
description;private String fileName; public int getId() { return id; } public
StringgetDescription() { return description; } public String getFileName() {
return fileName; } public void setId(int id) { this.id = id; } public void
setDescription(String description) { this.description = description; } public
void setFileName(String fileName) { this.fileName = fileName; } }
数据库表中的column名最好与bean中的属性名一致,这样在编写sql语句时会方便一些:


3) 实现dao层,编写关于Image的添加和删除方法:
ImageDao的接口类:
package com.deng.dao; import com.deng.bean.Image; import org.springframework
.stereotype.Repository; import java.util.List; @Repository public interface
ImageDao { int addImage(Image image); List<Image> getAllImages(); }
ImageDao的mapper文件(自增id的配置为useGeneratedKeys=”true”
keyProperty=”id”,keyProperty处填写自增id的在bean中的属性名):
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace=
"com.deng.dao.ImageDao"> <insert id="addImage" parameterType=
"com.deng.bean.Image" useGeneratedKeys="true" keyProperty="id"> INSERT INTO
`image`(`description`,`fileName`) VALUES(#{description},#{fileName})</insert> <
select id="getAllImages" resultType="com.deng.bean.Image"> SELECT * FROM `image`
</select> </mapper>
注意(很重要!!!),可能引起MyBatis报错的原因如下:

*
dao的接口类放在java文件夹下,mapper文件放在resouces下,不过最终要保证的都是它们编译后的路径要一致,否则dao会无法匹配到mapper
* 检查mapper中的namespace(接口类路径)、parameterType、resultType是否填写正确
* 检查mapper中的方法名是否和接口类中的方法名匹配,参数是否匹配


4) 修改启动类,增加注册eureka客户端功能和扫描mapper(@MapperScan)功能:
package com.deng; import org.mybatis.spring.annotation.MapperScan; import org
.springframework.boot.SpringApplication; import org.springframework.boot
.autoconfigure.SpringBootApplication; import org.springframework.cloud.client
.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication
@MapperScan("com.deng.dao") public class MicroImageApplication { public static
void main(String[] args) { SpringApplication.run(MicroImageApplication.class,
args); } }
3、添加测试数据:
1) 在test包下添加ImageDaoTest测试类:

package com.deng.dao; import com.deng.bean.Image; import org.junit.Test; import
org.junit.runner.RunWith;import
org.springframework.beans.factory.annotation.Autowired;import
org.springframework.boot.test.context.SpringBootTest;import
org.springframework.test.context.junit4.SpringRunner;/** * Created by deng on
2018/5/18. */ @RunWith(SpringRunner.class) @SpringBootTest public class
ImageDaoTest { @Autowired private ImageDao imageDao; @Test public void addImage
()throws Exception { for (int i = 1; i <= 9; i++) { imageDao.addImage(new Image(
"示例图片" + i, i + ".jpg")); } } }
运行ImageDaoTest成功后,查看数据库,可以发现数据已经成功插入,说明MyBatis+MySQL数据库配置成功:


2)
在resources下新建static文件夹,static文件夹下新建img文件夹,将1.jpg-9.jpg放入resources/static/img下:



之后检测图片是否能够在网页中访问,运行MicroImageAppliction类,在浏览器中访问localhost:8089/img/1.jpg,可以正常显示:


4、实现Service层和Controller层:
package com.deng.controller; import com.deng.bean.Image; import com.deng
.service.ImageService; import org.springframework.beans.factory.annotation
.Autowired; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org
.springframework.web.bind.annotation.RestController; import java.util.List;
@RestController @RequestMapping(value ="/images") public class ImageController
{ @Autowired private ImageService imageService; @RequestMapping(value =
"/getAll", method = RequestMethod.GET) public List<Image> generate() { return
imageService.getAllImages(); } } package com.deng.service; import com.deng.bean
.Image; import java.util.List; public interface ImageService { List<Image>
getAllImages(); } package com.deng.service.impl; import com.deng.bean.Image;
import com.deng.dao.ImageDao; import com.deng.service.ImageService; import
org.springframework.beans.factory.annotation.Autowired;import
org.springframework.stereotype.Service;import java.util.List; @Service public
class ImageServiceImpl implements ImageService { @Autowired private ImageDao
imageDao;@Override public List<Image> getAllImages() { return
imageDao.getAllImages(); } }
再次运行MicroImageAppliction,输localhost:8089/images/getAll,可以发现接口能够成功访问:


至此,后端服务基本框架的搭建已经完成。可以看到项目使用分层思想,整体分为三层(controller-service-dao)。

项目源码地址:https://github.com/LeiDengDengDeng/MySite
<https://github.com/LeiDengDengDeng/MySite>中的microimage。

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信