继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

「小程序JAVA实战」springboot的后台搭建(31)

IT人故事会
关注TA
已关注
手记 399
粉丝 1.7万
获赞 1318

根据下面的图,我们来建立下对应的springboot的聚合项目。源码:https://github.com/limingios/wxProgram.git 中的wx-springboot

maven主工程

选择Maven Project

选择路径

双击pom添加springboot的maven,查看maven的仓库中,springboot(1)的最后一个版本是1.5.15release直接使用1.5.15release!

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.idig8</groupId>
  <artifactId>wx-springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>小程序JAVA实战</name>
    <description>
        小程序
        maven
        springmvc
        springboot
        mybatis
        mybatis-pagehelper
        redis
        ffmpeg
        druid
        mariadb/mysql
        zookeeper音频操作
       出品:
        个人网站:https://idig8.com
        公众号:编程坑太多      
    </description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
</project>

maven子工程

  • common工程
    >使用的数据库是mariadb,mysql被oracle收购了,大家应该都知道,他的作者为了继续倡导开源免费,加上避嫌,就建立了一个分支mariadb。国外的很多打公司都慢慢从mysql切换成了mariadb,centos7内置的都是mariadb。mariadb跟mysql的命令对于开发人员来说都是一致的,所以依赖引用的时候直接引用mysql的jar就可以了。

点击pom 添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.idig8</groupId>
    <artifactId>wx-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>wx-springboot-common</artifactId>

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <!-- 排除springboot默认的logback日志框架 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 引入log4j日志依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>

        <!-- 引入spring aop依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
            </exclusions>
        </dependency>

        <!-- 资源配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- 阿里开源数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mapper -->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!--pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- apache 工具类 -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- swagger2 配置 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

        <!-- 引入 redis 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.7.RELEASE</version>
        </dependency>

        <!-- 引入zookeeper -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.0.0</version>
        </dependency>

    </dependencies>
</project>
  • pojo 工程

image.png

点击pom 添加依赖common依赖

选择common作为依赖

pojo的pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.idig8</groupId>
    <artifactId>wx-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>wx-springboot-pojo</artifactId>

  <dependencies>
    <dependency>
        <groupId>com.idig8</groupId>
        <artifactId>wx-springboot-common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>
  • mapper 工程

点击pom 添加依赖pojo依赖

mapper的pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.idig8</groupId>
    <artifactId>wx-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>wx-springboot-mapper</artifactId>
  <dependencies>
    <dependency>
        <groupId>com.idig8</groupId>
        <artifactId>wx-springboot-pojo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
  • service 工程

点击pom 添加依赖mapper依赖

service 的pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.idig8</groupId>
    <artifactId>wx-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>wx-springboot-service</artifactId>
  <dependencies>
    <dependency>
        <groupId>com.idig8</groupId>
        <artifactId>wx-springboot-mapper</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
  • api 工程

点击pom 添加依赖service依赖

api 的pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.idig8</groupId>
    <artifactId>wx-springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>wx-springboot-api</artifactId>
  <dependencies>
    <dependency>
        <groupId>com.idig8</groupId>
        <artifactId>wx-springboot-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

构建完毕

  • 增加 springboot的启动java文件

package com.idig8;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@ComponentScan(basePackages= {"com.idig8"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
  • resources 添加配置文件

image.png

log4j.properties

log4j.rootLogger=INFO,console,dailyFile
# TODO 发布到阿里云记得添加,另外控制台不输出(只输出warn或者error信息)
#INFO,console,dailyFile

#log4j.logger.org.mybatis = DEBUG
log4j.logger.com.imooc.mapper=INFO

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n

# 定期滚动日志文件,每天都会生成日志
log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.encoding=UTF-8
log4j.appender.dailyFile.Threshold=INFO
# TODO 本地日志地址,正式环境请务必切换为阿里云地址
log4j.appender.dailyFile.File=/idig8/logs/wx-web/log.log4j
log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n

application.properties


############################################################ # # 配置数据源相关   使用阿里巴巴的 druid 数据源 # ############################################################ spring.datasource.url=jdbc:mysql://ip:3306/test spring.datasource.username=XXXXXX spring.datasource.password=XXXXXX spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.druid.initial-size=1 spring.datasource.druid.min-idle=1 spring.datasource.druid.max-active=20 spring.datasource.druid.test-on-borrow=true spring.datasource.druid.stat-view-servlet.allow=true ############################################################ # # mybatis 配置 # ############################################################ # mybatis 配置 mybatis.type-aliases-package=com.idig8.pojo mybatis.mapper-locations=classpath:mapper/*.xml # 通用 Mapper 配置 mapper.mappers=com.idig8.utils.MyMapper mapper.not-empty=false mapper.identity=MYSQL # 分页插件配置 pagehelper.helperDialect=mysql pagehelper.supportMethodsArguments=true pagehelper.params=count=countSql # 文件上传配置 spring.http.multipart.maxFileSize=100Mb spring.http.multipart.maxRequestSize=1000Mb ############################################################ # # Server 服务端相关配置 # ############################################################ # 配置api端口号 server.port=8081 ############################################################ # Server - tomcat 相关常用配置 ############################################################ # tomcat的URI编码 server.tomcat.uri-encoding=UTF-8

启动 Application.java

properties 插件

PS:spring boot的聚合工程基本搭建完成。下一步就开始搭建api接口开发。


打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP