Have you ever been able to complete the components of a simple module without writing code?

Source: Internet
Author: User

The opening four even asked
    1. Do you bother to write normal additions and deletions to the method?
    2. Do you not like the code generation plug-in duplicate code?
    3. Do you crave a project with no redundant code?
    4. Do you want to complete a simple module without writing a single line of code?
Component origin

As a back-end programmer, I believe you have written the following kinds of code:

    1. Query based on PRIMARY key
    2. Multi-Criteria Paging query
    3. Insert
    4. Modify based on primary key
    5. Delete by primary key (single or bulk)

Aside from the business, these kinds of code are the most basic code in our project, and a lot of this code will appear in each project. So how do you write this code in your project?

    1. Write according to Dao-service-controller's process?
    2. Copy an existing module and fix the changes?
    3. Use code to generate plugins?
    4. Package a component yourself?
      <br><br>
      For me personally, I came from the top four steps. Just beginning to learn Java, follow the steps of the teacher from DAO to service interface to implementation class to controller. Wait until the work to feel that there is no big deal, direct copy after the modification. After working for a while, I feel that I'm too tired to write and start using code to generate plugins. <br><br>
      But the use of plug-ins after a period of time I feel that there are too many redundant code in each project, a single method name in the global search occurs dozens of times, if there is a plug-in code needs to be modified dozens of times, a little attention to the digging a hole and so on. And then I was thinking, since these are all redundant methods, then why don't I just encapsulate a component and abstract it out to make a component. I only maintain this component in future development, and put the rest of my energy into the business code? What components do I encapsulate?

      Component Name: Syj-minicode

When some of the single-table additions and deletions you just need to tell the front-end to call these interfaces on the line

The entityname that appears below is the object name of the current operation or the database table name (note that the naming convention conforms to the Hump nomenclature, for example: Userorder or User_order can) remove the interface for physical deletion, use the update interface for Tombstone

  1. Querying objects by primary key
    @RequestMapping(value = "/syj-api/{entityName}/{id}", method = RequestMethod.GET)
  2. Paging Query

    @RequestMapping(value = "/syj-api/{entityName}/page", method = RequestMethod.POST)@RequestBody: GridPageRequest

    Here's gridpagerequest for the condition of paged query, look at its constituent elements

    /** * 查询关键字Map  */private Map<String, String> searchMap;/** * 模糊查询关键字Map */private Map<String, String> likeSearchMap;/** * 排序关键字Map */private Map<String, String> orderMap;/** * 分组关键字数组 */private String[] groupArray;private int pageNum;private int pageSize;

    The front-end query only needs to assemble the Gridpagerequest object according to the query condition.

  3. Insert
    @RequestMapping(value = "/syj-api/{entityName}/insert", method = RequestMethod.POST)@RequestBody: Object(待插入对象)
  4. Update based on primary key
    @RequestMapping(value = "/syj-api/{entityName}/update", method = RequestMethod.PUT)@RequestBody: Object(待更新对象)
  5. Delete based on primary key
    @RequestMapping(value = "/syj-api/{entityName}/{id}", method = RequestMethod.DELETE)
  6. Bulk Delete
    @RequestMapping(value = "/syj-api/{entityName}/deleteByIds", method = RequestMethod.DELETE)@RequestBody: List<String> ids(待删除主键列表)
Expansion interface
  1. Because the field name of the primary key by which a single object queries, modifies, deletes by default uses "id", it is likely that the project uses a primary key such as UserID, OrderId, Roleid, and so on, if your project conforms to this condition to inject the bean below.

               @Bean           public IdField idField() {               Map<String,String> map=new ConcurrentHashMap<>();               map.put("user","userId");//key的名称为实体名称或数据库表名称,value为主键属性名               map.put("order","orderId");               map.put("role","roleId");               IdField idField=new IdField();               idField.setMap(map);               return idField;           
  2. Some items are inserted and updated with information such as the creator and the person who modified it. If you have this requirement, please call the following method

      1. Inserting an extension

    Create a bean named Insertextend and implement the Defaultextend interface

    @Componentpublic class InsertExtend implements ExtendInterface{    @Override    public Map<String, String> exectue() {        Map<String, String> map=new HashMap<>();        map.put("createTime", System.currentTimeMillis());        map.put("createBy", "创建人id");        map.put("createUserName", "创建人名称");        return map;    }}
      1. Update extension

        Create a bean named Updateextend and implement the Defaultextend interface

        @Componentpublic class UpdateExtend implements ExtendInterface{    @Override    public Map<String, String> exectue() {        Map<String, String> map=new HashMap<>();        map.put("updateTime", System.currentTimeMillis());        map.put("updateBy", "修改人id");        map.put("updateUserName", "修改人名称");        return map;    }}
Is there a more flexible way to use it?

The use of the above is directly abstract to the controller layer, to solve the general need is no problem, but we have business logic, then there is business logic in the case of how to use it?
You can call in the service after you have finished processing the business logic

  1. Initialization
    private BaseService getUserBaseService(){return ServiceBeanFactory.getBean("User");}
  2. Query by ID
    Map&lt;String,Object&gt; userMap=getUserBaseService().selectOneById("115");User user=(User) BeanMapUtil.mapToBean(map,User.class);
  3. Query the list by condition (I believe you already know how the Gridpagerequest object is assembled)
    List&lt;Map&lt;String, Object&gt;&gt; userMaps=getUserBaseService().selectBySelective(gridPageRequest);for (Map&lt;String, Object&gt; map:userMaps){User user=(User) BeanMapUtil.mapToBean(map,User.class);    }
  4. Insert
    getUserBaseService().insertSelective(user);
  5. Update
    getUserBaseService().updateByIdSelective(user);
  6. Delete
    getUserBaseService().deleteById("115");
  7. Bulk Delete
    List&lt;String&gt; list=new ArrayList&lt;&gt;();list.add("115");list.add("116");list.add("117");getUserBaseService().deleteByIds(list);
    Is the component useful?

    After reading my introduction you think this component can meet your basic needs, if you can, please visit the GitHub link below to see how and how to use the source code of this component.

GitHub

This article is from http://zhixiang.org.cn, reproduced please retain.

Have you ever been able to complete the components of a simple module without writing code?

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.