I. Project Introduction

In the last article, a Courier inquiry system for masturbation was read more than 1.8W and shared my self-service Courier project. I want to optimize this express query this week. Develop a background content management system, express information in the background for unified management. Searched for 2 hours to find a suitable waterfall content management system project PB-CMS. This project is suitable for building blogs and enterprise websites, and the background is content management. Key point the authors say the project is constantly being updated. So start building locally.

Ii. Development environment construction

2.1 technology stack

  • SpringBoot: A microservices framework that simplifies the initial setup and development of Spring applications.
  • Apache Shiro: A powerful and easy-to-use Java security framework for authentication, authorization, encryption, and session management
  • Mybatis Plus: a Mybatis enhancement tool, on the basis of Mybatis only do enhancement do not change, to simplify development, improve efficiency.
  • Thymeleaf: An XML/XHTML/HTML5 template engine that can be applied to transform template files to display data and text generated by your application.

2.2 the deployment

Download the project

git clone https://gitee.com/LinZhaoguan/pb-cms.git
Copy the code

Modifying a configuration file Configuration file In the resources root directory of the project, the main configuration files are as follows:

Yml # PRD (production) configuration file application.yml # Master configuration file logback-spring. XML # log configuration fileCopy the code

Application dev.yml = application dev.yml = application dev.yml = application dev.yml

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: JDBC: mysql: / / 127.0.0.1:3306 / pb - CMS - base? useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    username: root
    password: 123456
  redis:
    host: 127.0. 01.
    port: 6379
    password: 123456
    timeout: 5000
    jedis:
      pool:
        max-idle: 8
        min-idle: 0
        max-active: 8
        max-wait: - 1
server:
  servlet:
    context-path:/cms
Copy the code

Change the mysql database url (link), username (username), password (and password). Redis host(server address), port (port), password (password), if the same do not need to change.

Use Navicat to create pB-CMS-base database, as shown below:

Then import database script, database script file in docs\db\pb_cms_base. SQL, import data as shown below:

Run the project open com. Puboot. SpringbootApplication, run the main method to:

Debug
jar

java -jar pb-cms.jar Run the local JAR package
nohup java -jar pb-cms.jar > pb-cms.log & # Linux server running
Copy the code

Project operation Front-end operation results:

Note: The Theme of my idea is Material Theme UI.

3. Import Excel functions

The POM.xml configuration introduces the huTools tool here because it encapsulates many utility classes and can be used directly. Add the following dependencies to project POM.xml:

<! -- hutools-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.12.0</version>
</dependency>
Copy the code

Utility class ExcelUtils creates the utility class as follows

/ * * *@author wangzg
 * @date2020/4/13 * /
public class ExcelUtils {

    /** * get the contents of the first sheet *@param inputStream
     * @return* /
    public static List<KuaiDi> getKuaiDiList(InputStream inputStream)  {
        List<KuaiDi> kuaiDiList = new ArrayList<>();
        if(Objects.nonNull(inputStream)){
            ExcelReader excelReader = ExcelUtil.getReader(inputStream);
            // Add an alias to the table header, mainly to convert the Chinese name to the corresponding field of the database
            excelReader.addHeaderAlias("Tracking Number"."kuaidiNo");
            excelReader.addHeaderAlias("Username"."userName");
            excelReader.addHeaderAlias("Telephone"."phone");
            excelReader.addHeaderAlias("Express Company"."company");
            List<Map<String, Object>> rowList = excelReader.readAll();
            if(CollectionUtil.isNotEmpty(rowList)){
                rowList.stream().forEach(r->{
                    KuaiDi kuaiDi = new KuaiDi();
                    try {
                        populate(kuaiDi,r);
                        kuaiDi.setCreateTime(new Date());
                        kuaiDiList.add(kuaiDi);
                    } catch(Exception e) { e.printStackTrace(); }}); }}return kuaiDiList;
    }

    public static void main(String[] args) {
        List<KuaiDi> kuaiDiList = getKuaiDiList(ResourceUtil.getStream("D: \ \ Courier. XLS")); kuaiDiList.stream().forEach(System.out::println); }}Copy the code

GetKuaiDiList: kuaiDiList: kuaiDiList: kuaiDiList: kuaiDiList: kuaiDiList: kuaiDiList: kuaiDiList;

   @Override
    public void importData(MultipartFile multipartFile) throws Exception {
        List<KuaiDi> kuaiDiList = ExcelUtils.getKuaiDiList(multipartFile.getInputStream());
        if(CollectionUtil.isNotEmpty(kuaiDiList)){
            // Batch insert
            this.saveBatch(kuaiDiList); }}Copy the code

Fourth, the FAQ

3.1 Project IntegrationMybatisWhether this parameter is not specifiedtype-aliases-packageAttribute?

The answer is yes. Why single out this issue? Because I found the following configuration in the project report configuration file:

mybatis-plus:
  global-config:
    db-config:
      id-type: auto
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.puboot.model
Copy the code

Com.puboot. model package path project is not available, is type-aliases-package support fuzzy matching? I never thought about the author’s mistakes. Finally local testing, and check Mybitas Plus, the final explanation is this:

TypeAliasesPackage: MyBaits alias scanning path, by this property to package the classes to register alias, after registration when the Mapper XML file can be directly use the name of the class, instead of using the fully qualified class name (i.e. when XML call need not include the package name. Type-aliases -package supports aliases defined for multiple packages.

  /** * Packages to search type aliases. (Package delimiters are ",; \t\n") */
  private String typeAliasesPackage;
Copy the code

3.2 What is the difference between @RestController and @Controller?

Because this project integrates spring-boot-starter-Thymeleaf, we can develop our front-end page through Thymeleaf. Having learned Struts2 synchronization, you should know how to develop Web projects. The front-end request goes to the back-end controller’s business processing method, which binds the data to the context and then returns a string that matches the generated page returned to the front-end. Sometimes, however, you need the method to return json data directly from the response data. So the difference is obvious. @restController: This annotation is equivalent to @Controller + @responseBody.

3.3 How to Inject Controllerservice?

A common way to do this might be to use @AutoWired to inject the service we want, as follows:

@RequestMapping("kuaidi")
@Controller
public class KuaiDiController {

    @Autowired
    private KuaiDiService kuaiDiService;
}
Copy the code

In this project, I found a way to write it. The code is as follows:

@Controller
@RequestMapping("article")
@AllArgsConstructor
public class ArticleController {

    private final BizArticleService articleService;
    private final BizArticleTagsService articleTagsService;
    private final BizCategoryService categoryService;
    private final BizTagsService tagsService;
    private final SysConfigService configService;
}
Copy the code

This approach is essentially constructor injection, and @allargsconstructor is an annotation of the Lombok plug-in that automatically generates full-parameter constructors.

Five, the last

Open source projects make it easy to acquire and learn from other people’s source code. I am also optimizing the self-service express query function. For nothing else, just not to waste time; Each completed a function has a certain sense of achievement, let me enjoy it!

Refer to the article

  • Mybatis3.2 does not support Ant wildcards: juejin.cn/post/684490…

Restless ape man
Continuous technology sharing!