1. Create a springboot project and import the package
Spring boot starter excluding spring boot starter logging
This is the core launcher of Spring Boot, including automatic configuration, logging and YAML.
spring-boot-starter-log4j2
Compared with other log systems, log4j2 loses less data; Under the multithreading environment, the performance of the disruptor technology is more than 10 times higher than that of logback; Using jdk1 5. Concurrency, which reduces the occurrence of deadlock;
spring-boot-starter-aop
Aspect oriented programming
spring-boot-starter-data-redis
Redis is a high-performance non relational database. Redis works in memory and has extremely high performance. SpringBoot can also integrate redis into the project.
spring-boot-starter-mail
In terms of current IT system functions, mail function is a very important function. For example, the email function is required for password retrieval, email verification, email dynamic code, password forgetting, email marketing, etc. Combined with the most popular spring boot microservice, spring boot starter Mail Mail support package is launched.
spring-boot-starter-web
All jar s needed to introduce spring web
spring-boot-starter-test
test
fastjson
Fastjson is a Java library that can convert Java objects to JSON format. Of course, it can also convert JSON strings to Java objects.
mysql-connector-java
Required for mysql
spring-boot-configuration-processor
Spring uses the configuration in yml by default, but sometimes you need to use spring boot configuration processor to configure with traditional xml or properties
Add @ PropertySource("classpath:your.properties") at the beginning of the configuration class. Other usage is the same as loading yml configuration
@Property interpretation in PropertySource
1.value: indicates the path to load the configuration file.
2.ignoreResourceNotFound: whether the specified configuration file does not exist and whether an error is reported. The default is false. When set to true, the program will not report an error if the file does not exist. In actual project development, it is best to set ignoreResourceNotFound to false.
3.encoding: Specifies the encoding used to read the attribute file. We usually use UTF-8.
When we use @ Value and need to inject more values, the code will appear redundant, so @ ConfigurationProperties comes on stage
commons-lang3
Many commons toolkits provided by apache are known as the second API of Java, and lang3 package in common is most used by us
Package structure
org.apache.commons.lang3
org.apache.commons.lang3.builder
org.apache.commons.lang3.concurrent
org.apache.commons.lang3.event
org.apache.commons.lang3.exception
org.apache.commons.lang3.math
org.apache.commons.lang3.mutable
org.apache.commons.lang3.reflect
org.apache.commons.lang3.text
org.apache.commons.lang3.text.translate
org.apache.commons.lang3.time
org.apache.commons.lang3.tuple
commons-collections
Common collections enhances the Java collection framework. It provides several functions to simplify collection processing. It provides many new interfaces, implementations and utilities.
mybatis-plus-boot-starter
jar required by mybatis plus
lombok
Automatically generate setter, all parameter, no parameter and other methods
joda-time
Processing Date and time is a common requirement in Java. The basic tool classes are Date and Calendar, which we are familiar with. However, the api of these tool classes is not very convenient and powerful, so joda time, a library dedicated to processing Date and time, was born.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <!-- Exclude default logback --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/joda-time/joda-time --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.10</version> </dependency> </dependencies>
2. Mybatis paging plug-in
package com.okyang.config; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; //Enables spring to scan the configuration file @Configuration //Scan to xml under mapper package @MapperScan("com.okyang.dao.mapper") public class MybatisPlusConfig { // Paging plug-in public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor Interceptor = new MybatisPlusInterceptor(); Interceptor.addInnerInterceptor( new PaginationInnerInterceptor()); return Interceptor; } }
3. Cross domain configuration
package com.okyang.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override // Cross domain configuration public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://localhost:8080"); } }
4. springboot configuration file
#server server.port= 8888 spring.application.name=okyang_blog # datasource spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimeZone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #mybatis-plus #Print log output sql statement mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #Identification table prefix ms mybatis-plus.global-config.db-config.table-prefix=ms_
5. Writing entity classes and Mapper
package com.okyang.dao.pojo; import lombok.Data; @Data public class Article { public static final int Article_TOP = 1; public static final int Article_Common = 0; private Long id; private String title; private String summary; private int commentCounts; private int viewCounts; /** * Author id */ private Long authorId; /** * Content id */ private Long bodyId; /** *Category id */ private Long categoryId; /** * Topping */ private int weight = Article_Common; /** * Creation time */ private Long createDate; }
package com.okyang.dao.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.okyang.dao.pojo.Article; public interface ArticleMapper extends BaseMapper<Article> { }
6. Write controller
package com.okyang.controller; import com.okyang.service.ArticleService; import com.okyang.vo.Result; import com.okyang.vo.params.PageParms; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("articles") public class AticleController { @Autowired private ArticleService articleService; /** * Home page article list * @param pageParms * @return */ @PostMapping public Result listArticle(@RequestBody PageParms pageParms){ return articleService.listArticle(pageParms); } }
7. Write service
package com.okyang.service; import com.okyang.vo.Result; import com.okyang.vo.params.PageParms; public interface ArticleService { /** * Paging query article list * @param pageParms * @return */ Result listArticle(PageParms pageParms); }
8. Write a service implementation class
package com.okyang.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.okyang.dao.mapper.ArticleMapper; import com.okyang.dao.pojo.Article; import com.okyang.service.ArticleService; import com.okyang.vo.ArticleVo; import com.okyang.vo.Result; import com.okyang.vo.params.PageParms; import org.joda.time.DateTime; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleMapper articleMapper; @Override public Result listArticle(PageParms pageParms) { /** * Paging query article database table */ Page<Article> page = new Page<>(pageParms.getPage(),pageParms.getPageSize()); LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>(); //Top sort // queryWrapper.orderByDesc(Article::getWeight); //order by create_date desc queryWrapper.orderByDesc(Article::getWeight,Article::getCreateDate); Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper); List<Article> records = articlePage.getRecords(); //Can you return directly? No List<ArticleVo> articleVos = copyList(records); return Result.success(articleVos); } private List<ArticleVo> copyList(List<Article> records){ List<ArticleVo> articleVos = new ArrayList<>(); for (Article record : records) { articleVos.add(copy(record)); } return articleVos; } private ArticleVo copy(Article article){ ArticleVo articleVo = new ArticleVo(); BeanUtils.copyProperties(article,articleVo); articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm")); return articleVo; } }
9. Page default configuration
package com.okyang.vo.params; import lombok.Data; @Data public class PageParms { private int page = 1; private int pageSize = 10; }
10. vo of Result
package com.okyang.vo; import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor public class Result { private boolean success; private int code; private String msg; private Object data; public static Result success(Object data){ return new Result(true, 200, "success", data); } public static Result fail(int code,String msg){ return new Result(false, code, msg, null); } }
11,AticleVO
package com.okyang.vo; import lombok.Data; import java.util.List; @Data public class ArticleVo { private Long id; private String title; private String summary; private int commentCounts; private int viewCounts; private int weight; /** * Creation time */ private String createDate; private String author; // private ArticleBodyVo body; private List<TagVo> tags; // private List<CategoryVo> categorys; }
12,TagVo
package com.okyang.vo; import lombok.Data; @Data public class TagVo { private Long id; private String tagName; }