Mybatis工程升级到FluentMybatis后引发的问题以及解决方法

0. 背景交代

为了提高开发速度,我打算将公司原有`Mybatis`框架升级为`FluentMybatis`。可是遇到了一系列问题,下面开始爬坑

工程结构示意如下:

src/
├── main
│   ├── java.com.demo
│   │      ├── Application.java                          //SpringBoot启动类
│   │      ├── aop
│   │      │   └── GlobalExceptionHandler.java     //全局异常处理
│   │      ├── config
│   │      │   └── FluentMybatisGenerator.java     //FluentMybatis配置类
│   │      ├── controler
│   │      │   ├── CommodityController.java        //新功能,使用FluentMybatis
│   │      │   └── FeedbackController.java         //老功能,使用Mybatis
│   │      ├── enums
│   │      ├── mapper
│   │      │   └── FeedbackMapper.java             //MybatisMapper
│   │      ├── mybatis                         //Mybatis拓展
│   │      │   ├── enums                           //枚举类
│   │      │   ├── fluent                          //FluentMybatis代码生成目录
│   │      │   │   ├── dao
│   │      │   │   │   ├── impl
│   │      │   │   │   │   └── CommodityDaoImpl.java //FluentMybatis生成的DAO实现
│   │      │   │   │   └── intf
│   │      │   │   │       └── CommodityDao.java     //FluentMybatis生成的DAO接口
│   │      │   │   └── entity
│   │      │   │       └── CommodityEntity.java  //FluentMybatis生成的数据模型
│   │      │   ├── handler
│   │      │   │   └── StringListHandler.java    //自定义类型转换器(VARCHAR <==> 字符串列表)
│   │      │   └── module
│   │      │       └── StringList.java           //字符串列表
│   │      ├── plugins
│   │      │   ├── files
│   │      │   └── render
│   │      ├── pojo
│   │      │   └── Feedback.java                 //MyBatis数据模型
│   │      ├── service
│   │      │   ├── CommodityService.java         //新功能Service接口
│   │      │   ├── FeedbackService.java          //老功能Service接口
│   │      │   └── impl
│   │      │        ├── CommodityServiceImpl.java    //新功能Service接口实现
│   │      │        └── FeedbackServiceImpl.java     //老功能Service接口实现
│   │      ├── shiro
│   │      └── utils
│   └── resources
│       ├── application-dev.yml
│       ├── application-prod.yml
│       ├── application.yml
│       ├── logback.xml
│       └── mybatis
│           ├── mapper
│           │   └── FeedbackMapper.xml                //Mybatis Mapper XML文件
│           └── mybatis-config.xml
└── test
    └── java

主要涉及到的配置文件FluentMybatisGenerator内容:

package com.demo.config;

import cn.org.atool.fluent.mybatis.spring.MapperFactory;
import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Column;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import com.demo.mybatis.handler.StringListHandler;
import com.demo.mybatis.module.StringList;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * FluentMybatisConfigurer
 *
 * @author adinlead
 * @desc
 * @create 2023/3/6 13:34 (星期一)
 */
@Configuration
@MapperScan({"com.demo.mapper"})
public class FluentMybatisGenerator {
    /**
     * 定义fluent mybatis的MapperFactory
     */
    @Bean
    public MapperFactory mapperFactory() {
        return new MapperFactory();
    }

    /**
     * 生成代码类
     */
    @Tables(
            srcDir = "src/main/java",
            basePack = "com.demo.mybatis.fluent", //注意,此处生成代码的路径与原生Mybatis不在同一个包下
            daoDir = "src/main/java",
            tablePrefix = {"tb_"},
            tables = {@Table(value = {"tb_commodity"}, columns = {
                    @Column(value = "create_time", insert = "now()"),
                    @Column(value = "update_time", insert = "now()", update = "now()"),
                    @Column(value = "cover_images", javaType = StringList.class, typeHandler = StringListHandler.class)
            })})
    static class CodeGenerator {
    }
}
  1. 问题以及解决

1.1 FluentMybatis无法自动生成实体类

现象:在运行后,没有在com.demo.mybatis.fluent中生成entity和dao

解决方法:

