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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值