当前位置: 首页 >Java技术 > mybatisplus-sql注入器

mybatisplus-sql注入器

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 {@Overridepublic MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {String sql = "select * from " + tableInfo.getTableName();SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);retu 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 {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);methodList.add(new FindAll());retu methodList;}}

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

点击查看代码
@Beanpublic MyInject inject(){retu new MyInject();}

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

作者:shigp1
来源链接:https://www.cnblogs.com/shigongp/p/16601603.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/java/117307.html

标签:MybatisPlus
分享给朋友:

“mybatisplus-sql注入器” 的相关文章