在FluentMybatisGenerator.java中,在返回自定义fluent mybatis的MapperFactory之前,执行FileGenerator.build方法:

    @Resource
    private DataSource dataSource;

    /**
     * 定义fluent mybatis的MapperFactory
     */
    @Bean
    public MapperFactory mapperFactory() {
        FileGenerator.build(dataSource, CodeGenerator.class);
        // 引用配置类,build方法允许有多个配置类
        return new MapperFactory();
    }

mvn执行clean 后 再次运行项目即可

效果如下

1.2 自定义SqlSessionFactoryBean导致的一系列问题

官方WIKI-环境部署-简单示例中看到了如下代码:

@Configuration
@MapperScan({"你的Mapper package"})
public class ApplicationConfig {
    @Bean("dataSource")
    @Bean
    public DataSource newDataSource() {
        // TODO
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(newDataSource());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // 以下部分根据自己的实际情况配置
        // 如果有mybatis原生文件, 请在这里加载
        bean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLazyLoadingEnabled(true);
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        return bean;
    }

    // 定义fluent mybatis的MapperFactory
    @Bean
    public MapperFactory mapperFactory() {
        return new MapperFactory();
    }
}

在此示例中,作者重定义了SqlSessionFactoryBean,但是如果你没能正确配置,那么这一行为也会导致一系列错误。

1.2.1 Spring无法找到需要注入的对象

错误如下:


Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-08 13:17:02 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type 'com.demo.mybatis.fluent.mapper.CommodityMapper' that could not be found.


Action:

Consider defining a bean of type 'com.demo.mybatis.fluent.mapper.CommodityMapper' in your configuration.

这个一般是Spring 进行DI时,找不到FluentMybatis的Mapper引起的错误,原因在于官方文档中要求你在@MapperScan中添加MybatisMapper包路径,但是如果你的MybatisMapper和FluentMybatisMapper不在一个包中时,也需要把FluentMybatis生成的Mapper路径也添加到扫描器中,或者干脆放大扫描范围。

  1. 添加FluentMybatisMapper包路径(推荐)

@Configuration
@MapperScan({"com.demo.mapper", "com.demo.mybatis.fluent.mapper"})
public class FluentMybatisGenerator {
    ...
}
  1. 扩大扫描范围

@Configuration
@MapperScan({"com.demo"})
public class FluentMybatisGenerator {
    ...
}
1.2.2 原MybatisMapper报BindingException错误

调用原生MybatisMapper中的方法时,提示无法映射到方法,错误信息如下:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.demo.mapper.FeedbackMapper.countManagerRecords
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53)
    at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:115)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:102)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
    at com.sun.proxy.$Proxy93.countManagerRecords(Unknown Source)
    at com.demo.service.impl.FeedbackServiceImpl.getManagerFeedbacks(FeedbackServiceImpl.java:77)
    at com.demo.controler.FeedbackController.getAdminRecords(FeedbackController.java:74)
    at com.demo.controler.FeedbackController$$FastClassBySpringCGLIB$$80abedc3.invoke(<generated>)

这个错误一般是由于你没有正确配置MyBatis XML文件路径导致的,需要在自定义SqlSessionFactoryBean方法中修改XML文件扫描路径:

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        /* 注意!在这里需要将原生Mybatis的XML路径写到配置中 */
        bean.setMapperLocations(resolver.getResources("classpath*:mybatis/mapper/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLazyLoadingEnabled(true);
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        return bean;
    }
1.2.3 Mybatis别名解析错误

在修改完1.2.2问题后,你大概率会碰到这个问题,具体表现为:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in class path resource [com/demo/shiro/ShiroConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shiro.spring.web.ShiroFilterFactoryBean]: Factory method 'shiroFilterFactoryBean' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in class path resource [com/demo/shiro/ShiroConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shiro.web.mgt.DefaultWebSecurityManager]: Factory method 'securityManager' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shiroRealm': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [xxxx/DemoProject/target/classes/com/demo/mapper/UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactoryBean' defined in class path resource [com/demo/config/FluentMybatisGenerator.class]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [xxxx/DemoProject/target/classes/mybatis/mapper/FeedbackMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [xxxx/DemoProject/target/classes/mybatis/mapper/FeedbackMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Feedback'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Feedback
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    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:213)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:270)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:567)
    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.SpringApplication.run(SpringApplication.java:1343)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332)
    at com.demo.Application.main(NewCommunity.java:27)

