zoukankan      html  css  js  c++  java
  • 基于SpringBoot从零构建博客网站

    技术选型和整合开发环境

    1、技术选型

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

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

    技术选型概览图,如下:

    2、代码分包

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

    com.swnote
    

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

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

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

    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表测试,首先往该表中插入一条记录,即:

    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,结果如下:

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

    关注我

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

  • 相关阅读:
    一. web框架-----------ES6语法和VUE介绍(一)
    十.Django---framework框架 响应器(渲染器)
    【hiho1087】Hamiltonian Cycle
    【CF160E】Buses and People
    【51nod1672】区间交
    【洛谷P2915】Mixed Up Cows
    【模板】长链剖分
    【洛谷P4552】IncDec Sequence
    【CF1181C】Flag
    【CF1181D】Irrigation
  • 原文地址:https://www.cnblogs.com/atcloud/p/10723126.html
Copyright © 2011-2022 走看看