springboot+mybatis框架搭建--1.初始化项目

2019-02-15  本文已影响0人  小狼在IT

源码地址:https://github.com/acehjr/springBoot-MyBatis
1.创建项目

image.png
就选这几个吧:
image.png

2.配置初始化
1)增加3个配置properties:


image.png

2)在maven新建profiles节点:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <env>test</env>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <env>prd</env>
        </properties>
    </profile>
</profiles>

接着在 application.properties 增加一个配置:

spring.profiles.active=@env@

也有不少教程说在application.properties里面,通过 spring.profiles.active=dev 来切换配置文件,不利用maven,我个人不建议这样,利用maven,可以避免不小心把test的配置发布到prd服务器里面。
按上述配置完后,你调试程序,是可以选择不同的配置去运行了:


image.png
image.png

后续想要打包到测试环境,就用:clean package -Ptest
想打包到正式环境就用:clean package -Pprd
不需要去application.properties改任何东西了。

3)datasource.url配置
因为引入了mybatis-spring-boot-starter,所以需要去配置spring.datasource,不然会报错。
3-1)引入数据库需要的jar:

    <!-- 数据库连接 begin -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.12</version>
    </dependency>
    <!-- 数据库连接 end -->

在application.properties配置属性:

    #-------------------------------------------------------------
    #spring.datasource begin
    #如果引入了Mybatis,又没在声明中自定义datasource,那么这里必须声明spring.datasource,否则报错。
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.url=jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
    spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
    spring.datasource.username=${dbUserName}
    spring.datasource.password=${dbpsw}
    spring.datasource.initialSize=5
    spring.datasource.minIdle=20
    spring.datasource.maxActive=50
    spring.datasource.maxWait=30000
    spring.datasource.timeBetweenEvictionRunsMillis=60000
    spring.datasource.minEvictableIdleTimeMillis=300000
    spring.datasource.validationQuery=SELECT 1 
    spring.datasource.testWhileIdle=true
    spring.datasource.testOnBorrow=false
    spring.datasource.testOnReturn=false
    spring.datasource.poolPreparedStatements=true
    spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
    spring.datasource.filters=stat,wall,slf4j
    spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    spring.datasource.useGlobalDataSourceStat=true
    #spring.datasource end
    #-------------------------------------------------------------

注意此处 username,password的属性值使用的是占位符,实际的值会写在application-dev.properties这些环境配置文件里面。


image.png

截止到这一步,项目初始化完成。
题外话:让打包出来的target干净些。
在刚才的步骤里,我打包出来的target是这样的:


image.png

这里我只需要application.properties和application-prd.properties两个配置文件,它却将另外两个我不需要的也打包进去,看着不太舒服。

    <resources>
        <resource><!-- 存放配置文件到target/classes目录下 -->
            <targetPath>${project.build.outputDirectory}</targetPath>
            <directory>src/main/resources</directory>
            <filtering>true</filtering><!--true表示需要替换掉占位符-->
            <includes>
                <include>application.properties</include>
                <include>*-${env}.properties</include>
            </includes>
        </resource>
    </resources>
image.png

打包出来的,就比较“干净”了。

需要注意的是,加了这个resources,那么所有需要打包的资源文件,都需要写到includes里面。我后续会写成这样:

    <resources>
        <resource><!-- 存放配置文件到target/classes目录下 -->
            <targetPath>${project.build.outputDirectory}</targetPath>
            <directory>src/main/resources</directory>
            <filtering>true</filtering><!--true表示需要替换掉占位符-->
            <includes>
                <include>**/*.xml</include>
                <include>i18n/**/*.properties</include>
                <include>config/application.properties</include>
                <include>config/*-${env}.properties</include>
                <include>logback-spring.xml</include>
            </includes>
        </resource>
    </resources>

当然你也可以不做这个resources,也没什么差。

上一篇 下一篇

猜你喜欢

热点阅读