IDEA + Maven + SpringBoot + Mybatis + Thymeleaf + Swagger实现一个web项目

前言

Project创建

创建项目,从New Project开始,图文教程优于文案教程,所以创建过程已图文为主
项目创建图文步骤一:当https://start.spring.io无法连接时也可以使用custom的https://start.aliyun.com项目创建图文步骤一:项目的创建方式
项目创建图文步骤二
项目创建图文步骤二项目创建图文步骤三项目创建图文步骤三:项目的依赖配置
项目创建图文步骤四项目创建图文步骤四:项目的本地配置

初始项目结构说明

初始项目结构介绍图
到此,以上内容为整个项目个创建过程。
接下来就是启动项目验证,本地的启动方式有两种,如下图
项目启动图文

启动方式一:项目创建时自动配置好的启动类,也可通过配置Edit Configurations进行配置启动类为SpringbootApiApplication
启动方式二:值得注意的是SpringbootApiApplication.java中有main方法,所以可直接在SpringbootApiApplication类中右键任何位置进行run

初始项目配置

到此,项目虽然创建起来了,但只是创建过程中添加了部分基础组件,IDEA却无法在项目中进行自动生成配置,比如没有配置访问时的项目名、没有配置端口号、没有配置数据库等等,这些都是需要手动配置的,那么以下则为个人总结的初始项目需要配置的内容

个人习惯配置

对于SpringBoot项目的配置,有两种文件格式的配置方式,均可使用,所以遵循个人喜好即可
1、在application.properties中进行配置(默认生成的配置文件)
2、在application.yml中进行配置(层级更简洁,个人偏向)

Maven配置

下图仅为个人习惯配置,因为我个人本地有自己的Maven库,所以指定使用我自己的
项目个人配置内容图文一:本地Maven仓库的指定

数据库连接驱动JDBC的配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/itsdf07_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password:
    driver-class-name: com.mysql.jdbc.Driver

以上为个人平时的默认配置,useUnicode=true&characterEncoding=utf-8&useSSL=false这些为JDBC所携带的配置参数,请根据需要进行修改即可
如不配置上述JDBC的驱动内容,则无法启动项目,且出现异常信息如下

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-12 10:00:02.943 ERROR 7796 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Disconnected from the target VM, address: '127.0.0.1:61130', transport: 'socket'

Process finished with exit code 1

异常原因其实很清楚,我们创建项目过程,只添加了组件,并没有配置组件的原因。到此,项目便可正常启动了,启动日志如下

2020-03-12 10:04:00.469  INFO 4196 --- [           main] com.itsdf07.SpringbootApiApplication     : Starting SpringbootApiApplication on DESKTOP-JQDVDVV with PID 4196 (D:\Workspaces\api\springboot-api\target\classes started by itsdf07 in D:\Workspaces\api\springboot-api)
2020-03-12 10:04:00.482  INFO 4196 --- [           main] com.itsdf07.SpringbootApiApplication     : No active profile set, falling back to default profiles: default
2020-03-12 10:04:01.774  WARN 4196 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.itsdf07]' package. Please check your configuration.
2020-03-12 10:04:02.318  INFO 4196 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-12 10:04:02.328  INFO 4196 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-12 10:04:02.329  INFO 4196 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-12 10:04:02.422  INFO 4196 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-12 10:04:02.422  INFO 4196 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1808 ms
2020-03-12 10:04:02.661  INFO 4196 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-12 10:04:02.801  WARN 4196 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-03-12 10:04:03.088  INFO 4196 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-12 10:04:03.093  INFO 4196 --- [           main] com.itsdf07.SpringbootApiApplication     : Started SpringbootApiApplication in 3.603 seconds (JVM running for 5.773)

