public class FindOne extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
/* 执行 SQL ,动态 SQL 参考类 SqlMethod */
String sql = "select * from " + tableInfo.getTableName()
+ " where " + tableInfo.getKeyColumn() + "=#{" + tableInfo.getKeyProperty() + "}";
/* mapper 接口方法名一致 */
String method = "findOne";
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return addSelectMappedStatementForTable(mapperClass, method, sqlSource, tableInfo);
}
}
- 自定义sql注入
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
//增加自定义方法
methodList.add(new DeleteAll());
methodList.add(new FindOne());
/**
* 以下 3 个为内置选装件
* 头 2 个支持字段筛选函数
*/
// 例: 不要指定了 update 填充的字段
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
methodList.add(new AlwaysUpdateSomeColumnById());
methodList.add(new LogicDeleteByIdWithFill());
return methodList;
}
}
- 定义到BaseMapper
public interface MyBaseMapper<T> extends BaseMapper<T> {
/* ↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓↓↓ */
/**
* 以下定义的 4个 default method, copy from {@link com.baomidou.mybatisplus.extension.toolkit.ChainWrappers}
*/
default QueryChainWrapper<T> queryChain() {
return new QueryChainWrapper<>(this);
}
default LambdaQueryChainWrapper<T> lambdaQueryChain() {
return new LambdaQueryChainWrapper<>(this);
}
default UpdateChainWrapper<T> updateChain() {
return new UpdateChainWrapper<>(this);
}
default LambdaUpdateChainWrapper<T> lambdaUpdateChain() {
return new LambdaUpdateChainWrapper<>(this);
}
/**
* 以下定义的 4个 method 其中 3 个是内置的选装件
*/
int insertBatchSomeColumn(List<T> entityList);
int alwaysUpdateSomeColumnById(@Param(Constants.ENTITY) T entity);
int deleteByIdWithFill(T entity);
/**
* 以下为自己自定义
*/
T findOne(Serializable id);
int deleteAll();
}
- 测试
@Slf4j
@SpringBootTest
public class InjectorTest {
@Resource
private StudentMapper studentMapper;
/**
* 插入2条数据
* INSERT INTO STUDENT ( ID, NAME, AGE ) VALUES ( ?, ?, ? )
* 1549911999816278017(Long), 小明0号(String), 0(Integer)
*
* INSERT INTO STUDENT ( ID, NAME, AGE ) VALUES ( ?, ?, ? )
* 1549911999887581185(Long), 小明1号(String), 1(Integer)
*/
@Test
public void test() {
log.error("--------------------------------------insert-------------------------------------------------------");
List<Long> ids = Lists.newArrayList();
for (int i = 0; i < 2; i++) {
Student student = new Student("小明" + i + "号", i);
studentMapper.insert(student);
ids.add(student.getId());
}
}
/**
* 插入18条数据
* INSERT INTO STUDENT (ID,NAME,AGE) VALUES (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?) , (?,?,?)
* 1549912388825489409(Long), 小明2号(String), 2(Integer), 1549912388825489410(Long), 小明3号(String), 3(Integer), 1549912388825489411(Long), 小明4号(String), 4(Integer), 1549912388825489412(Long), 小明5号(String), 5(Integer), 1549912388825489413(Long), 小明6号(String), 6(Integer), 1549912388825489414(Long), 小明7号(String), 7(Integer), 1549912388825489415(Long), 小明8号(String), 8(Integer), 1549912388825489416(Long), 小明9号(String), 9(Integer), 1549912388825489417(Long), 小明10号(String), 10(Integer), 1549912388825489418(Long), 小明11号(String), 11(Integer), 1549912388825489419(Long), 小明12号(String), 12(Integer), 1549912388825489420(Long), 小明13号(String), 13(Integer), 1549912388825489421(Long), 小明14号(String), 14(Integer), 1549912388825489422(Long), 小明15号(String), 15(Integer), 1549912388825489423(Long), 小明16号(String), 16(Integer), 1549912388825489424(Long), 小明17号(String), 17(Integer), 1549912388825489425(Long), 小明18号(String), 18(Integer), 1549912388825489426(Long), 小明19号(String), 19(Integer)
*
* select * from student where id = 1549912388737409026
* Student(id=1549912388737409026, name=小明0号, age=0)
*/
@Test
public void test1() {
log.error("--------------------------------------insert-------------------------------------------------------");
List<Long> ids = Lists.newArrayList();
for (int i = 0; i < 2; i++) {
Student student = new Student("小明" + i + "号", i);
studentMapper.insert(student);
ids.add(student.getId());
}
log.error("--------------------------------------insertBatchSomeColumn-------------------------------------------------------");
List<Student> ss = Lists.newArrayList();
for (int i = 2; i < 20; i++) {
Student student = new Student("小明" + i + "号", i);
ss.add(student);
}
studentMapper.insertBatchSomeColumn(ss);
ids.addAll(ss.stream().map(Student::getId).collect(Collectors.toList()));
Student select = studentMapper.select("select * from student where id = " + ids.get(0));
System.out.println(select);
}
/**
* 清空表中数据
* delete from STUDENT
*/
@Test
public void test2() {
log.error("--------------------------------------deleteAll-------------------------------------------------------");
studentMapper.deleteAll();
}
}