Custom SQL Injector in MyBatis‑Plus: Built‑in Extensions, Global Configuration, and How to Create Your Own
This article explains the concept of SQL injectors in MyBatis‑Plus, lists the built‑in injectable methods, shows how to configure them globally, demonstrates creating a custom mapper and injector with full code examples, and provides step‑by‑step testing procedures for extending MyBatis‑Plus functionality.
What is an SQL Injector?
When using MyBatis‑Plus, DAO classes inherit BaseMapper , and each method in BaseMapper is essentially an SQL injector.
The core package of MyBatis‑Plus provides several default injectable methods, which are shown in the accompanying image.
Built‑in Extension SQL Injectors
MyBatis‑Plus supplies a set of ready‑made injectable methods, such as:
AlwaysUpdateSomeColumnById : updates all fields by ID, including null values.
InsertBatchSomeColumn : performs a true batch insert (as opposed to the pseudo‑batch saveBatch ).
LogicDeleteBatchByIds : logical delete with field fill (e.g., update time, updater).
Upsert : insert a record with selected fields.
Global Configuration of SQL Injectors
@Component
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
// Add custom injectors here
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
methodList.add(new AlwaysUpdateSomeColumnById(i -> i.getFieldFill() != FieldFill.INSERT));
return methodList;
}
}Custom Mapper Definition
public interface MyBaseMapper<T> extends BaseMapper<T> {
/**
* Full‑field update that does not ignore null values
*/
int alwaysUpdateSomeColumnById(@Param("et") T entity);
/**
* True batch insert
*/
int insertBatchSomeColumn(List<T> entityList);
}The custom mapper now extends MyBaseMapper instead of BaseMapper :
@Mapper
public interface UserMapper extends MyBaseMapper<UserDO> {}Testing the Injectors
@SpringBootTest
@RunWith(SpringRunner.class)
@ComponentScan("com.jincou.mybatisplus.dao")
public class SqlInjectorTest {
@Autowired
private UserMapper mapper;
@Test
public void alwaysUpdateSomeColumnById() {
UserDO user = new UserDO();
user.setUsername("小小");
user.setPhone(null);
user.setSex("女");
user.setId(1);
mapper.alwaysUpdateSomeColumnById(user);
}
@Test
public void insertBatchSomeColumn() {
UserDO user = new UserDO();
user.setUsername("zhangsan");
user.setPhone("13811111111");
user.setSex("女");
UserDO user1 = new UserDO();
user1.setUsername("lisi");
user1.setPhone("13822222222");
user1.setSex("男");
List<UserDO> list = Lists.newArrayList(user, user1);
mapper.insertBatchSomeColumn(list);
}
}Both tests produce the expected results, confirming that the built‑in injectors work as intended.
How to Create a Custom SQL Injector
When the built‑in injectors do not meet specific requirements, you can define your own. The example below adds a simple findAll method.
public interface MyBaseMapper<T> extends BaseMapper<T> {
/** Query all records */
List<T> findAll();
} public class FindAll extends AbstractMethod {
public FindAll() { super("findAll"); }
public FindAll(String methodName) { super(methodName); }
@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, sqlSource, tableInfo);
}
} @Component
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
methodList.add(new FindAll());
return methodList;
}
}Finally, test the new method:
@Test
public void findAll() {
List<UserDO> users = mapper.findAll();
}The test runs successfully, demonstrating how to extend MyBatis‑Plus with custom SQL injectors.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.