根本原因在MyBatis中:

Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [xxxx/DemoProject/target/classes/mybatis/mapper/FeedbackMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Feedback'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Feedback
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:123)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:95)
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:611)
    ... 92 common frames omitted
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Feedback'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Feedback
    at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:118)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:263)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:254)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:246)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:119)
    ... 94 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Feedback'.  Cause: java.lang.ClassNotFoundException: Cannot find class: Feedback
    at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:120)
    at org.apache.ibatis.builder.BaseBuilder.resolveAlias(BaseBuilder.java:149)
    at org.apache.ibatis.builder.BaseBuilder.resolveClass(BaseBuilder.java:116)
    ... 98 common frames omitted
Caused by: java.lang.ClassNotFoundException: Cannot find class: Feedback
    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:196)
    at org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
    at org.apache.ibatis.io.Resources.classForName(Resources.java:261)
    at org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
    ... 100 common frames omitted

据我猜测,其原因在于FluentMybatis没有夹在原生Mybatis的实体类,解决方法很简单,在自定义SqlSessionFactoryBean的方法中,手动添加原生MyBatis实体类所在包即可:

    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        /* 注意!在这里需要将原生Mybatis的XML路径写到配置中 */
        bean.setMapperLocations(resolver.getResources("classpath*:mybatis/mapper/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLazyLoadingEnabled(true);
        configuration.setMapUnderscoreToCamelCase(true);
        /* 注意!在这里需要将原生MyBatis的实体类包名添加到TypeAliasRegistry中 */
        configuration.getTypeAliasRegistry().registerAliases("com.demo.pojo");
        bean.setConfiguration(configuration);
        return bean;
    }
1.2.0 一割解千愁

经过测试,其实不需要自定义SqlSessionFactoryBean也可以正常的将原生MyBatis和FluentMybatis二者结合使用,所以如果上面的方法不能解决问题时,可以尝试将自定义SqlSessionFactoryBean方法删除后再试。

3. 完整的FluentMybatisGenerator.java:

package com.demo.config;

import cn.org.atool.fluent.mybatis.spring.MapperFactory;
import cn.org.atool.generator.FileGenerator;
import cn.org.atool.generator.annotation.Column;
import cn.org.atool.generator.annotation.Table;
import cn.org.atool.generator.annotation.Tables;
import com.demo.mybatis.handler.StringListHandler;
import com.demo.mybatis.module.StringList;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * FluentMybatisConfigurer
 *
 * @author adinlead
 * @desc
 * @create 2023/3/6 13:34 (星期一)
 */
@Configuration
public class FluentMybatisGenerator {
    /**
     * 从Spring中获取DataSource
     */
    @Resource
    private DataSource dataSource;


    /**
     * 使用定义Fluent Mybatis的MapperFactory
     */
    @Bean
    public MapperFactory mapperFactory() {
        /* 引用配置类,build方法允许有多个配置类 */
        FileGenerator.build(dataSource, CodeGenerator.class);
        return new MapperFactory();
    }

    /**
     * 使用自定义SqlSessionFactoryBean
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        /* 注意!在这里需要将原生Mybatis的XML路径写到配置中 */
        bean.setMapperLocations(resolver.getResources("classpath*:mybatis/mapper/*.xml"));
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLazyLoadingEnabled(true);
        configuration.setMapUnderscoreToCamelCase(true);
        /* 注意!在这里需要将原生Mybatis的XML路径写到配置中 */
        configuration.getTypeAliasRegistry().registerAliases("com.demo.pojo");
        bean.setConfiguration(configuration);
        return bean;
    }

    /**
     * 生成代码类
     */
    @Tables(
            srcDir = "src/main/java",
            basePack = "com.demo.mybatis.fluent",
            daoDir = "src/main/java",
            tablePrefix = {"tb_"},
            tables = {@Table(value = {"tb_commodity"}, columns = {
                    @Column(value = "create_time", insert = "now()"),
                    @Column(value = "update_time", insert = "now()", update = "now()"),
                    @Column(value = "cover_images", javaType = StringList.class, typeHandler = StringListHandler.class)
            })})
    static class CodeGenerator {
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值