ACTable开源框架的使用及异常

介绍

ACTable是对Mybatis做的增强功能,支持SpringBoot以及传统的SpringMvc架构,配置简单,使用方便。主要是自动生成数据库表,直接修改java代码,数据库就会对应的变化,省去在调整数据库表的问题,在开发阶段非常实用。
本项目使用的springboot3.2.2版本,集成Mybatis,Mybatis-plus, ACTable组件.

配置文件:

# actable的配置信息
actable:
  table:
    auto: create
  model:
    pack: com.xxx.yours_project.*.entity
  database:
    type: mysql
  #actable.index.prefix=自己定义的索引前缀#该配置项不设置默认使用actable_idx_
  #actable.unique.prefix=自己定义的唯一约束前缀#该配置项不设置默认使用actable_uni_
  # mybatis自有的配置信息,key也可能是:mybatis.mapperLocations
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
  type-handlers-package: com.xxx.yours_project.base.util

springboot3.0以后,ACTable的自启动已经失效,先要配置主动启动:

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

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(YywjSupportApplication.class, args);
        // 容器中获取actable的核心处理类
        StartUpHandler bean =run.getBean(StartUpHandler.class, args);
        // 手动执行actable的建表方法
        bean.startHandler();
    }
}

异常

在使用Mybatis-plus时,需要注意:

  1. 配置文件中是:mybatis-plus.mapper-locations;
  2. 在build.gradle文件中,引入的是:com.baomidou:mybatis-plus-boot-starter,而非com.baomidou:mybatis-plus
  3. 在使用自定义handle时,要注意 @MapperType,
    开始时作者使用@MappedTypes({List.class, String.class}),但是启动后报错,原因是Mybatis将这个handle滥用了,导致失败,改用@Component,项目正常启动,不再报错
    慎用@MapperType:
//@MappedTypes({List.class, String.class})
@Component
public class ListStringHandle implements TypeHandler<List<String>> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
        if (CollectionUtils.isNotEmpty(strings)) {
            preparedStatement.setString(i, strings.stream().collect(Collectors.joining(",")));
        } else {
            preparedStatement.setString(i, null);
        }
    }

    @Override
    public List<String> getResult(ResultSet resultSet, String s) throws SQLException {
        if (StringUtils.isNotEmpty(resultSet.getString(s))) {
            return Arrays.asList(resultSet.getString(s).split(","));
        }
        return null;
    }

    @Override
    public List<String> getResult(ResultSet resultSet, int i) throws SQLException {
        if (StringUtils.isNotEmpty(resultSet.getString(i))) {
            return Arrays.asList(resultSet.getString(i).split(","));
        }
        return null;
    }

    @Override
    public List<String> getResult(CallableStatement callableStatement, int i) throws SQLException {
        if (StringUtils.isNotEmpty(callableStatement.getString(i))) {
            return Arrays.asList(callableStatement.getString(i).split(","));
        }
        return null;
    }
}

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值