从零开始完整搭建 Spring-Boot 项目开发框架的教程
前言
难度:简单
类型:step-by-step
适用:初学者,完全没有接触过 Spring-Boot
开发环境:jdk 1.8
关键词:java, sring-boot, spring-mvc, restful
笔者环境:macOS
1. 项目创建
1.1 使用脚手架生成项目
打开 http://start.spring.io/ 网站,选择Gradle
,依赖项选择Web(网站)
、DevTools(开发工具)
、JPA(ORM)
、MySQL(数据库)
、Thymeleaf(模板)
。
如下图所示:
![](http://upload-images.jianshu.io/upload_images/2419521-b9b7f13c33463028.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
2. 项目配置
2.1 配置 application.properties
打开~demo/src/main/resource/application.properties
文件,添加必要配置,如下:
#服务端口
server.port=8012
#mysql 配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mysql 链接字符串
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false
#mysqly用户名和密码
spring.datasource.username=your_mysql_name
spring.datasource.password=your_mysql_password
#jap 配置,是否输出 sql
spring.jpa.show-sql=true
#thymeleaf配置
#开发环境关闭缓存
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
#设置使用非严格的HTML5校验
spring.thymeleaf.mode=LEGACYHTML5
#调整id生成策略
spring.jpa.hibernate.use-new-id-generator-mappings=false
2.2 配置 Application 主入口
打开类文件 demo/src/main/java/com/example/demo/DemoApplication.java
2.2.1 添加注解@ComponentScan
组件扫描,如果不添加,可能无法正确的@Autowired
2.2.2 添加注解@Configuration
声明配置,告诉 Spring,该类是bean 的配置类
2.2.3 完成后的样子
如下图:
![](http://upload-images.jianshu.io/upload_images/2419521-54d0e478758f1bb5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
3. Resftful 接口的示例
3.1 添加数据表的 Entity 实体类
3.1.1假设有一个数据表test_user_info
结构如下图 :
![](http://upload-images.jianshu.io/upload_images/2419521-4d6df17dc3d7f457.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
插入一些测试数据:
![](http://upload-images.jianshu.io/upload_images/2419521-ddd3bfb923065a1b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/255)
3.1.2 新建包名 entities
其中新建 Entity 实体 TestUserInfo
,完整如下图:
![](http://upload-images.jianshu.io/upload_images/2419521-db450e28e0d331de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/253)
其中@Entity
声明这是一个数据实体,因为表名和类名不一致,所以用@Table
告诉 Jpa,这个表名的具体名字。
@Id
声明了主键,@GeneratedValue
声明了主键是自增的,
@Column
声明了字段名的真实名字,Jpa 大小写是代表下划线分割,所以只能传全部小写的字段名。
3.2 新建 Repository
3.2.1 新建包名 repositories
新建接口 TestUserInfoRepository
,该接口继承自,,需要JpaRepository<T,ID>
,T
就是数据实体类,ID
是类 id 字段的类型,如下图:
![](http://upload-images.jianshu.io/upload_images/2419521-d70c16eb45c6d700.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
@Repository
声明,告诉Spring-Boot,这是一个仓储的组件。
3.2.2 创建查询
因为 JpaData,采用的是规范契约的方式来声明查询,所以只要按照规范来创建接口中的方法,框架会自动生成对应的 sql 语句。如果有特殊需求,也可以用声明的方式写 sql 语句,例如:
![](http://upload-images.jianshu.io/upload_images/2419521-547230d4ba881539.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
上下两条语句的效果一样,区别就是第一条按照规格来写,框架会自动提示字段的名字,定义了方法,框架会自动解析 sql,第二条框架会读取@Query
中的 sql 使用。
3.3 创建 Service 服务
3.3.1 新建包名 services,
新建服务类TestInfoService,如下图
![](http://upload-images.jianshu.io/upload_images/2419521-f2f0ac9243a75ea6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/268)
```@Service`` 声明了这是一个服务类。
3.3.2 关联 Repository
如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-248cae8bdfbc1c88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/428)
因为 TestUserInfoRepository 声明了@Repository
,所以能够使用@Autowired
进行关联,这里要说明一点,SpringMvc 中的@Controller
,@Service
,@Repository
本质都是@Component
,Spring-Boot 本质上能够使用@Autowired
进行关联的必须是有@Component
声明,或者其子继承声明的才行。
3.3.3 创建查询方法
如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-92f025c72ac006dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
一个简单的服务查询类和方法。
3.4 创建 Model 实体
3.4.1 创建报名 models
TestUserInfoResponseModel用于接口返回相应 json 使用,如下图
![](http://upload-images.jianshu.io/upload_images/2419521-0cc2a5b747dc8df1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/346)
3.5 创建 Restful 的 Controller
3.5.1 创建包名 controllers
ApiController 类,如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-79a1b32cb89d0ac8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/234)
@RestController
声明这是一个Restful 的控制器,@RequestMapping
声明了 url-route,是/api
开头的。
3.5.2 关联 service
如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-207af2cd6e554944.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/420)
同 Repository
一样,TestUserInfoService
声明了@Service
,所以他可以被@Autowired
,前面讲过原因了。
3.5.3 创建 aciton方法
如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-d9b6fc737bdf50fb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
@RequestMapping
,声明了 action 的 url-route,那么这个action 的完整路径就是/api/get_user
另外,因为是 Restful 的 controller ,所以,如果不声明@ResponseBody
的话,就必须返回一个对象实体,框架会自动序列化成一个 json。
3.5.4 创建返回 plain 字符串的action
如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-d5dd2b713d4f4f4a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/295)
声明了@ResponseBody
,接口机会以纯文本的形式返回。
4. 请求测试
4.1 运行项目
4.1.1 点击运行,看到控制输出如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-cc69129afc000621.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
看到端口的展示,代表运行成功了。
4.1.2 请求get_user接口
地址为:http://localhost:8012/api/get_user?name=test1
![](http://upload-images.jianshu.io/upload_images/2419521-40288c28ed24733a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/434)
4.1.3 请求 get_plain 接口
地址为:http://localhost:8012/api/get_plain?name=test1
![](http://upload-images.jianshu.io/upload_images/2419521-4ca77ae9d859b083.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/439)
4.1.4 至此,Restful 的项目 Demo 创建完毕。
5. 创建动态Web项目
5.1 创建 HomeController
并且关联service
,如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-03e517ef363e8c57.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/400)
@Controller
声明这是一个 web 的控制器,/home
是 url-route
5.2 创建 action
如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-b249b38fbcda0b38.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
@RequestMapping中 value 可以多一个,空白,代表可以省略,/home/index,或者/home都是请求这个 action。
方法返回的 String
,是 ViewName
,比如新建一个 index.html
的模板,这里使用就是返回字符串index
。
参数中的 Model
是在模板中使用的 Model
实体,示例中把查询出来的实体,装入了 model
的字典中,这样在前台模板就能使用。
5.3 另外一种 aciton
如果不太喜欢返回字符串,可以使用另外一种,如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-dc5eaa811effdcfa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/482)
返回 ModelAndView
,这样自己实例化的 ModelAndView
,传入模板的名字,还有前台需要使用的 model
,就能避免返回字符串,我个人推荐使用第一种方式,会比较灵活。
5.4 创建模板页面
在/resource/templates
中创建index.html
,如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-5cec25ddca572841.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/403)
因为我们使用的是thymeleaf
模板,所以html
中声明了 xmlns=th="http://www.thymeleaf.org"
,这样在 html 种就能使用模板属性去绑定数据,控制元素。
span
中的 th:text=“${model?.id}
”,告诉了模板,这个span
的text
设置为model?.id
的值。带上?
问号的意思是,model
可能为 null
,这样不会报错,null
的时候,text
就是空。
model
就是我们在 action
传给 model
的名字,id
,name
,age
,就是 model
的字段。
5.5 运行请求
请求地址:http://localhost:8012/home/index?name=test1
如下图,
![](http://upload-images.jianshu.io/upload_images/2419521-f3761b6dfc4e0c1f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/433)
请求另一个 aciton:
http://localhost:8012/home/test
![](http://upload-images.jianshu.io/upload_images/2419521-813be00c1ac712ef.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/433)
5.6 至此动态 web的示例结束
作者:mcjiffy
链接:https://www.jianshu.com/p/512f84c439d8
來源:简书
从零开始完整搭建 Spring-Boot 项目开发框架的教程的更多相关文章
- Spring Boot系列学习文章(一) -- Intellij IDEA 搭建Spring Boot项目
前言: 最近做的一个项目是用Spring Boot来做的,所以把工作中遇到的一些知识点.问题点整理一下,做成一系列学习文章,供后续学习Spring Boot的同仁们参考,我也是第一次接触Spring ...
- Myeclipse下使用Maven搭建spring boot项目
开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...
- Spring Boot入门(一):搭建Spring Boot项目
从本篇博客开始,我们开始进入Spring Boot的世界,它的出现使Spring的开发变得更加简洁,因此一经推出受到众多程序员的喜爱. 作为Spring Boot系列的第一篇博客,我们先来讲解下如何搭 ...
- 基于 intellij IDEA 快速搭建Spring Boot项目
在<一步步搭建 Spring Boot maven 框架的工程>一文中,已经介绍了如何使用Eclipse快速搭建Spring Boot项目.由于最近将开发工具由Eclipse ...
- 使用IDEA,Eclispe搭建Spring Boot项目
如何创建一个Spring Boot项目?这里使用maven来进行依赖管理,根据常用的IDE,可以使用IDEA.Eclipse.或者访问官方网站搭建. 项目搭建环境准备 JDK:1.8 MAVEN:3. ...
- Myeclipse下使用Maven搭建spring boot项目(第二篇)
现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...
- Spring boot入门(一):快速搭建Spring boot项目
(一)Spring boot介绍 本部分摘自:https://www.zhihu.com/question/64671972/answer/223383505 Spring Boot是由Pivotal ...
- 构建微服务:快速搭建Spring Boot项目
Spring Boot简介: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...
- 基于gralde搭建spring boot项目
搭建基于gradle的sprint boot项目,swagger-ui辅助 spring boot官网:http://projects.spring.io/spring-boot/get start ...
- 快速搭建Spring Boot项目
Spring boot是Spring推出的一个轻量化web框架,主要解决了Spring对于小型项目饱受诟病的配置和开发速度问题. Spring Boot 包含的特性如下: 创建可以独立运行的 Spri ...
随机推荐
- SLAM Course - WS13/14 by Cyrill Stachniss (1) 课程资源汇总
本帖是作者学习SLAM 课程笔记的资源帖,汇总了SLAM Course - WS13/14 by Cyrill Stachniss 的相关资源. 1. 课程网站,有相关课件作业和教学视频下载. htt ...
- ECC 算法
一.简介 1)椭圆曲线密码学的初级读本 http://8btc.com/thread-1240-1-1.html 2)ECC加密算法入门介绍 http://www.pediy.com/kssd/ped ...
- python 类变量 在多线程下的共享与释放问题-乾颐堂
最近被多线程给坑了下,没意识到类变量在多线程下是共享的,还有一个就是没意识到 内存释放问题,导致越累越大 1.python 类变量 在多线程情况 下的 是共享的 2.python 类变量 在多线程情况 ...
- [SoapUI] 在某个测试步骤下面增加Script Assertion,运用 messageExchange 获取response content
import com.eviware.soapui.support.GroovyUtils import com.eviware.soapui.support.XmlHolder import org ...
- 并发编程CAS操作
并发编程CAS操作 简介 CAS即compare and swap,中文就是比较并交换 CAS是Java并发包的基石 原理 其实CAS的原理相对来说比较简单.将要被改变的数据和期望的值作比较,当两个值 ...
- ServiceStack.Redis.RedisNativeClient的方法“get_Db”没有实现。
项目中用到redis,用nuget导入,但是运行时遇到问题 Exception: “Com.JinYiWei.Cache.RedisHelper”的类型初始值设定项引发异常.System.TypeIn ...
- yii2 Html::a
Html::a($text,$url = null,$options = []) $url 可以直接是字符串 // An empty string. This will return the curr ...
- Python下载网页图片
有时候不如不想输入路径,那就需要用os模块来修改当前路径 下面是从其他地方看到的一个例子,就是把图片url中的图片名字修改,然后就可以循环保存了,不过也是先确定了某个url 来源:http://www ...
- 数独高阶技巧入门之三——Fish
术语Fish代表了一组工作原理相同的关于特定候选数的解题技巧(Fish技巧直接产生自数独规则——每个单元内的数字都不能重复),Fish家族成员包括“体型”从小到大的X-Wing.Swordfish. ...
- c# 跨线程访问窗体UI
定义个结构体用于存储线程中传递的参数信息 struct ImgInfo { public string url; public string path; }; 参数传递到线程中 ImgInfo img ...