fixed: 修复添加 mapper.xml 会出现 NPE 的问题。 close #I6X59V

This commit is contained in:
开源海哥 2023-04-20 09:36:56 +08:00
parent 114d2ec963
commit a8023de0a8
5 changed files with 43 additions and 1 deletions

View File

@ -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;
}
/**

View File

@ -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) {

View File

@ -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);
}

View File

@ -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:

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mybatisflex.test.mapper.MyAccountMapper">
<!-- selectByName -->
<select id="selectByName" resultType="com.mybatisflex.test.model.Account">
select * from `tb_account` where `user_name` = #{name}
</select>
</mapper>