Springboot+layui + code generation full set of forked

1. Objectives

It is to use a "tool" to complete most of the work of CURD and provide a small system that can quickly complete the functions.

The following is an open source framework, Springboot+layui, which already includes organization, user, menu, permission, automatic code generation and other functions. (for fork, there are also some functional tests)
https://gitee.com/kelvin11/springboot-plus
E:\gitlab\kelvin\springboot-layui-management
A completed case: E:\tmp \ office \ design \ xindafu springboot plus master \ springboot plus (internal MQTT)

2. Organization structure, user, role and authority sorting

Some cache queries have been commented out

What is role data authorization?

How to realize data permission? How to understand the corresponding figures?

  • Function point and menu permission

  • Function point and button permission

  • The menu with "Data permission" is configured, and the management functions such as modify and other buttons cannot be displayed

After you select view yourself only, the following sql will be executed

┏━━━━━ Debug [coreRoleFunction._gen_selectByTemplate_page] ━━━
┣ SQL: 	 select * from `core_role_function` where 1=1 and `ROLE_ID`=? and `FUNCTION_ID`=? limit ? , ?
┣ Parameters:	 [174, 185, 0, 1]
┣ Location:	 com.ibeetl.admin.console.service.FunctionConsoleService.updateFunctionAccessByRole(FunctionConsoleService.java:140)
┣ Time:	 1ms
┣ result:	 [1]
┗━━━━━ Debug [coreRoleFunction._gen_selectByTemplate_page] ━━━

┏━━━━━ Debug [coreRoleFunction._gen_updateById] ━━━
┣ SQL: 	 update `core_role_function` set `ROLE_ID`=?,`FUNCTION_ID`=?,`DATA_ACCESS_TYPE`=?,`DATA_ACCESS_POLICY`=? where `ID` = ?
┣ Parameters:	 [174, 185, 1, null, 208]
┣ Location:	 com.ibeetl.admin.console.service.FunctionConsoleService.updateFunctionAccessByRole(FunctionConsoleService.java:143)
┣ Time:	 2ms
┣ to update:	 [1]
┗━━━━━ Debug [coreRoleFunction._gen_updateById] ━━━

2.1. Users and roles

And then do the new operation

2.2. Function point, role, menu, role function authorization, role data authorization

Function node is the basis of menu and role permission. To do menu, menu permission and Data permission, you need to:

  1. First, establish the corresponding function point. If you want to create a menu, you need to first create a function point (ps: "function address" is the menu address, and "function type" is the general function)
  2. New menu item. Select the corresponding function point in menu address; if menu type is navigation, it means that there is a next level menu under this item; if menu is the actual menu level page.
  3. In "role function authorization", the function nodes that can be accessed by the authorized role will be visible
  4. In "role data authorization", if the "function type" of the function node is "including data permission", after the function node is authorized to the role, you can further set the data permission of the function node in "role data authorization". Refer to the next section for detailed functions.

2.3. Role data authorization

The background is that data permission can be divided into the following options:

For the role "Hua Bang administrator", the data authority "user list" is set to "view the same company (excluding subsidiaries)"

2.3.1. Role data authorization (page configuration)

2.3.1.1. Only view yourself - abnormal

There is an error in this case. Go back and analyze the reason

2.3.1.2. View the same company (excluding subsidiaries)

2.3.1.3. Same structure

Only users on the same layer can be seen

At this time, if a new "Huabang user 2" is added under the branch of "Jiangsu Huabang network technology", it can be seen as follows:

2.3.1.4. All under departments and sub departments

2.3.1.5. All organizations

[external link image transfer failed. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jk73rvji-1592464192855) (C: \ users \ Lenovo \ appdata \ roaming \ typora \ typora-user-images \ image-2020061165808572. PNG))

2.3.1.6. All under the group - abnormal

[failed to save the image in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-tqjbj6uw-1592464192874) (C: \ users \ Lenovo \ appdata \ roaming \ typora \ typora-user-images \ image-2020061165901373. PNG))

2.3.1.7. Parent company

2.3.1.8. Group Department

2.3.2. Role data authorization (how to Update)

Modify the data permission as shown in the figure below to see what sql statement is executed

┏━━━━━ Debug [coreRoleFunction._gen_selectByTemplate_page] ━━━
┣ SQL: 	 select * from `core_role_function` where 1=1 and `ROLE_ID`=? and `FUNCTION_ID`=? limit ? , ?
┣ Parameters:	 [174, 2, 0, 1]
┣ Location:	 com.ibeetl.admin.console.service.FunctionConsoleService.updateFunctionAccessByRole(FunctionConsoleService.java:140)
┣ Time:	 1ms
┣ result:	 [1]
┗━━━━━ Debug [coreRoleFunction._gen_selectByTemplate_page] ━━━

┏━━━━━ Debug [coreRoleFunction._gen_updateById] ━━━
┣ SQL: 	 update `core_role_function` set `ROLE_ID`=?,`FUNCTION_ID`=?,`DATA_ACCESS_TYPE`=?,`DATA_ACCESS_POLICY`=? where `ID` = ?
┣ Parameters:	 [174, 2, 2, null, 212]
┣ Location:	 com.ibeetl.admin.console.service.FunctionConsoleService.updateFunctionAccessByRole(FunctionConsoleService.java:143)
┣ Time:	 10ms
┣ to update:	 [1]
┗━━━━━ Debug [coreRoleFunction._gen_updateById] ━━━

It's the core_ role_ Set the data of this id in the function table_ ACCESS_ TYPE. DATA_ ACCESS_ See the figure above for type enumeration

2.3.3. How is role data authorization effective?

For example, in the user management menu page, there is a function to query the user list_ ID 2, code user.query ), explore how it works.

In DataAccessFunction

3. beetl related technology SQL

http://ibeetl.com/guide/#/beetlsql/quickstart

======>Considering that beetlsql may not be a commonly used ORM framework, it may take more time to replace this framework (Dao class needs to be changed, and Dao class inherits BaseMapper...)

======>Considering the introduction of mybatis plus, the existing dao code will not be modified, and the new functions will be realized through the new mybatis scheme

Overall: you may need to be familiar with beetlsql to quickly implement functions, and you may also need to try to access mybatis

3.1. Try to write a beetlsql paging query function

Query user_ For console table data, the following functions need to be implemented:

  1. According to the built CoreUser object, set the query conditions dynamically (null is not the query condition, and non null will be queried)
  2. Page by page query, 3 items per page, query page 1
  3. Fuzzy query: the user whose name contains "1" in CoreUser is found
  4. Sort by user name in ascending order
    Solution: use LambdaQuery to query (or consider using BaseMapper - this method is not recommended by the author)
    @Test
    public void testOne() throws Exception {
        CoreUser queryTemplateDto = new CoreUser();
        queryTemplateDto.setJobType0("JT_01");//JT_01 JT_02. If it is not transferred, there is no such query condition
        LambdaQuery<CoreUser> query = userConsoleDao.createLambdaQuery()
                .andLike(CoreUser::getName,"%1%")   // Realize function point 3
                .andEq(CoreUser::getJobType0, Query.filterEmpty(queryTemplateDto.getJobType0()))   // Implement function point 1
                .asc(CoreUser::getName);   // Implement function point 4
        query.page(1, 3);   // Implement function point 2
//        List<CoreUser> userList = query.page(2, 2).getList();
//        for (CoreUser user: userList) {
//            System.out.println(user);
//        }
    }

3.2. New table: generate PO, create Dao, and execute query process

3.2.1. Generate PO

    @Test
    public void testGenerateCode() throws Exception {
        // Or generate java files directly
        GenConfig config = new GenConfig();
        config.preferBigDecimal(true);
        config.setPreferDate(true);
        sqlManager.genPojoCodeToConsole("test_product");// Quick build, display to console
    }

3.2.2. Create Dao

Just inherit BaseMapper directly. Pay attention to setting generic classes

3.2.3. Execute query

@Test
    public void testNewPO() {
        LambdaQuery<TestProduct> query = testProductDao.createLambdaQuery()
                .desc(TestProduct::getDelFlag);
        List<TestProduct> productList = query.select();
        for (TestProduct tmp : productList) {
            System.out.println(tmp.getProductName());
        }

    }

3.3. User defined sql query

3.3.1. Query list

List<TestMachine> machineList = testMachineDao.execute("SELECT * FROM test_machine WHERE machine_name LIKE ?", "%Dell%");

3.3.2. Query returns specific POJO

  • Create a new dto class to receive query results

    public class MyMachine{
        private String machineName;
    
        public String getMachineName() {
            return machineName;
        }
    
        public void setMachineName(String machineName) {
            this.machineName = machineName;
        }
    
        @Override
        public String toString() {
            return "MyMachine{" +
                    "machineName='" + machineName + '\'' +
                    '}';
        }
    }
    
  • In the src/main/resource directory, create the testMachine.md File, content is

    getTestMachineA
    ===
    
    	select * from test_machine
    	
    
  • stay testMachineDao.java New getMachineA() method

    @SqlResource("springbootplus.testMachine")
    public interface TestMachineDao extends BaseMapper<TestMachine>{
        public PageQuery<TestMachine> queryByCondition(PageQuery query);
        public void batchDelTestMachineByIds( List<Long> ids);
    
        public List<MyMachine> getTestMachineA();
    }
    
  • Run test method

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = {MainApplication.class})// Specify startup class
    public class ApplicationTests {
    
        @Autowired
        TestMachineDao machineDao;
    
        /**
         * Generate code for PO class
         * @throws Exception
         */
        @Test
        public void testSql() throws Exception {
            List<MyMachine> getTestMachineA = machineDao.getTestMachineA();
            System.out.println("111");
        }
    
    }
    
  • The screenshot of directory structure and operation is as follows

4. Generating subsystem

In principle, the author does not recommend modifying the code in admin console or admin core. It is recommended to automatically generate a project (actually a module) first. This module depends on admin console and admin core, and then import this module

4.1. Generate new module

4.2. Import module

Start from the new module

However, I found the project directory a little strange

So consider rebuilding it, and then select the new module directory to the same level directory of the current admin core (the purpose is that git management is simpler, and the project code is all together)

Then, just import the module, test and start the new project MainApplication, and access the management system

4.3. Make a query interface in the subsystem

You can use LambdaQuery directly without additional configuration

5. Generate page + service+dao+PO

5.1. Automatic code generation

The generated path is selected in "E: \ gitlab \ Kelvin \ springboot lay UI management \ springbootplus". This is my new project directory. In this way, the newly created files will be automatically placed in this directory. Note that the automatically generated code package s are dao, entity, service and web

In this way, HTML & JS, controller, service, dao and entity have been generated

5.2. Create a new "machine management" function point

5.3. Create a new "machine management" menu

5.4. Visit the "machine management" page

[failed to transfer the pictures in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-aqOOfp18-1592464193052)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20200618091302500.png))

See background error report

Found no service restart Access after restart

There is a small problem that some fields do not need to be displayed on the page. For example, when "add", you do not need to fill in "add time" and "update time"

At present, there is no solution to this problem. It is estimated that it needs to be manually modified. After all, if display is not configured, you need to change to nullable or insert some default values in db. This may need to be implemented by yourself.

Tags: SQL SpringBoot Java Mybatis

Posted on Thu, 18 Jun 2020 03:50:16 -0400 by otuatail