项目梳理
SpringBoot项目结构
一般项目分问三层:
前端控制器(Controller)
数据服务层(Service)
数据接口访问层(Dao/Mapper)
注解
层 | 构成 | 注解 | 说明 | 作用 |
---|---|---|---|---|
Controller层 | 就由一个类(class)构成 | @Controller | 放于类上 | |
Service层 | 接口(interface)+类(class) | @Service | 放于类上 | |
Dao/Mapper层 | 由接口(interface) 构成 | @Mapper | 放于接口上 | |
Application启动类 | @ComponentScan | 要想项目划分结构,就必须在启动类(xxxApplication)上配置@ComponentScan(basePackages = “com.xxx.mall”); SpringBoot在没配置@ComponentScan的情况下,会默认扫描启动类所在的包以及子包下的类。例如:比如 RuoYiApplication这个是在com.ruoyi包下,那么spring boot默认扫描启动类所在的包以及子包,那么com.ruoyi.xx.xx都会扫描到 | 扫描配置了@Component、@Service、@Repository、@Controller注解的类并且装入bean容器进行管理 | |
Application启动类 | @MapperScan | @MapperScan({“com.xxx.mapper”})指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类 | 用于批量注解Mapper接口 | |
一般在业务实现层impl | @Autowired | 用来进行依赖注入;@Autowired 先根据类型(byType)查找,如果存在多个(Bean)再根据名称(byName)进行查找 | @Autowired 支持属性注入、构造方法注入和 Setter 注入;如果注入的是 Mapper 层的对象,编译器会提示报错信息 | |
一般在业务实现层impl | @Resource | 用来进行依赖注入;@Resource 先根据名称(byName)查找,如果(根据名称)查找不到,再根据类型(byType)进行查找 | @Resource 只支持属性注入和 Setter 注入 |
重点:
@Mapper与@MapperScan不可同时使用;
@Mapper用于注解单个mapper接口
@Mapper Scan用于批量注解Mapper接口
@Mapper不起作用时,因为缺少下图中的依赖包
如果mapper类没有在Spring Boot主程序可以扫描的包或者子包下面,可以使用如下方式进行配置:
@SpringBootApplication
@MapperScan({"com.kfit.*.mapper","org.kfit.*.mapper"})
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
配置文件
项目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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>reactBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>reactBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--thymeleaf模板引擎配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Web依赖-->
<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.0</version>
</dependency>
<!--MySQL数据库配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<!--单元测试配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<!--HuTool为我们提供的一些便捷工具。-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>src/main/resources/generator/generator-config.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
src/main/resource下面的yml文件
spring:
#数据库链接配置
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#mybatis配置文件
mybatis:
#配置映射文件,classpath指的是类路径,也就是编译之后的target文件夹下的WEB-INF/class文件夹
# classpath 可以理解为 target/WEB-INF/class
mapper-locations: classpath:mapper/*.xml
# 配置这个可以少写路径,就可以在 mapper的xml中直接简写实体类名称 (不是必须的)
type-aliases-package: com.example.reactboot.bean
项目结构图片