首页 > 数据库 >mybatisplus-sql注入器

mybatisplus-sql注入器

时间:2022-08-19 12:33:27浏览次数:55  
标签:tableInfo mybatisplus sql import com public 注入

sql注入器
使用mybatisplus只需要继承BaseMapper接口即可使用;但是有新的需求需要扩展BaseMapper里面的功能时可使用sql注入器。

扩展BaseMapper里面的功能

点击查看代码
public interface UserMapper extends BaseMapper<User> {
    List<User> findAll();
}

实现FindAll方法

点击查看代码
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class FindAll extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = "select * from " + tableInfo.getTableName();
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass,"findAll",sqlSource,tableInfo);
    }
}

定义sql注入器

点击查看代码
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.example.mybatisplusdemo.sample.method.FindAll;

import java.util.List;

public class MyInject extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new FindAll());
        return methodList;
    }
}

将sql注入器注册到Spring容器中

点击查看代码
@Bean
    public MyInject inject(){
       return new MyInject();
    }

Mybatis-Plus中使用sql注入器场景:mapper 层 选装件
int alwaysUpdateSomeColumnById(T entity);
int insertBatchSomeColumn(List entityList);
int logicDeleteByIdWithFill(T entity);

标签:tableInfo,mybatisplus,sql,import,com,public,注入
From: https://www.cnblogs.com/shigongp/p/16601603.html

相关文章

  • 在docker内 mysql 中执行sql文件
    通过dockerps查询当前运行的容器,找到mysql容器的iddockerps将项目内的SQL文件拷贝到mysql容器内部的home下的temp文件内sudodockercp/root/sqlfile/mydata.sql8c......
  • SQL脚本开发经验
    需求1:将Excel中的一个条目的数据关联数据库表中固定条目并将关键字段进行替换生成临时表aa关联两张表并将结果数据插入到新表INSERTINTOBGT_apptasks_vals2023_8......
  • Postgresql之基础
      在默认配置下,之允许本机访问Postgresql#切换到postgres用户su-postgresLastlogin:WedMar113:16:48CST2017onpts/1-bash-4.2$psqlpsql(9.2.1......
  • Navicat 连接MySQL数据库出现错误:2059 - authentication plugin 'caching_sha2_passwo
    出现这个错误的原因是因为MySQL8.0.19数据库使用的加密方式是:caching_sha2_password,解决: 1 showvariableslike'default_authentication_plugin查看加密信息 2 ......
  • 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接
    驱动程序无法通过使用安全套接字层(SSL)加密与SQLServer建立安全连接,Error:“TheserverselectedprotocolversionTLS10isnotacceptedbyclientpreferences[......
  • 使用kubersphere 安装有状态服务 MySQL
    kind:StatefulSetapiVersion:apps/v1metadata:name:his-mysqlnamespace:hislabels:app:his-mysqlannotations:kubesphere.io/creator:dev-z......
  • mysql innodb 为什么用B+树作为索引数据结构,而非其他结构
    B树的层数较低,即意味着读取磁盘的次数较少在mysql中一个节点的大小是16K,如果一行数据约1k,其主键为8字节的bigint,那么3层即可容纳约2000万行对比其他结构:hash不体现......
  • mysql 的安装
    1.链接进入mysql官网  https://www.mysql.com2.点击download   3.滑动到下面找到社区版  4.找到这个点击5.点击Archives  6.可以选择自己想要的......
  • Docker安装MySQL
    1,docker仓库搜索mysqldockersearchmysql  2,docker仓库拉取mysql8.0dockerpullmysql:8.0  3,查看本地仓库镜像是否下载成功dockerimagesmysql:8.0......
  • mysqldump 在 StoneDB 中的使用注意事项
    InnoDB导入StoneDB此场景是利用mysqldump从InnoDB导出,然后再导入StoneDB,在导入StoneDB前,需要对导出文件做如下修改。1)修改存储引擎CREATETABLE`t_user`(xxx)ENGIN......