标签:lock from append mapper 参考 close lan 分页 chain
包含 分页、逻辑删除、自定义全局操作 等绝大部分常用功能的使用示例,相当于大整合的完整示例
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> <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
# DataSource Config spring: datasource: driver-class-name: org.h2.Driver url: jdbc:h2:tcp://192.168.180.115:19200/~/mem/test username: root password: test ? # Logger Config logging: level: com.mp.deluxe: debug ? mybatis-plus: # 扫描 mapper.xml mapper-locations: classpath:/mapper/*Mapper.xml configuration: jdbc-type-for-null: ‘null‘
配置类
public interface MyBaseMapper<T> extends BaseMapper<T> { ? /** * 自定义通用方法 */ Integer deleteAll(); ? int myInsertAll(T entity); ? /** * 如果要自动填充,@{@code Param}(xx) xx参数名必须是 list/collection/array 3个的其中之一 * * @param batchList * @return */ int mysqlInsertAllBatch(@Param("list") List<T> batchList); } ? @Configuration @MapperScan("com.mp.deluxe.mapper") public class MybatisPlusConfig { ? /** * 1.分页插件 * 2.多租户 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } ? @Bean public OptimisticLockerInterceptor optimisticLockerInterceptor(){ return new OptimisticLockerInterceptor(); } ? /** * 自定义 SqlInjector * 里面包含自定义的全局方法 */ @Bean public MyLogicSqlInjector myLogicSqlInjector() { return new MyLogicSqlInjector(); } } ? public class TestTypeHandler extends BaseTypeHandler<String> { ? @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, "TestTypeHandler set {" + parameter + "}"); } ? @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { String string = rs.getString(columnName); return "TestTypeHandler(rs columnName) get {" + string + "}"; } ? @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String string = rs.getString(columnIndex); return "TestTypeHandler(rs columnIndex) get {" + string + "}"; } ? @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String string = cs.getString(columnIndex); return "TestTypeHandler(cs columnIndex) get {" + string + "}"; } } ? @Data @Accessors(chain = true) public class User { private Long id; private String name; private Integer age; @TableField(typeHandler = TestTypeHandler.class) private String email; ? @Version private Integer version; ? @TableLogic(value = "0", delval = "1") @TableField(select = false) private Integer deleted; ? @TableField(value = "create_time", fill = FieldFill.INSERT) private Timestamp createTime; }