From a8023de0a8d28f491cf1ba112acccbf3c532c10d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 20 Apr 2023 09:36:56 +0800 Subject: [PATCH] =?UTF-8?q?fixed:=20=E4=BF=AE=E5=A4=8D=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20mapper.xml=20=E4=BC=9A=E5=87=BA=E7=8E=B0=20NPE=20=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E3=80=82=20close=20#I6X59V?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/FlexSqlSessionFactoryBean.java | 9 ++++++++- .../test/controller/AccountController.java | 12 ++++++++++++ .../com/mybatisflex/test/mapper/MyAccountMapper.java | 10 ++++++++++ .../src/main/resources/application.yml | 3 +++ .../src/main/resources/mapper/accountMapper.xml | 10 ++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml diff --git a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/FlexSqlSessionFactoryBean.java b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/FlexSqlSessionFactoryBean.java index 2ae4b155..6804aaf8 100644 --- a/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/FlexSqlSessionFactoryBean.java +++ b/mybatis-flex-spring/src/main/java/com/mybatisflex/spring/FlexSqlSessionFactoryBean.java @@ -585,6 +585,12 @@ public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean this.transactionFactory == null ? new SpringManagedTransactionFactory() : this.transactionFactory, dataSource instanceof FlexDataSource ? dataSource : new FlexDataSource(FlexConsts.NAME, dataSource))); + + // 需先构建 sqlSessionFactory,再去初始化 mapperLocations + // 因为 xmlMapperBuilder.parse() 用到 FlexGlobalConfig, FlexGlobalConfig 的初始化是在 sqlSessionFactory 的构建方法里进行的 + // fixed gitee https://gitee.com/mybatis-flex/mybatis-flex/issues/I6X59V + SqlSessionFactory sqlSessionFactory = this.sqlSessionFactoryBuilder.build(targetConfiguration); + if (this.mapperLocations != null) { if (this.mapperLocations.length == 0) { LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found."); @@ -609,7 +615,8 @@ public class FlexSqlSessionFactoryBean extends SqlSessionFactoryBean LOGGER.debug(() -> "Property 'mapperLocations' was not specified."); } - return this.sqlSessionFactoryBuilder.build(targetConfiguration); + + return sqlSessionFactory; } /** diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java index edf1276c..2d0179e1 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/controller/AccountController.java @@ -20,6 +20,7 @@ import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.row.Db; import com.mybatisflex.core.row.Row; import com.mybatisflex.test.mapper.AccountMapper; +import com.mybatisflex.test.mapper.MyAccountMapper; import com.mybatisflex.test.model.Account; import com.mybatisflex.test.service.AccountService; import org.springframework.transaction.annotation.Transactional; @@ -35,6 +36,10 @@ public class AccountController { AccountMapper accountMapper; + @Resource + MyAccountMapper myAccountMapper; + + @Resource AccountService accountService; @@ -46,6 +51,13 @@ public class AccountController { } + + @GetMapping("/account/select/{name}") + Account selectName(@PathVariable("name") String name){ + return myAccountMapper.selectByName(name); + } + + @GetMapping("/account/{id}") @Transactional public Account selectOne(@PathVariable("id") Long id) { diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java new file mode 100644 index 00000000..4df82f9e --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyAccountMapper.java @@ -0,0 +1,10 @@ +package com.mybatisflex.test.mapper; + +import com.mybatisflex.test.model.Account; +import org.apache.ibatis.annotations.Param; + +public interface MyAccountMapper extends AccountMapper { + + + Account selectByName(@Param("name") String name); +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index 717a1fcf..7e354699 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -11,6 +11,9 @@ spring: init: schema-locations: classpath:schema.sql data-locations: classpath:data.sql +mybatis-flex: + mapper-locations: + - classpath*:/mapper/*.xml #mybatis-flex: # datasource: # ds1: diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml new file mode 100644 index 00000000..255f9407 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/mapper/accountMapper.xml @@ -0,0 +1,10 @@ + + + + + + + +