SpringBoot使用MyBatisPlus自动创表-Controller层无法访问,任何Url都报404

找了老半天,发现大意了。。。。开始项目一直能正常运行。但是加了mybatis.actable.jar自动创建表格后,启动项目,任何url都访问不了,controller和拦截器都失效了。开始觉得启动类和controller在同级别下,就没往注解扫描器的问题。耗时老半天。。。。。所以特意写个笔记记录以下避免下次犯蠢!

首先说一下actable.jar的使用:

一.导入依赖:

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.5.3.1</version>
</dependency>
<dependency>
	<groupId>com.gitee.sunchenbin.mybatis.actable</groupId>
	<artifactId>mybatis-enhance-actable</artifactId>
	<version>1.5.0.RELEASE</version>
</dependency>

二.yml配置

mybatis:
  table:
    auto: update
    #create系统启动后,会将所有的表删除掉,然后根据model中配置的结构重新建表,该操作会破坏原有数据。
    #update系统会自动判断哪些表是新建的.哪些字段要修改类型等,哪些字段要删除,哪些字段要新增,该操作不会破坏原有数据。#none系统不做任何处理。
    #add新增表/新增字段/新增索引新增唯一约束的功能,不做做修改和删除(只在版本1.0.9.RELEASE及以上支持)。model:
  model:
    pack: com.example.demo.entity #扫描用于创建表的对象的包名,多个包用"."隔开
  database:
    type: mysql #数据库类型目前只支持mysql
mybatis-plus:
  #1.如果是mybatis直接在mybatis下增加该配置。
  #2.如果使用properties配置方式,要写成mapperLocations
  mapper-locations: classpath*:mapper/*.xml,classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml #第一个是自己写xml的路径,第二个是固定的

三.注解

@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"})
@ComponentScan("com.gitee.sunchenbin.mybatis.actable.manager.*")
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}
@Table(name = "test")
public class Test {
    @TableId(type = IdType.AUTO)
    @IsKey
    @IsAutoIncrement
    @Column
    private Integer id;

    @Column(name = "create_time",comment = "创建时间")
    private LocalDateTime createTime;//对应数据库的datetime

    @Column(name = "update_time",comment = "修改时间")
    private Timestamp updateTime;//对应数据库的datetime
}

URL报错:localhost:8080/test

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu May 11 00:00:04 CST 2023

There was an unexpected error (type=Not Found, status=404).

就首页可以访问,默认映射的index首页

思考半天,觉得处理器Handler都没注入到Spring IOC容器中,之前都能正常访问呀,Controller层和启动类也都在一层呀。

最后看了下启动类,配置actable的注解的时候,

@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"})
@ComponentScan("com.gitee.sunchenbin.mybatis.actable.manager.*")
@SpringBootApplication
public class DemoApplication

加了ComponentScan注解,指定了mybatis.actable.manager包的位置,所以不管你自己的Controller层和启动类是否在同级目录下,都不会再去默认扫描启动类同级目录下的所有包了,只扫描@ComponentScan中指定的包了,所有这时候就要添加指定自己项目的需要开启注解扫描的包:

{"com.gitee.sunchenbin.mybatis.actable.manager.*","com.example.demo"}
@MapperScan({"com.gitee.sunchenbin.mybatis.actable.dao.*"})
@ComponentScan({"com.gitee.sunchenbin.mybatis.actable.manager.*","com.example.demo"})
@SpringBootApplication
public class DemoApplication

就完美解决了。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Spring BootMyBatis-Plus 进行项目重构,可以遵循以下步骤来完成: 1. 添加依赖:在项目的 `pom.xml` 文件中,添加 Spring BootMyBatis-Plus 的相关依赖。例如: ```xml <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> ``` 请确保将 MyBatis-Plus 的最新版本号替换到 `<version>` 标签中。 2. 配置数据库连接:在 `application.properties` 或 `application.yml` 文件中配置数据库连接信息,包括数据库 URL、用户名和密码等。 3. 创建实体类:根据项目需求,在 Java 包中创建实体类,用于映射数据库表结构。 4. 创建 Mapper 接口:创建一个继承自 MyBatis-Plus 的 `BaseMapper` 接口的 Mapper 接口,用于定义数据库操作方法。 ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface YourEntityMapper extends BaseMapper<YourEntity> { // 在此定义自定义的数据库操作方法 } ``` 5. 编写 Mapper XML 文件:在 resources 目录下创建与 Mapper 接口对应的 XML 文件,编写 SQL 语句以实现数据库操作。 6. 创建 Service :创建一个 Service 来处理业务逻辑,并注入 Mapper 接口的实例。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class YourEntityService { private final YourEntityMapper yourEntityMapper; @Autowired public YourEntityService(YourEntityMapper yourEntityMapper) { this.yourEntityMapper = yourEntityMapper; } // 在此编写业务逻辑方法 } ``` 7. 创建 Controller :创建一个 Controller 来处理 HTTP 请求,并注入 Service 的实例。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/your-entity") public class YourEntityController { private final YourEntityService yourEntityService; @Autowired public YourEntityController(YourEntityService yourEntityService) { this.yourEntityService = yourEntityService; } // 在此定义接口方法,处理请求 } ``` 8. 运行项目:启动 Spring Boot 应用程序,可以使用 `@SpringBootApplication` 注解标记启动类,并在其中添加 `main` 方法。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 以上是使用 Spring BootMyBatis-Plus 进行项目重构的基本步骤。你可以根据实际需求进行更多的配置和开发工作,如添加其他依赖、配置缓存、编写业务逻辑等。 希望以上内容对你有所帮助!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值