
本文分为几个部分,
首先是在idea中新建项目,
然后是配置 项目的目录(新建controller、service、dao等),
然后是自定义的一些工具类(比如启动后打印地址等)。
=======================================================
1.、创建篇
新建项目,如下图所示
new 一个project
选择Spring Initializr。
因为spring官网经常连不上,所以用国内镜像。
填写Group、Artifact和Name后点击next,如下图所示。

选择需要的组件:基本组件有web模块中的web、SQL模块中的MySQL、JDBC和Mybatis、额外的组件可以根据需求自行选择,不过需要注意的是springboot版本不要选择太高,此处选择的是
2.6.13版本,Java 8 不要选3以上版本。


5.确定项目名和项目存储路径。

6.点击finish,整个项目架构如下图所示。其中(项目名+Application).class为sprinboot核心类、application.properties为主配置文件。

2.配置篇
1.连接数据库配置(因为我们已经在选择组件中选择了SQL和JDBC)
所以不需要手动引入Maven依赖,只需要直接配置就好),
(.yml)和(.properties)的配置格式是不一样的,我个人比较喜欢使用yml格式的,
因此我将application.properties改为了application.yml。

将properties后缀名改为yml,点击refactor即可完成修改。

具体配置如下,(注:yml配置格式相对教严格,需要注意以下各内容的具体位置,比如冒号后边要有个空格)

driver-class-name为数据库连接驱动,username和password为填写自己想要连接的数据库用户名和密码,url中的spring为我本地中的一个数据库,使用utf-8编码格式,不开启ssl验证。
完整的yml 配置:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://10.11.11.11:3306/库名?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false
server:
port: 8066
mybatis:
mapper-locations: classpath:mapper/*.xml
只要改下username 、password、url 中蓝色部分。
2.在startProject\src\main\java\com.test.demo中,创建MVC模式对应的package,如下
image
domain包主要用来集中管理相应的实体类,controller为控制层,service和serviceImpl分别为服务层和服务实现层,mapper用来管理的是与mybatis结合后存储的接口类,类似于传统的dao层。
3.在startProject\src\main\resources中,创建mapper文件夹,存放mapper对应的mapper.xml文件。

蓝色新建的几个文件夹是手动创建的包
4.配置项目端口号和配置定位mybatis的mapper文件夹的路径。其中开放的端口号设置为8066(可自行指定端口,但是不可与其他开放的端口有冲突,不指定时默认为8080端口)。
mapper-location指定了扫描mapper中的所有以xml为后缀名的文件。
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://10.11.11.11:3306/库名?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false
server:
port: 8066
mybatis:
mapper-locations: classpath:mapper/*.xml
3.自定义一些配置
实现效果:

首先先下载,通用代码
https://download.csdn.net/download/hzp666/89146459
把这3个文件放到项目下:

效果这样

然后添加一些依赖,修改下pom.xml文件
其中如果自己要打包,部署的话,这个需要注释掉

借鉴下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sinopharm</groupId>
<artifactId>jybackend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jybackend</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.36</version>
</dependency>
<!-- hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<!-- <configuration>-->
<!-- <mainClass>com.sinopharm.jybackend.JybackendApplication</mainClass>-->
<!-- <skip>true</skip>-->
<!-- </configuration>-->
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>central</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<layout>default</layout>
<!-- 是否开启发布版构件下载 -->
<releases>
<enabled>true</enabled>
</releases>
<!-- 是否开启快照版构件下载 -->
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
然后修改下启动主程序:
1.在class 类上添加注解
@MapperScan(basePackages = "com.sinopharm.jybackend.demos.web.dao")

- main 方法改成:
public static void main(String[] args)throws UnknownHostException {
System.out.println("run");
ConfigurableApplicationContext application = SpringApplication.run(JybackendApplication.class, args);
log.info("..######..##.....##..######...######..########..######...######.\n" +
".##....##.##.....##.##....##.##....##.##.......##....##.##....##\n" +
".##.......##.....##.##.......##.......##.......##.......##......\n" +
"..######..##.....##.##.......##.......######....######...######.\n" +
".......##.##.....##.##.......##.......##.............##.......##\n" +
".##....##.##.....##.##....##.##....##.##.......##....##.##....##\n" +
"..######...#######...######...######..########..######...######.");
Environment env = application.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String path = env.getProperty("server.servlet.context-path");
if (StringUtils.isEmpty(path)) {
path = "";
}
log.info("\n----------------------------------------------------------\n\t" +
"Application is running! Access URLs:\n\t" +
"Local访问网址: \t\thttp://localhost:" + port + path + "\n\t" +
"External访问网址: \thttp://" + ip + ":" + port + path + "\n\t" +
"----------------------------------------------------------");
}
然后如果程序报错了,那可能是导包不对,alt + enter 改一下
=============================================================
报错:
org.yaml.snakeyaml.scanner.ScannerException: while scanning a simple key
原因:
因为.yml
如
server:
port:8081
如果port:8081的键值如果没有用:空格把键值分开,就会报这个错
解决方案:
加个空格,正常的 port 应该是橘黄色的,没有空格或格式不对,会变白色字体

Consider defining a bean of type '******.dao.SaveWordDAO' in your configuration.
原因:
主程序类没有扫描到dao
解决方法:
在主程序上添加扫描路径
@MapperScan(basePackages = "com.sinopharm.jybackend.demos.web.dao")

: 忽略输入 jybackend-0.0.1-SNAPSHOT.jar中没有主清单属性
原因:
打jar包后,Java -jar 启动结果报错。因为pom.xml中build 有一部分配置错误。
解决办法:
把这个注释掉
