mybatis plus 两个包实体名字相同启动报错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authClientController' defined in file [E:\www\java\work\ssjf49_bison\bison-service\bison-system\target\classes\com\bison\system\controller\AuthClientController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authClientServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authClientMapper' defined in file [E:\www\java\work\ssjf49_bison\bison-service\bison-system\target\classes\com\bison\system\mapper\AuthClientMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.apache.ibatis.type.TypeException: The alias 'Goods' is already mapped to the value 'com.bison.oms.entity.Goods'.
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:144)
at com.bison.core.launch.BisonApplication.run(BisonApplication.java:37)
at com.bison.system.SystemApplication.main(SystemApplication.java:18)
问题描述:
项目中两个包中有相同名字的实体类,项目启动时报错‘The alias 'Goods' is already mapped to the value 'com.bison.oms.entity.Goods'’
mybatisplus 两个包实体名字相同
MyBatis-Plus 中两个包的实体名称相同会导致冲突,因为框架需要通过实体类的完全限定名来区分不同的映射关系。
解决方案:
-
使用别名:在 MyBatis-Plus 的配置中为其中一个实体类指定别名。
@TableName(value = "table_name") @Alias("CustomGoodsCate") public class EntityName { // ... }
-
使用不同的包名:将两个实体类放在不同的包中,这样它们的完全限定名就不同了。
-
使用 @MapperScan 注解指定 Mapper 接口的包路径,避免扫描到错误的包。
@MapperScan(basePackages = "com.example.mapper1") @SpringBootApplication public class Application { // ... }
-
使用 @Mapper 注解指定具体的 Mapper 接口位置。
@Mapper(value = "com.example.mapper1.EntityMapper") public interface EntityMapper { // ... }