当前位置:网站首页>< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (2)

< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (2)

2022-04-21 20:21:00 indaeyo

2021SC@SDUSC

The last article analyzed the code module module-page And its first sub module module-page-model, This article will analyze :

  • Sub module module-page-service、model-page-service-provider Overview .
  • Interface class SinglePageService, Implementation class SinglePageServiceProvider.
  • Because of the length , The next article will analyze the code of the classes not analyzed in the two sub modules .

Catalog

1. module-page-service、model-page-service-provider summary

2. The required depend on

2.1 module-page-service

2.2 model-page-service-provider

3. Interface class SinglePageService.java

4. Implementation class SinglePageServiceProvider.java

5. Add :SEO

6. summary


1. module-page-service、model-page-service-provider summary

  • module-page-service Is a package that writes interfaces , contain SinglePageService And SinglePageCommentService, It's for a single page 、 A single comment provides the function definition of the corresponding operation database , For example, delete according to the page primary key Model、 Add or update Model Data etc. .
  • model-page-service-provider Implement the above interface , The specific implementation of the method in the interface is given .
  • model-page-service-provider In addition to implementing the interface , still PageViewsCountUpdateTask Class , By inheritance Runnable Interface , Calculate the number of page visits per article .

2. The required depend on

2.1 module-page-service

<dependencies>
        <dependency>
            <groupId>io.jboot</groupId>
            <artifactId>jboot</artifactId>
        </dependency>

        <dependency>
            <groupId>io.jpress</groupId>
            <artifactId>module-page-model</artifactId>
            <version>4.0</version>
        </dependency>
</dependencies>

module-page-service The underlying dependence jboot, and jboot Is based on jfinal Framework implementation . The related functions using these two frameworks will be specifically mentioned below . And this module needs to use the... Analyzed in the previous article module-page-model package , Entity class module .

2.2 model-page-service-provider

<dependencies>

        <dependency>
            <groupId>io.jboot</groupId>
            <artifactId>jboot</artifactId>
        </dependency>

        <dependency>
            <groupId>io.jpress</groupId>
            <artifactId>jpress-core</artifactId>
        </dependency>

        <dependency>
            <groupId>io.jpress</groupId>
            <artifactId>module-page-model</artifactId>
            <version>4.0</version>
        </dependency>

        <dependency>
            <groupId>io.jpress</groupId>
            <artifactId>module-page-service</artifactId>
            <version>4.0</version>
        </dependency>

    </dependencies>

because model-page-service-provider To implement a class , So you need an interface package module-page-service, And the core operation code jpress-core.

3. Interface class SinglePageService.java

  • This class inherits from JbootServiceJoiner.
  • JbootServiceJoiner The most important of all 、 The most frequently used is JbootModel.
  • JbootModel The underlying dependence JFinal Of ActiveRecord, yes JFinal The core component , Provided in addition to the previous home cooked Model Richer database operation functions outside the class .
import com.jfinal.plugin.activerecord.Page; // Operate on the page 
import com.jfinal.plugin.activerecord.Table; // Operate on the table 
import com.jfinal.plugin.activerecord.Db; // Operation on Database 
import com.jfinal.plugin.activerecord.Model;
  • therefore ,JbootServiceJoiner It can provide a single table as a database table 、 Multi table join function , Examples are as follows .
public interface JbootServiceJoiner {
	<M extends JbootModel> Page<M> join(Page<M> page, String columnName);
	<M extends JbootModel> List<M> joinMany(List<M> models, String targetColumnName, String joinName, String[] attrs);
}
  • SinglePageService By using JbootServiceJoiner Service for , Define the function of adding, deleting, modifying and querying the database , Examples are as follows .
/**
     *  according to   Primary key   lookup  Model
     *
     * @param id
     * @return
     */
    SinglePage findById(Object id);
/**
     *  Add or update  Model  data ( The primary key value is  null  Just add , Not for  null  Update )
     *
     * @param model
     * @return  After adding or updating successfully , Return to the  Model  The primary key value 
     */
    Object saveOrUpdate(SinglePage model);
// Page status management 
Page<SinglePage> _paginateByStatus(int page, int pagesize, String title, String status);

4. Implementation class SinglePageServiceProvider.java

  • SinglePageServiceProvider Inherit JbootServiceBase, Use the functions to realize SinglePageService.
  • SinglePageServiceProvider Realization SinglePageService, Implement the specific functions .
  • JbootServiceBase There is DAO attribute , by JbootModel type , We use concrete functions through him .
  • The following describes the purpose of each function , Important functions will be posted with detailed code to show .

Through the article id Clear cache .

public void deleteCacheById(Object id)

Use Db Class , adopt id Delete articles .buildInSqlPara It is possible to build sql Parameters of , And link as in the previous statement . We don't need to pass a few parameters .

 @Override
    public boolean deleteByIds(Object... ids) {
        return Db.update("delete from single_page where id in  " + SqlUtils.buildInSqlPara(ids)) > 0;
    }

According to the State ( draft 、 Release 、 dustbin ) Mark the number of pages .

    @Override
    public Page<SinglePage> _paginateByStatus(int page, int pagesize, String title, String status) {
				// Create a column , Is the status bit of the article 
        Columns columns = Columns.create("status", status);
// If the status bit is not empty , That is already in the draft 、 Release or in the dustbin 
        if (StrUtil.isNotBlank(title)) {
// Link fuzzy query 
            columns.like("title", "%" + title + "%");
        }
// Number of identification pages 
        return DAO.paginateByColumns(page,
                pagesize,
                columns,
                "id desc");
    }

Same function as the previous function , The page number in the trash can is given .

  @Override
    public Page<SinglePage> _paginateWithoutTrash(int page, int pagesize, String title)

Change the page status bit .

    @Override
    public boolean doChangeStatus(long id, String status)

Change the implementation of page status bit ,Seomanager To optimize its core keywords .

    @Override
    public boolean update(SinglePage model) {
        boolean isSuccess = super.update(model);
        if (isSuccess && model.isNormal()) {
            SeoManager.me().ping(model.toPingData());
            SeoManager.me().baiduUpdate(model.getUrl());
        }
        return isSuccess;
    }

Save page , Used to modify the page , At the same time, optimize the core keywords .

    @Override
    public Object save(SinglePage model) {
        Object ret = super.save(model);
        if (ret != null && model.isNormal()) {
            SeoManager.me().ping(model.toPingData());
            SeoManager.me().baiduPush(model.getUrl());
        }
        return ret;
    }

The article counts a certain status bit , Easy to display on the page .

    @Override
    public int findCountByStatus(String status) 

adopt slug Find the article , Get the first article .

    @Override
    public SinglePage findFirstBySlug(String slug) {
        return DAO.findFirstByColumn(Column.create("slug", slug));
    }

adopt flag Find the article , Get a list of articles .

    @Override
    public List<SinglePage> findListByFlag(String flag) 

 

Calculate the number of visitors per page . By calling PageViewsCountUpdateTask Class recordCount Method to implement .

    @Override
    public void doIncViewCount(long id) {
        PageViewsCountUpdateTask.recordCount(id);
    }

5. Add :SEO

SEO The standard definition of :Search Engine Optimization, Search engine optimization . It refers to the optimization of website content according to the rules of search engine , Improve the website's natural ranking of keyword search results in search engines , Move your brand and products forward .

JPress When it was released , Set... In the article seo:

 

6. summary

This blog analyzes the interface class and implementation class of page processing , come to know JPress How to use JBoot、JFinal, adopt Model class 、Db+Record Class etc. , Diversity 、 Simple database operation method .

版权声明
本文为[indaeyo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212013209112.html