实习周记(一):花了5天的增删改查
学习内容
核心框架:Spring Boot
持久层框架:Mybaits-Plus
交互界面:LayUI
编写流程
创建SpringBoot项目
通过官网选择想要的依赖,生成jar包,用ide作为maven项目打开即可
也可以通过ide直接创建springboot项目,大概率因为访问超时而创建失败
- 依赖列表
Lombok //Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法
Spring-Web //提供了核心 HTTP 集成,包括一些便捷的 servlet 过滤器, Spring HTTP 调用,用于集成其它 web 框架的基础结构以及技术
Thymeleaf //spring boot支持的适用于 Web 和独立环境的现代服务器端 Java 模板引擎
JDBC API //连接数据库
项目配置
springboot默认的全局配置文件有两个,一个是application.properties
,另一个是application.yml
- 在
src\main\resources\application.properties
中进行配置
#####自定义的配置信息,即SpringBoot中没有提供的配置,是我们自己额外提供的动态配置信息###########
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8
#############################SpringBoot内置的配置信息,它会自动读取注入
#mybatisplus配置
mybatis-plus.mapper-locations=mapper/*.xml
mybatis-plus.configuration.use-column-label=true
mybatis-plus.configuration.auto-mapping-behavior=full
mybatis-plus.configuration.map-underscore-to-camel-case=true
#配置SpringBoot默认的日志环境,开启打印SQL语句的Debug模式,语法:logging.level.<mapper所在包名>=debug
logging.level.com.example.crud.mapper=debug
#Tomcat端口号 默认是8080端口
server.port=8088
# Thymeleaf模板引擎配置
#开启模板缓存(默认值:true)
spring.thymeleaf.cache=false
#在呈现模板之前检查模板是否存在
spring.thymeleaf.check-template=true
#检查模板位置是否正确(默认值:true)
spring.thymeleaf.check-template-location=true
#Content-Type的值(默认值:text/html)
spring.thymeleaf.servlet.content-type=text/html
#开启MVC Thymeleaf视图解析(默认值:true)
spring.thymeleaf.enabled=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#要被排除在解析之外的视图名称列表,用逗号分隔
spring.thymeleaf.excluded-view-names=
#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
spring.thymeleaf.prefix=classpath:/templates/
#在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.suffix=.html
可以减少我们代码中一些路径的书写,比如通过
#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
spring.thymeleaf.prefix=classpath:/templates/
#在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.suffix=.html
可以让我们在controller写跳转接口时,直接写对应页面名字,不需要写绝对路径
- yaml配置的优点在于易于阅读和实现,采用键值对的方式,但需要严格遵守书写格式
server:
port: 18082
具有层级关系,同一层的键应该对齐,且:后应该增加一个空格
项目架构
一般采用常用的MVC三层架构,model,view,controller(详细解释请上网看博客),目的在于解耦和,提高代码复用性,
而根据需求,又可以将其分为以下几层,但整体架构方式不变
- controller
负责具体业务模块流程的控制,主要用于调用service层的接口来控制业务流程,可以是页面跳转,也可以是返回数据
- entity(实体类)
实体类是用于对必须存储的信息和相关行为建模的类,与数据库中的表一一对应
- mapper
数据持久层,负责对数据库的数据进行操作
设计接口,并通过xml文件编写sql语句
- model
对请求与响应的数据格式进行一些限制(通过实体类的形式),这一层可以不要
- service
业务层,调用mapper层的接口,根据业务需求将获取到数据进行处理,为controller所用
简单的业务一般直接编写service类,调用mapper层接口进行数据处理
复杂的页面一般会采用接口与实现类的方式,设计接口,然后编写接口的实现类,为controller层提供服务
增删改查
- 在
entity
中创建User类,与数据库中表的对应 - 在
mapper
中编写UserMapper
接口,并继承BaseMapper<T>
,继承的类为mybatis-plus的写好的CURD类,有常用的增删改查,当有更复杂的sql语句时,设计接口,然后在resources
中添加对应的UserMapper.xml
,编写sql语句 - 在
service
中添加UserService
实现类,调用Usermapper,对数据进行加工,并提供给controller层(一般通过方法) - 在
controller
中调用UserService
的方法,并将其返回给前端界面 - 前端界面通过
ajax
或者Thymeleaf
进行通信,获取后端返回数据
其中存在的问题
- mybatis-plus是对mybatis进行的一个功能扩展,但并未改变其内容
- mybatis-plus中内置的CRUD有两种,一种为mapper层继承的BaseMapper,一种为service中继承的iservice,后者可以当成前者的一个扩展
核心
各司其职,每一层都专注于自身功能的实现
多看文档,网上博客一般或多或少都会有错漏(血泪教训)
文档
Spring Boot:https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools.restart
Mybatis-Plus:https://mp.baomidou.com/
Thymeleaf:https://www.thymeleaf.org/(了解即可)