Java Backend Management System (IX): Code collation optimization

Source: Internet
Author: User

Project planning

For unified configuration and code decoupling, we have re-organized and planned the code.

After re-planning, the code structure is as follows:

Kitty-pom: Unified management of Maven versions, packaged configurations

Kitty-common: Common code module, main Placement tool class

Kitty-core: Core code module, main package public business module

Kitty-admin: Background management module, including users, roles, menu management, etc.

kitty-boot:spring boot boot module with some global configuration information

Optimization Details Kitty-core

1. Create a new Kitty-core project and migrate the contents of the Kitty-admin project page package to the Kitty-core project page package.

2. Adding Kitty-common dependencies

<dependency>    <groupId>com.louis</groupId>    <artifactid>kitty-common</ Artifactid>    <version>0.0.1-SNAPSHOT</version>
</dependency>

3. Add unified Controller Interface return results package Httpresult

Httpresult.java

 Packagecom.louis.kitty.core.http; Public classHttpresult {Private intCode; PrivateString msg; PrivateObject data;  Public StaticHttpresult error () {returnError (Httpstatus.sc_internal_server_error, "Unknown exception, please contact Administrator"); }         Public Statichttpresult Error (String msg) {returnerror (Httpstatus.sc_internal_server_error, msg); }         Public StaticHttpresult Error (intcode, String msg) {Httpresult R=NewHttpresult ();        R.setcode (code);        R.setmsg (msg); returnR; }     Public Statichttpresult OK (String msg) {Httpresult R=NewHttpresult ();        R.setmsg (msg); returnR; }         Public Statichttpresult OK (Object data) {Httpresult R=NewHttpresult ();        R.setdata (data); returnR; }         Public StaticHttpresult ok () {return NewHttpresult (); }     Public intGetCode () {returnCode; }     Public voidSetcode (intcode) {         This. Code =Code; }     PublicString getmsg () {returnmsg; }     Public voidsetmsg (String msg) { This. msg =msg; }     PublicObject GetData () {returndata; }     Public voidsetData (Object data) { This. data =data; }    }

4. Adding a universal Curd interface

Curdservice.java

 PackageCom.louis.kitty.core.service;Importjava.util.List;Importcom.louis.kitty.core.page.PageRequest;ImportCom.louis.kitty.core.page.PageResult;/*** Universal Curd Interface*/ Public InterfaceCurdservice<t> {    /*** Save operation *@paramRecord *@return     */    intSave (T record); /*** Update operation *@paramRecord *@return     */    intupdate (T record); /*** Delete operation *@paramRecord *@return     */    intDelete (T record); /*** Bulk Delete operation *@paramentities*/    intDelete (list<t>Records); /*** Search by ID *@paramID *@return     */T FindByID (Long ID); /*** Paged Query * This encapsulates the paging request and results, avoiding direct introduction of specific frames of paging objects, such as MyBatis or JPA paging objects * To avoid situations where the service layer, the control layer's paging interface need to be changed because the ORM framework is replaced, and the ORM framework is replaced does not * affect the service layer above the paging interface, to understand the role of decoupling *@paramPagerequest Custom, unified paging query Request *@returnPageresult Custom, Unified paged Query Results*/pageresult findpage (pagerequest pagerequest); }

5. Dependent Parent Pom

Kitty-admin

1. Add Kitty-common dependencies.

<dependency>    <groupId>com.louis</groupId>    <artifactid>kitty-common</ Artifactid>    <version>0.0.1-SNAPSHOT</version></dependency>

2. Delete the page package contents.

3. Replace the Controller's return result with Httpresult.

4. The Service interface inherits the Curdservice interface uniformly.

5. Service implementation class, additions and deletions change the generic code example.

 PackageCom.louis.kitty.admin.sevice.impl;Importjava.util.List;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportCom.github.pagehelper.PageHelper;ImportCom.github.pagehelper.PageInfo;ImportCom.louis.kitty.admin.dao.SysUserMapper;ImportCom.louis.kitty.admin.model.SysUser;ImportCom.louis.kitty.admin.sevice.SysUserService;Importcom.louis.kitty.core.page.PageRequest;ImportCom.louis.kitty.core.page.PageResult;Importcom.louis.kitty.core.page.PageUtils; @Service Public classSysuserserviceimplImplementsSysuserservice {@AutowiredPrivateSysusermapper Sysusermapper; @Override Public intSave (Sysuser record) {returnsysusermapper.insertselective (record); } @Override Public intupdate (Sysuser record) {returnsysusermapper.updatebyprimarykeyselective (record); } @Override Public intDelete (Sysuser record) {returnSysusermapper.deletebyprimarykey (Record.getuserid ()); } @Override Public intDelete (list<sysuser>Records) {         for(Sysuser record:records) {delete (record); }        return1; } @Override PublicSysuser FindByID (Long id) {returnSysusermapper.selectbyprimarykey (ID); } @Override Publicpageresult findpage (pagerequest pagerequest) {returnPageutils.getpageresult (Pagerequest, GetPageInfo (pagerequest)); }        /*** Call the paging plugin to complete the page pagination *@paramPagequery *@return     */    PrivatePageinfo<sysuser>GetPageInfo (pagerequest pagerequest) {intPagenum =Pagerequest.getpagenum (); intPageSize =pagerequest.getpagesize ();        Pagehelper.startpage (Pagenum, pageSize); List<SysUser> Sysmenus =Sysusermapper.findpage (); return NewPageinfo<sysuser>(Sysmenus); } @Override PublicList<sysuser>FindAll () {returnSysusermapper.findall (); }}

6. Dependent Parent Pom

Kitty-boot

1. Add Kitty-common dependencies.

<dependency>    <groupId>com.louis</groupId>    <artifactid>kitty-common</ Artifactid>    <version>0.0.1-SNAPSHOT</version></dependency>

2. Dependent parent Pom

Kitty-pom

1. New Kitty-pom project, add Pom.xml.

2. Add pre-dependent, Rollup version properties

3. Add a Maven submodule to add a packaged configuration

Compile Package

Select the Pom.xml under Kitty-pom to package.

Resources

http://maven.apache.org

https://www.yiibai.com/maven/

SOURCE download

Code Cloud: Https://gitee.com/liuge1988/kitty

The rain recalls the light dust
Source: https://www.cnblogs.com/xifengxiaoma/
All rights reserved, welcome reprint, Reprint please indicate the original author and source.

Java Backend Management System (IX): Code collation optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.