基於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,結果如下:
結果是正確的,所以開發環境整合完成了。
關注我
以你最方便的方式關注我:
微信公眾號:
