zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    VisualSVN 服务不能自动启动的问题
    "There is already an open DataReader associated with this Command which must be closed first"错误
    LINQ 中调用存储过程自动绑定列名
    vs2008中调试iis7.0托管的程序
    SQL SERVER 2005的一个怪问题: 在查询结果面板中编辑失败.
    在VS2003下把一个DataTable Update 到数据库
    帮人解决一个小问题:QQ空间登录时脚本错误造成无法登入
    关于Sys未定义错误
    多层母版页嵌套中, 内层母版页的事件默认不触发
    线程同步
  • 原文地址:https://www.cnblogs.com/dalianpai/p/11770457.html
Copyright © 2011-2022 走看看