likes
comments
collection
share

手撸一个简单的springboot项目

作者站长头像
站长
· 阅读数 43

前言

今天带大家手撸一个springboot项目(前后端不分离),基于springboot,mybatis,layUI实现

项目基本结构

手撸一个简单的springboot项目

  1. 首先后缀名为SystemApplication的类为项目的启动类,其代码形式基本如下所示:

手撸一个简单的springboot项目

  • 注解一位声明其为一个springboot工程
  • 注解二声明其持久层扫描的位置
  1. application.properties为配置文件,完成数据库以及一些项目的基本配置,本项目的一些基本配置如下:
# 应用名称
spring.application.name=scientific_research_management_system
# 应用服务 WEB 访问端口
server.port=8085
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
mybatis.mapper-locations=classpath:mappers/*xml
# 数据库连接地址
spring.datasource.url=jdbc:mysql://0.0.0.0:3306/jiangsustore?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=XXX
spring.datasource.password=XXX
# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=true
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html

#日志输出
logging.level.root=warn
#日志级别
logging.level.com.xie.scientific_research_management_system.persistence=trace
#日志输出内容
logging.pattern.console=%p%m%n

# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
#指定Mybatis的实体目录

mybatis.type-aliases-package=com.xie.scientific_research_management_system.domain
  1. domain层,也就是实体层,一般对应数据库中数据类型

手撸一个简单的springboot项目

  1. persistent层,也就是大家所说的Dao层,实现对数据库操作的封装,对数据的增删改查,因为项目中使用了mybatis持久层框架,因此不用写那些数据库连接以及操作的代码,其与resources中的mappers层相对应,例如,持久层中StudentAccountMapper函数getAccountByStudent_idAndPassword(),函数意义为通过传入的参数获取用户信息, 手撸一个简单的springboot项目 其在.xml文件中实现:

手撸一个简单的springboot项目 mybatis官方文档

  1. service层:实现的是业务模块的逻辑应用设计,其是建立在持久层上的,处理具体的事务逻辑,良好的设计可以有效减少项目的代码量。

  2. controller层:负责具体业务模块流程的控制,其对具体事务的实现是直接调用service层的接口。

手撸一个简单的springboot项目

  1. 刚刚mappers层上面已提及,static层以及templates层就是我们常说的view视图层,其中static中主要存储的是一些静态资源,比如css文件,图片,字体,视音频等等,另外static中的index.html为项目的初始页面,运行项目后就会访问到这个文件。而templates层中的结构就视具体的项目而定了。

后记

关于springboot项目前后端分离怎样实现,可以参考这两篇文章:

转载自:https://juejin.cn/post/7008089386940268574
评论
请登录