基於SpringBoot從零構建博客網站 - 技術選型和整合開發環境

技術選型和整合開發環境

一、技術選型

博客網站是基於SpringBoot整合其它模塊而開發的,那麼每一個模塊選擇的技術以下:html

  • SpringBoot版本選擇目前較新的2.1.1.RELEASE版本
  • 持久化框架選擇Mybatis
  • 頁面模板引擎選擇Freemarker
  • 前臺框架選擇Bootstrap
  • 後臺框架選擇AdminLTE
  • 數據庫選擇Mysql
  • 數據庫版本管理選擇Flyway

技術選型概覽圖,以下:java

二、代碼分包

首先肯定本工程爲sw-blog(即:守望博客),基礎包名爲:mysql

com.swnote

經過前面同系列的兩篇文章可知,本博客網站主要分紅3個模塊,即用戶管理及權限相關模塊、文章及專欄等博文相關模塊和公共模塊。爲此這3個模塊分別所屬的包爲auth、blog和common,即:web

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

而後每一個模塊下都是有本模塊的controller、service、dao和domain,因此本工程包的結構以下:spring

三、整合開發環境

根據前面所肯定的技術,那麼工程的pom文件內容以下:sql

<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_」開頭環境變量,爲後期注入值用的,若是還須要有其它的環境變量後期也會慢慢的加。apache

四、測試

爲了檢測開發環境是否正確,爲此測試從數據庫中獲取一條數據,而後將數據傳遞到頁面上顯示。微信

利用comm_config表測試,首先往該表中插入一條記錄,即:mybatis

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,結果以下:

結果是正確的,因此開發環境整合完成了。

關注我

以你最方便的方式關注我:
微信公衆號:

相關文章
相關標籤/搜索