是接上篇文章,我们创建好的模块项目如下:
然后我们需要删除 不必要的Application 启动类,我们只需要保留一个在controller 模块就好,同时我们也需要删除不必要的 application.properties 配置文件 目前只保留,dao 模块 和 controller 模块的 配置文件(当然也可以只保留一个配置)
删完后就是这个样子的,接下来我们就需要配置,配置文件以及依赖关系,写一些简单的代码,让我们的项目跑起来
我们先配置dao 模块:下面是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>demo</groupId>
<artifactId>demo_dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_dao</name>
<!--引用父类-->
<parent>
<artifactId>demo</artifactId>
<groupId>com.ssm.creation</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<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.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>demo</groupId>
<artifactId>demo_entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
注意:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.1.9</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
他们是有区别的:且2个jar 最后不要同时存在,因为在配置文件里面,稍有不慎,就会引发
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
且难排查,我后面会出一篇文章,整理出现该问题的所有原因
mybatis-plus中的MybatisPlusAutoConfiguration覆盖了mybatis中的MybatisAutoConfiguration。
下面是2个jar 在配置文件里面的引用路径,千万不要错了
mybatis:
mapper-locations: classpath:smallarge/small_base/mapper/xml/*Mapper.xml
mybatis-plus: mapper-locations: classpath*:/mybatis/mapper/*Mapper.xml
1.创建UserMapper 和 UserMapper.xml 注意:类和xml 的名字最好对标,方便管理
2.UserMapper 加注解 @Repository 是因为 等会service 模块引用 UserMapper 的时候 会报红,我们需要用@Repository 来标注组件
# 应用名称 spring: datasource: url: jdbc:mysql://localhost:3306/network?useUnicode=true&characterEncoding=UTF-8&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 #mybatis mybatis-plus: mapper-locations: classpath*:/mybatis/mapper/*Mapper.xml type-aliases-package: demo.demo_entity configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl check-config-location: true
到这里我们dao 模块的设置就已经完成了 注意: 不要忘记了 包与包之间的依赖
这里我们开始创建Controller 类,这里会报一个找不到这个bean的错误,这是因为没有扫描到这个bean,这里有两种方式
第一种 启动类标注 @ComponentScan第二种:自定义注解(推荐,启动类代码干净整洁)
所以这里我们要创建一个自定义注解,来把所有需要扫描的包搞进去,然后直接添加注解就可以了(自定义的这个注解类,可以放进commons 这里我为了省事就直接扔Controller了)
一丶创建 CommonConfig.class
package demo.demo_controller.utils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
/**
* @program: demo
* @description:
* @create: 2022-02-11 22:08:10
**/
@Component
@ComponentScan(basePackages = {"demo.demo_commons","demo.demo_controller","demo.demo_dao","demo.demo_entity","demo.demo_service"})
@MapperScan("demo.demo_dao.mapper")
public class CommonConfig {
}
二丶创建EnableCommon 注解
package demo.demo_controller.utils;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* ClassName: EnableCommon <br/>
* Description: <br/>
* date: 2022/2/11 22:14<br/>
*
* @since JDK 1.8
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(CommonConfig.class)
public @interface EnableCommon {
}
三丶 启动类引用
接下来 Controller 模块 我们需要删除pom 里面不必要的东西
<?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>demo</groupId>
<artifactId>demo_controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_controller</name>
<packaging>jar</packaging>
<parent>
<artifactId>demo</artifactId>
<groupId>com.ssm.creation</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<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.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>demo</groupId>
<artifactId>demo_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<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>
</plugin>
</plugins>
</build>
</project>
注意:如果删除不干净,会报版本冲突的错误,我们只需要打一个包所以 Controller 设定jar 就行
service 丶 dao丶entity丶commons 模块的pom.xml 把build 部分全部删除了就行
运行结果:
有不清楚的地方 留言