Spring boot 使用问题记录

(1.)Mybatis映射xml放在resources路径下导致的问题,看下面maven配置:

<resources>
         <!-- 本地jar打包配置 -->
  		 <resource>
			 <!-- 指定属性文件的目录,build的过程需要找到它,并且将其
			 放到targetPath下,默认的directory是${basedir}/src/main/resources-->
             <directory>src/main/webapp/WEB-INF/lib</directory>
			 <!-- 指定build资源到哪个目录,默认是base directory -->
             <targetPath>BOOT-INF/lib/</targetPath>
			 <!-- 指定包含文件的patterns,符合样式并且在directory目录下的文件将会包含进project的资源文件。-->
             <includes>
                 <include>**/*.jar</include>
             </includes>
         </resource>
         <!-- resources下的文件build配置 -->
         <resource>
	        <directory>src/main/resources</directory>
	        <targetPath>BOOT-INF/classes/</targetPath>
	      </resource>
  	</resources>

上面的配置打包后通过java -jar执行没有问题,因为spring-boot识别。但是,本地通过IDE运行的时候会报org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

以下是本人项目里面读取映射文件的代码:

@Bean
 public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
 
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mybatis/mapper/**/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }   
    

代码里是直接读取classpath下对应的资源,但是如果加上上面的配置,build完成后classpath的文件如下:

对应的映射文件被编译进了BOOT-INF下。这样就会导致本地代码跑的时候在classpath下根据代码里面写的路径找不到对应的映射文件,所以报了这个错。所以在本地环境下需要把对应的resource注释掉。但是生产环境需要取消注释:看下面打好包的文件:

个人理解:spring-boot项目classpath默认为BOOT-INFO下的classpath,打包的时候会默认打到对应的BOOT-INF/classpath下。如果不加上面配置,是不会打进去的。启动jar包时会报错。

猜你喜欢

转载自blog.csdn.net/weixin_39032575/article/details/81110207