创建spring boot+Mybatis项目全流程
第一步:打开IDEA 【New -> Project】
第二步:选择Spring Initializr
第三步:输入Name以及相关配置,可以参考截图
注意:Server URL 出可以替换为阿里云镜像【https://start.aliyun.com】速度更快
第四步:选择相关依赖
我这里只是选择了一些基本的依赖,大家可以按需添加,或者后面再在pom文件里面添加依赖也可以。
第五步:配置数据源,打开【application】文件
server:port: 8080 #设置端口号#数据源
spring:datasource:#配置需要连接的数据库表名【kg_one】url: jdbc:mysql://localhost:3306/kg_one?seUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8username: *** #数据库账号password: *** #数据库密码driver-class-name: com.mysql.cj.jdbc.Driver
如果提示【com.mysql.cj.jdbc.Driver】有问题,请检查pom文件中是否有以下依赖
<!-- JDBC --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency>
<!-- Mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></dependency>
<!-- Mybatis --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency>
到这一步,不出意外的话,您单击小绿以后就能看到如下界面了。恭喜您,搭建完毕,可以开始了。
以下是完整的pom文件以及application文件供参考
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.feituxia</groupId><artifactId>feituixia-admin</artifactId><version>0.0.1-SNAPSHOT</version><name>feituixia-admin</name><description>feituixia-admin</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>
<!-- JDBC --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId></dependency>
<!-- Mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.29</version></dependency>
<!-- Mybatis --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><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>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></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.feituxia.feituixiaadmin.FeituixiaAdminApplication</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
application.yaml
server:port: 8080#数据源
spring:datasource:url: jdbc:mysql://localhost:3306/kg_one?seUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8username: wendaopassword: 123456Hxkdriver-class-name: com.mysql.cj.jdbc.Driver