使用IDEA创建SpringBoot+MyBatis+MySQL进行项目搭建
1.创建项目
文件 --> 新建 --> 项目 填写项目基本信息 项目名称、存放位置、项目语言、项目类型、JDK版本、打包
Web选项选择Spring Web
SQL选项中勾选JDBC API、MyBatis Framework、MySQl Driver选项
至此一个空项目创建完毕(查看pom.xml maven依赖文件如下)
<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo1</artifactId><version>0.0.1-SNAPSHOT</version><name>demo1</name><description>demo1</description><properties><java.version>11</java.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.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
此时运行启动类文件报错如下:
原因分析:原因是由于没有配置数据源信息
解决方法:需要在application.properties文件中配置数据源信息
spring.datasource.url=jdbc:mysql://localhost:3306/scoreDB?serverTimezone=UTC&&characterEncoding=UTF-8
spring.datasource.password=123456
spring.datasource.username=root
至此运行成功,默认端口号8080 访问地址:http://localhost:8080
2.参数配置(在application.properties配置文件中进行相关配置)
#端口号配置
server.port=9099#数据源相关配置
spring.datasource.url=jdbc:mysql://localhost:3306/scoreDB?serverTimezone=UTC&&characterEncoding=UTF-8
spring.datasource.password=123456
spring.datasource.username=root#mybatis映射对应的实体类位置
mybatis.type-aliases-package=com.learn.system.pojo
#mybatis的映射xml文件路径位置
mybatis.mapper-locations=classpath:/mapper/*Mapper.xmlspring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.html