经常带着疑问以及有看log习惯的人总能发现一写细节问题,如在启动过程的日志中,出现了JDBC的驱动警告提示Loading class com.mysql.jdbc.Driver’. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary
其实这个因为是JDBC连接Mysql不同版本时使用的驱动不匹配
com.mysql.jdbc.Driver 是 mysql-connector-java 5中的
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的
而项目在创建过程中添加组件时,均是默认最新版本,所有可在下图中查看当前项目中使用的Mysql版本就明白了
查看当前项目使用的mysql组件版本
解决方案就是驱动与Mysql版本对应即可,而修改方式有如下两种
1、Mysql6(含6)以上使用JDBC的com.mysql.cj.jdbc.Driver驱动
2、Mysql6以下使用JDBC的com.mysql.jdbc.Driver驱动
poc.xml中指定Mysql组件的版本配置如下

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!--<scope>runtime</scope>-->
    <version>5.1.47</version><!--指定版本号-->
</dependency>

Tomcat配置

由于SpringBoot的打包特性是jar包,且使用默认集成的Tomcat服务器进行部署,而我们的项目部署之后访问规范中是根据项目名称以及接口名称进行访问,且要保证端口不与其他项目冲突,那么我这边也遵循其优点,进行配置自己项目名称以及端口号

server:
  port: 17081 #项目端口号
  servlet:
    context-path: /itsdf07 #访问的项目名

备注:SpringBoot项目同样也可以打成war包,并且使用自行配置的Tomcat服务器进行部署,但是本文不做这方面的说明!

Mybatis数据库映射配置

mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.aso.store.entity  # 注意:对应实体类的路径
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # 配置控制台打印sql查询语句并附带查询结果数据

配置Mybatis Generator自动生成代码

pom.xml中增加mybatis generator 自动生成代码插件

<!-- mybatis generator 自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.1</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>

resources中增加mybatis generator配置文件
增加generatorConfig.xml
配置mybatis generator文件的启动方式
配置mybatis generator的启动方式
在这里插入图片描述
启动方式
验证自动生成结果
验证自动生成结果

Thymeleaf配置

#  ###ThymeLeaf配置
thymeleaf:
  ##配置模板引擎:是让controller层到templates文件夹寻找xx.html(src/main/resources/templates)
  prefix: classpath:/templates/ #配置前缀,可以不配置,因为静态页面存放的默认位置就是该templates
  suffix: .html #配置后缀,也是可以不用配置,也是默认html
  cache: false #启用模板缓存。
  encoding: UTF-8 #模板编码
  content-type: text/html; charse=utf-8 #Content-Type值
  check-template: true #在呈现模板之前检查模板是否存在。
  check-template-location: true #检查模板位置是否存在。
  enabled: true #启用MVC Thymeleaf视图分辨率。
  excluded-view-names: #应该从解决方案中排除的视图名称的逗号分隔列表。
  mode: HTML5 #应用于模板的模板模式。另请参见StandardTemplateModeHandlers。
  template-resolver-order: #链中模板解析器的顺序。
  view-names: #可以解析的视图名称的逗号分隔列表。

Thymeleaf模板引擎的配置,如果只是简单的页面跳转,则可以不用配置,因为静态页面的默认存放路径就是在classpath:/templates/路径下

解决跨域问题

出于安全原因,浏览器禁止Ajax调用驻留在当前原点之外的资源

使用区分不同开发环境下的配置文件

在一个规范的项目运作中,从项目的研发到最后的上线生成过程中,应该要经过四种项目环境
1、本地开发环境,即dev版本
2、测试环境,即test版本
3、预生产环境,即pre版本
4、生产环境,即prod,且一般只有运维可以操作
在以上4中环境中,如果只用一个配置文件来达到四种环境下的不同配置,那么真的是一件不小的苦力活,而且还无法保证其改动的百分百正确性,那么java的面向对象封装思想就又体现出了其优势:即把共性的部分提取出来,不同环境的差异配置到不同环境的配置文件中,然后通过spring.profiles.active进行切换,切换规则为根据application-*.yml命名中*的内容进行配置
不同开发环境下的配置文件
从上图,可以看出,原本在application.yml中配置的端口号以及JDBC连接数据库的驱动配置已经看不见了,那么如果项目最起码的不配置JDBC连接数据库的驱动配置,项目肯定是启动不了的,那么配置到哪里去了呢?这里已开发环境和预生产环境的配置文件作为例子进行截图对比
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值