my java web architecture scheme

//Business logic code snippet
private String word;
    private String area;
    private String type;
    private Integer year;
    private String order;
    private Integer page;

@NotNull
@Min (0)
private Integer id;

public void list(HttpServletRequest req, HttpServletResponse res) throws ... {
//Get an instance of the request parameter processing tool class, which is read directly from the context, you can also use spring or implement dependency injection yourself
        RequestParameterUtil paramUtil = (RequestParameterUtil) req.getServletContext().getAttribute("requestParameterUtil");
// get the message object
        Messages messages = (Messages) req.getAttribute("messages");
        String[] paramNames = {"word", "area", "type", "year", "order", "page"};
//The request parameter tool class uses reflection to bind to the this object according to the naming convention according to the parameter array specified by paramNames. This example is a query operation. Of course, for the insert operation, it is faster and more convenient to bind to the javaBean object.
//Then the tool class automatically executes the java standard annotation-based data validity verification validate
//bind() method returns a boolean value, indicating whether there is a binding or validation error, messages is a message object, which saves all graded messages
        paramUtil.bind(req, this, paramNames, messages);


...
        try (Connection conn = HdgangDataSource.getConnection(HostFactory.getHost())) {
            URL url = new URL("/cult/list.do");//url tool class, the constructor parameter is the request path, which can be overridden in the view. It is not recommended to set this directly for complex projects, and can be obtained from the controller
            Tool.bind(url, this, paramNames);//Bind valid request parameters to the url object
            Pagination pagination = new Pagination();//Create a query pagination tool class instance
            pagination.setRowCnt(24);//Set 24 records per page
            pagination.setPageParamName("page");//Set the request parameter name of the pagination of the url
            pagination.setCurrentPage(page);//Set the current page number
            pagination.setUrl(url);//Set url
            pagination.setEncode(res.getCharacterEncoding());//Set the default encoding of URL parameters

//DAO method
            List<Movie> movieList = MovieDAO.getList(conn, word, order, pagination);
// Can be replaced with dependency injection. pagination is a pagination object, and the view can directly match the template with one line of code to render the pagination module
            req.setAttribute("movieList", movieList);
            req.setAttribute("pagination", pagination);
        }
//You can return a string message and hand it over to the controller to handle the jump
        req.getRequestDispatcher("/cult/list.jsp").forward(req, res);
    }


//get set method omitted


//DAO method

public static List<Movie> getList(Connection conn, String word, String order, Pagination pagination)
            throws ... {

//Bind named parameters through map (similar to: xxx), also supports binding through javaBean
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("fulltext", fulltext);

//It is awkward to splicing complex sql according to the query conditions, if myBatis is implemented. The MovieSql interface is defined here, and the specific implementation can cross databases. Here new directly, the actual application should depend on injection or load and instantiate according to the configuration file.
        MovieSql movieSql = new MovieSqlMysqlImpl();

        String scalarSql= movieSql.getSelectScalarSql(word);
//DAO is a database tool class, getScalar gets the number of records that meet the conditions
        long rowTotalCnt = DAO.getScalar(conn, scalarSql, paramMap);
        List<Movie> movieList = null;
        if (rowTotalCnt > 0) {
//Set the total number of records obtained by the query to pagination
            pagination.setRowTotalCnt(rowTotalCnt);
            String sql = movieSql.getSelectListSql(word,order, pagination.getOffset(),pagination.getRowCnt());
//getBeanList gets the paging records obtained by the query. The overloading of the getBeanList method supports binding named parameters through javaBean and Map
            movieList = DAO.getBeanList(conn, sql, paramMap, Movie.class);
        }
        return movieList;
    }




//Get the interface of the SQL statement
public interface MovieSql{
     public String getSelectScalarSql(String word);
     public String getSelectListSql(String word,String order, int offset, int rowCnt);

     ...
}


/ / Implement the MovieSql interface, dynamically splicing sql statements.
public class MovieSqlMysqlImpl implements MovieSql{
     private final String select = "select `id`,`imdbid`,`name`,`year`,`duration`,`aliases`,`directors`,`actors`,`tags`,`intro`"
                + ",`areas`,`types`,`score`,`date`,`update`,`state` from `movie` ";
       private final String selectCnt = "select count(*) from `movie` ";

     private String getConditionsSqlFragment(String word){
        String conditions = "";
        String fulltext = "";
        if (word != null && !word.trim().isEmpty()) {
            word = word.trim();
            fulltext = Tool.encode(word, true, "+");
            conditions = " where match(`fulltext`) against(:fulltext IN BOOLEAN MODE) ";
        }
        return conditions;
     }

     private String getOrdersSqlFragment(String order){
        String orders;
        switch (order) {
            case "score":
                orders = " order by `score` desc ";
                break;
            case "sdate":
                orders = " order by `date` desc ";
                break;
            default:
                orders = " order by `update` desc,`date` desc ";
                break;
        }
        return orders;
     }

     @Override
     public String getSelectScalarSql(String word){
        return selectCnt +" "+ getConditionsSqlFragment(word) + ";";
     }

     @Override
     public String getSelectListSql(String word,String order, int offset, int rowCnt){
        return select +" "+ getConditionsSqlFragment(word) +" "+ getOrdersSqlFragment(order) + " limit " + offset + "," + rowCnt + ";";
     }

     ...
}



For more details, please refer to http://afadgaeg.iteye.com/blog/2155226

Architecture principles :
1. Lightweight components are better than heavyweight frameworks. Achieve complete decoupling
2. Convention is better than configuration. Achieve zero configuration _
_ _ _ If there is any violation, please inform the moderator, and I will cooperate to delete it): I am currently in Shenzhen, looking for a job in the direction of java. If you are suitable for job recruitment, you can contact my email: [email protected] or mobile phone: 13874372370






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326225005&siteId=291194637