SpringBoot+Mybatis框架简单搭建
一、整体架构
Springboot搭建最重要的三个部分:
- pom.xml:pom文件,引入你需要依赖的jar包
- application.properties:配置项目端口以及数据库等
- application.class:项目启动类,注意必须要放在外层
二、详解
1、pom.xml
添加项目依赖jar包
-
核心依赖,项目启动:
-
其他依赖,允许实现web交互、aop等功能:
- 具体代码:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot_crystal_test</groupId>
<artifactId>design</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<!-- Spring核心依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.6</version>
</dependency>
<!-- PostgreSQL驱动 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.9</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2、application.properties
添加项目配置信息
# 项目端口
server.port=8119
# mybaits配置
mybatis.mapper-locations=classpath:/mappers/*.xml
# 数据库连接配置
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/test
spring.datasource.username=postgres
spring.datasource.password=postgres
3、application.class
springboot启动类
package springboot.crystal.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Program: design
* @Author: CRYSTAL_LUCKY
* @Date: 14:25 2021/7/28
* @Description: 启动类
*/
@SpringBootApplication
@MapperScan("springboot.crystal.test.mappers") //mapper层所在位置,也可以选择在mapper层上加注解@Mapper
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class);
}
}
Mapper
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class);
}
}
三、测试
简单写了一个测试类
成功获取结果