SpringBoot整合MybatisPlus3.X之自定义Mapper(十)
-
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/p6spy/p6spy --> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.8.0</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.49</version> <scope>test</scope> </dependency> <!-- for testing --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
-
application.yml
spring: datasource: driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:h2:tcp://192.168.180.115:19200/~/mem/test username: root password: test
-
Mapper及注入器
public class CustomizeSuperMapperSqlInjector extends DefaultSqlInjector { /** * 如果只需增加方法,保留MP自带方法 * 可以super.getMethodList() 再add * @return */ @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); methodList.add(new FindOne()); return methodList; } } public interface MySuperMapper<T> extends Mapper { public T findOne(Object id); } public interface UserMapper extends MySuperMapper<User> { }
-
配置类
@Configuration @MapperScan("com.mp.basemapper.mapper") public class MybatisPlusConfig { @Bean public CustomizeSuperMapperSqlInjector customizeSuperMapperSqlInjector(){ return new CustomizeSuperMapperSqlInjector(); } }
-
实体类
@Data @Accessors(chain = true) public class User { private Long id; private String name; private Integer age; private String email; private Integer version; private Integer deleted; private Timestamp createTime; }
-
方法
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); } }
-
@SpringBootTest class BasemapperApplicationTests { @Autowired UserMapper userMapper; @Test public void test() { User user = userMapper.findOne(1L); System.out.println(user); } }
-
测试结果
2019-10-31 11:59:30.159 INFO 828 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. Consume Time:4 ms 2019-10-31 11:59:30 Execute SQL:select * from user where id=1