SpringBoot整合MybatisPlus3.X之自定义Mapper

Posted 天宇轩-王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

     

以上是关于SpringBoot整合MybatisPlus3.X之自定义Mapper的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合MybatisPlus3.X之自定义Mapper

SpringBoot整合MybatisPlus3.X之SQL执行分析插件(十四)

springboot-mybatisplus笔记

Vue+SpringBoot超详细!一周开发一个SpringBoot + Vue+MybatisPlus+Shiro+JWT+Redis前后端分离个人博客项目!!!项目完结

Vue+SpringBoot超详细!一周开发一个SpringBoot + Vue+MybatisPlus+Shiro+JWT+Redis前后端分离个人博客项目!!!项目完结

阿昌教你看懂mybatisplus的sql语句的创建过程