報錯:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

springboot2配置mybatis-plus後調用BaseMapper方法插入數據時報錯:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

jar包:

<!-- mybatis增強工具 -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.2</version>
</dependency>
<!-- alibaba 數據庫連接池容器 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

最終發現是配置的DataSource文件導致:

@Configuration
@MapperScan(basePackages = "com.hune.mapper", sqlSessionTemplateRef = "sqlSessionTemplate")
public class DuridConfig {


    @Value("${spring.datasource.url:#{null}}")
    private String dbUrl;
    @Value("${spring.datasource.username: #{null}}")
    private String username;
    @Value("${spring.datasource.password:#{null}}")
    private String password;
    @Value("${spring.datasource.driverClassName:#{null}}")
    private String driverClassName;
    @Value("${spring.datasource.initialSize:#{null}}")
    private Integer initialSize;
    @Value("${spring.datasource.minIdle:#{null}}")
    private Integer minIdle;
    @Value("${spring.datasource.maxActive:#{null}}")
    private Integer maxActive;
    @Value("${spring.datasource.maxWait:#{null}}")
    private Integer maxWait;
    @Value("${spring.datasource.timeBetweenEvictionRunsMillis:#{null}}")
    private Integer timeBetweenEvictionRunsMillis;
    @Value("${spring.datasource.minEvictableIdleTimeMillis:#{null}}")
    private Integer minEvictableIdleTimeMillis;
    @Value("${spring.datasource.validationQuery:#{null}}")
    private String validationQuery;
    @Value("${spring.datasource.testWhileIdle:#{null}}")
    private Boolean testWhileIdle;
    @Value("${spring.datasource.testOnBorrow:#{null}}")
    private Boolean testOnBorrow;
    @Value("${spring.datasource.testOnReturn:#{null}}")
    private Boolean testOnReturn;
    @Value("${spring.datasource.poolPreparedStatements:#{null}}")
    private Boolean poolPreparedStatements;
    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize:#{null}}")
    private Integer maxPoolPreparedStatementPerConnectionSize;
    @Value("${spring.datasource.filters:#{null}}")
    private String filters;
    @Value("${spring.datasource.connectionProperties:#{null}}")
    private String connectionProperties;

    @Bean(name = "dataSource") // 聲明其爲Bean實例
    @Primary // 在同樣的DataSource中,首先使用被標註的DataSource
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        // configuration
        if (initialSize != null) {
            datasource.setInitialSize(initialSize);
        }
        if (minIdle != null) {
            datasource.setMinIdle(minIdle);
        }
        if (maxActive != null) {
            datasource.setMaxActive(maxActive);
        }
        if (maxWait != null) {
            datasource.setMaxWait(maxWait);
        }
        if (timeBetweenEvictionRunsMillis != null) {
            datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        }
        if (minEvictableIdleTimeMillis != null) {
            datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        }
        if (validationQuery != null) {
            datasource.setValidationQuery(validationQuery);
        }
        if (testWhileIdle != null) {
            datasource.setTestWhileIdle(testWhileIdle);
        }
        if (testOnBorrow != null) {
            datasource.setTestOnBorrow(testOnBorrow);
        }
        if (testOnReturn != null) {
            datasource.setTestOnReturn(testOnReturn);
        }
        if (poolPreparedStatements != null) {
            datasource.setPoolPreparedStatements(poolPreparedStatements);
        }
        if (maxPoolPreparedStatementPerConnectionSize != null) {
            datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        }

        if (connectionProperties != null) {
            datasource.setConnectionProperties(connectionProperties);
        }

        List<Filter> filters = new ArrayList<>();
        filters.add(statFilter());
        filters.add(wallFilter());
        datasource.setProxyFilters(filters);

        //druid數據庫密碼加密
        try {
            datasource.setFilters("config"); //數據庫過濾器
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Properties properties = new Properties();
        properties.setProperty("config.decrypt","true"); //加密
        datasource.setConnectProperties(properties);

        return datasource;
    }

    @Bean(name = "statFilter")
    @Primary
    public StatFilter statFilter() {
        StatFilter statFilter = new StatFilter();
        statFilter.setLogSlowSql(true);
        statFilter.setMergeSql(true);
        statFilter.setSlowSqlMillis(1000);

        return statFilter;
    }

    @Bean(name = "wallFilter")
    @Primary
    public WallFilter wallFilter() {
        WallFilter wallFilter = new WallFilter();

        // 允許執行多條SQL
        WallConfig config = new WallConfig();
        config.setMultiStatementAllow(true);
        wallFilter.setConfig(config);

        return wallFilter;
    }

    @Bean(name = "jdbcTemplate")
    @Primary
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

    @Bean(name = "transactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory setSqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "sqlSessionTemplate")
    @Primary
    public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    public String getDbUrl() {
        return dbUrl;
    }

}

註釋掉sqlSessionFactorysqlSessionTemplate兩個Bean就可以正常調用mybatis-plus的方法了。因爲有這兩段代碼它就不能調用MybatisSqlSessionFactoryBean,也就無法綁定Mybatis-plus的方法。

最終我把文件全部註釋了,使application.yml中配置的dataSource

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/hune?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
    username: root
    password: bNVOqb7WKLX5xjnw+LMv92taj25KOxDimXxILPQjw42wgv+1lHzOl8kr97xDwWdhpY67QuYCS7sWN4W46YbkFA==
    #druid連接池配置
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      #初始化時建立物理連接的個數
      initial-size: 10
      #最小連接池數量
      min-idle: 10
      #最大連接池數量 maxIdle已經不再使用
      max-active: 100
      #獲取連接時最大等待時間,單位毫秒
      max-wait: 60000
      #既作爲檢測的間隔時間又作爲testWhileIdel執行的依據
      time-between-eviction-runs-millis: 60000
      #銷燬線程時檢測當前連接的最後活動時間和當前時間差大於該值時,關閉當前連接
      min-evictable-idle-time-millis: 300000
      #用來檢測連接是否有效的sql 必須是一個查詢語句
      #mysql中爲 select 'x'
      #oracle中爲 select 1 from dual
      validation-query: select 'x'
      #申請連接的時候檢測,如果空閒時間大於timeBetweenEvictionRunsMillis,執行validationQuery檢測連接是否有效。
      test-while-idle: true
      #申請連接時會執行validationQuery檢測連接是否有效,開啓會降低性能,默認爲true
      test-on-borrow: false
      #歸還連接時會執行validationQuery檢測連接是否有效,開啓會降低性能,默認爲true
      test-on-return: false
      #當數據庫拋出不可恢復的異常時,拋棄該連接
      #exception-sorter: true
      #是否緩存preparedStatement,mysql5.5+建議開啓
      pool-prepared-statements: true
      #當值大於0時poolPreparedStatements會自動修改爲true
      max-pool-prepared-statement-per-connection-size: 20
      #配置擴展插件,增加config
      filters: stat,wall,slf4j,config
      #通過connectProperties屬性來打開mergeSql功能;慢SQL記錄;config.decrypt=true; 讓ConfigFilter解密密碼
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;
      #合併多個DruidDataSource的監控數據
      use-global-data-source-stat: true
      #設置訪問druid監控頁的賬號和密碼,默認沒有 http://localhost:9000/hune/druid/index.html
      stat-view-servlet:
        login-username: admin
        login-password: admin

最終測試mybatis-plus的方法成功插入數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章