基于SpringBoot从零构建博客网站 - 技术选型和整合开发环境

1、技术选型

博客网站是基于SpringBoot整合其它模块而开发的,那么每个模块选择的技术如下:

  • SpringBoot版本选择目前较新的2.1.1.RELEASE版本
  • 持久化框架选择Mybatis
  • 页面模板引擎选择Freemarker
  • 前台框架选择Bootstrap
  • 后台框架选择AdminLTE
  • 数据库选择Mysql
  • 数据库版本管理选择Flyway

技术选型概览图,如下:

001.png

2、代码分包

首先确定本工程为sw-blog(即:守望博客),基础包名为:

com.swnote

通过前面同系列的两篇文章可知,本博客网站主要分成3个模块,即用户管理及权限相关模块、文章及专栏等博文相关模块和公共模块。为此这3个模块分别所属的包为auth、blog和common,即:

com.swnote.auth
com.swnote.blog
com.swnote.common

然后每个模块下都是有本模块的controller、service、dao和domain,所以本工程包的结构如下:

002.png

3、整合开发环境

根据前面所确定的技术,那么工程的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">

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

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.swnote</groupId>
    <artifactId>sw-blog</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>sw-blog</name>
    <url>http://maven.apache.org</url>

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

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

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

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

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

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

        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
</project>

application.yml的配置内容如下:

spring:
  application: 
    name: swblog
  datasource: 
    url: ${SWBLOG_DB_URL:jdbc:mysql://localhost:3306/swblog?characterEncoding=utf8}
    username: ${SWBLOG_DB_USERNAME:root}
    password: ${SWBLOG_DB_PWD:12345678}
    driver-class-name: com.mysql.cj.jdbc.Driver
  flyway:
    clean-disabled: true
    enabled: true
    locations: classpath:db/migration
    baseline-on-migrate: true
  freemarker:
    suffix: .ftl
    content-type: text/html
    charset: UTF-8
    cache: false
    template-loader-path:
      - classpath:/templates
  mvc:
    static-path-pattern: /static/**
    
server:
  port: ${SWBLOG_PORT:80}

mybatis: 
  mapper-locations: classpath:com/swnote/*/dao/*.xml
  type-aliases-package: com.swnote.auth.domain,com.swnote.blog.domain,com.swnote.common.domain

其中配置主要数据库的配置、flyway的配置、freemarker的配置和mybatis的配置,同时还设置4个以“SWBLOG_”开头环境变量,为后期注入值用的,如果还需要有其它的环境变量后期也会慢慢的加。

4、测试

为了检测开发环境是否正确,为此测试从数据库中获取一条数据,然后将数据传递到页面上显示。

利用comm_config表测试,首先往该表中插入一条记录,即:

003.png

Dao的Mapper文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.swnote.common.dao.ConfigDao">
    <sql id="fields">
        configId, configValue, description
    </sql>

    <!-- 根据主键获取配置信息 -->
    <select id="retrieve" parameterType="String" resultType="Config">
        select <include refid="fields"/> from comm_config where configId = #{configId} 
    </select>
</mapper>

Service层代码:

package com.swnote.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.swnote.common.dao.ConfigDao;
import com.swnote.common.domain.Config;
import com.swnote.common.service.IConfigService;

/**
 * 站点相关配置信息服务类
 * 
 * @author lzj
 * @since 1.0
 * @date [2019-04-04]
 */
@Transactional
@Service
public class ConfigService implements IConfigService {

    @Autowired
    private ConfigDao configDao;
    
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public Config retrieve(String configId) {
        return configDao.retrieve(configId);
    }
}

Controller层的测试代码:

package com.swnote.common.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.swnote.common.domain.Config;
import com.swnote.common.service.IConfigService;

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private IConfigService configService;
    
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String test(ModelMap model) {
        Config config = configService.retrieve("name");
        
        model.addAttribute("config", config);
        return "test";
    }
}

页面代码:

<!doctype html>
<html>
    <head>
        <title>测试</title>
        <meta charset="utf-8">
    </head>
    
    <body>
        <h2>${config.configValue}</h2>
    </body>
</html>

启动工程后,访问:http://127.0.0.1/test/index,结果如下:

004.png

结果是正确的,所以开发环境整合完成了。

关注我

以你最方便的方式关注我:
微信公众号:


架构与我
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • feisky云计算、虚拟化与Linux技术笔记posts - 1014, comments - 298, trac...
    不排版阅读 3,754评论 0 5
  • 一、关于Apache和Tomcat Apache HTTP Server(简称 Apache),是 Apache ...
    曼巴童鞋阅读 832评论 0 1
  • 2016 7.30 今天是在秦岭陪孩子们度过的第十三天,每一天不仅是孩子们的老师,自己同时也在进步和历练,有时候一...
    虞岛阅读 188评论 0 1
  • 2018.01.17 沙盒 iOS开发是在沙盒中开发的,对一些部分的文件的读写进行了限制,只能在几个目录下读写文件...
    摹喵居士阅读 164评论 0 0
  • 项目中使用了tomcat,Nginx,测试阶段,生产阶段经常会有些bug需要调查。需要有些日志管理工具,在没有EL...
    老肖阅读 10,740评论 0 3