SpringBoot2.0.4—使用Druid、Mybatis和Redis配置

本篇只简单讲述简的环境搭建,如题,在SpringBoot2.0.4中集成Druid(阿里的数据源)、Mybatis(sql写在xml中的配置)和redis(最常用的缓存数据库)。本项目只限提供RestAPI服务,不涉及网站页面文件(静态和动态)的支持。

1.先看pom.xml

<dependencies>
        
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-redisartifactId>
            <version>1.4.7.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.9version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.3.2version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.2version>
            <scope>providedscope>
        dependency>
    dependencies>

2.项目结构
SpringBoot2.0.4—使用Druid、Mybatis和Redis配置_第1张图片

3.springBoot主要配置application.properties,几乎所有的配置都在这里了,已附上官网配置参考链接,想深入的同学可以自行阅读

debug=false
trace=false

server.port=8080
server.path=http://localhost

# Druid连接池配置,官方配置参考:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
spring.datasource.druid.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf8
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=10
spring.datasource.druid.max-wait=10
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000

# Druid WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/*

# Druid StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=

#Mybatis配置,官方参考:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
mybatis.mapper-locations=classpath*:mybatis/*Mapper.xml

# Redis配置,官方参考:https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#common-application-properties
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=5000

4.启动类Application.java的代码,也是少的可怜;这里唯一需要注意的就是Mybatis的配置这里需要指定一下扫描的mapper包路径(其实觉得完全可以放在application.properties中指定,但本人亲测,只有放在这里管用,具体原理还没细纠)

@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

5.其他废话
除了上述几个配置之外,其他的代码跟普通的spring项目一样,没有任何区别。虽说springboot虽然还需要一些配置,但整体来说已经比传统的项目配置要少的多了,主要在于一些大厂都已经开始支持springboot了,于是就有了各种各样的starter。需要哪个服务的支持,可以直接去github上搜对应的starter就可以了。最后附上springboot的官方配置连接,还是很详细的。
SpringBoot官方配置参考

你可能感兴趣的:(springboot)