项目配置管理

要完成的目标

  • 放置mybatis的xml映射文件
  • mybatis的配置环境切换
  • springboot的配置环境切换

放置mapper.xml

参考:mybatis错误——java.io.IOException: Could not find resource com/xxx/xxxMapper.xml - CSDN博客
直接放在src/java/main下没有成功,最终放在:src\main\resources\mapper

<mappers>
        <mapper resource="mapper/GroupMapper.xml" />
        <mapper resource="mapper/GroupRoleMapper.xml" />
        <mapper resource="mapper/PrivHiveHdfsMapper.xml" />
        <mapper resource="mapper/RoleMapper.xml" />
        <mapper resource="mapper/RolePrivMapper.xml" />
        <mapper resource="mapper/UserGroupMapper.xml" />
        <mapper resource="mapper/UserMapper.xml" />
        <mapper resource="mapper/UserRoleMapper.xml" />
    </mappers>

修改pom.xml

作为项目配置环境的总入口,通过maven的profile机制来切换环境。

  • 在build标签内
 <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/resources/${profiles.activation}/</directory>
            </resource>
        </resources>

在project标签内:

 <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profiles.activation>dev</profiles.activation>
            </properties>
        </profile>

        <profile>
            <id>pro</id>
            <properties>
                <profiles.activation>pro</profiles.activation>
            </properties>
        </profile>
    </profiles>

springboot的配置环境切换

修改中:application.properties
spring.profiles.active=@profiles.activation@

这样:
application.properties中应用maven的pom.xml中引用profiles.activation.

  • 如果profiles.activation为dev,则使用application-dev.properties
  • 如果profiles.activation为prd,则使用application-prd.properties

注意:一般${}方式会被maven处理。如果你pom继承了spring-boot-starter-parent,Spring
Boot已经将maven-resources-plugins默认的${}方式改为了@@方式,如@name@

最终效果

项目的resource目录


image.png

`mvn package'后,检查项目的target目录:除了kdbs项目下的代码,其他都是在resource目录下的内容。


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,286评论 19 139
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,869评论 0 4
  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,956评论 0 5
  • 经历会让你明白,吃得苦中苦,未必能改变什么。 56分钟能(or足够)从一座城市到达另一座城市, (开始手跟不上眼 ...
    锺琬阅读 96评论 0 0
  • 12月14日,我们英语组又迎来了来自上海的“老朋友”--- 何亚男特级教师。 两天六课,现在启程。 The fir...
    江外英语组阅读 1,481评论 0 0