Explorar el Código

基于SQL管理工具的使用范例

huangbo %!s(int64=4) %!d(string=hace) años
padre
commit
325f7b532d

+ 7 - 7
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/control/AuthController.java

@ -23,15 +23,15 @@ public class AuthController {
23 23
    
24 24
    /**
25 25
     * 登录
26
     * @param param
26
     * @param input
27 27
     */
28 28
    @ResponseBody
29 29
    @RequestMapping("/login")
30
    public JMap login(JMap param) throws Exception {
30
    public JMap login(JMap input) throws Exception {
31 31
        log.debug("正在登陆");
32 32
        /*获取账号密码*/
33
        String username = param.getString("username");
34
        String password= param.getString("password");
33
        String username = input.getString("username");
34
        String password= input.getString("password");
35 35
        
36 36
        /*校验账号密码*/
37 37
        JMap result = new JsonMap();
@ -45,14 +45,14 @@ public class AuthController {
45 45
    }
46 46
    /**
47 47
     * 获取菜单
48
     * @param param
48
     * @param input
49 49
     */
50 50
    @ResponseBody
51 51
    @RequestMapping("/menu")
52
    public JMap takeMenu(JMap param) throws Exception {
52
    public JMap takeMenu(JMap input) throws Exception {
53 53
        log.debug("获取菜单");
54 54
        /*获取账号*/
55
        String username = param.getString("username");
55
        String username = input.getString("username");
56 56
        String menuJson = authService.menu(username);
57 57
        JMap result = new JsonMap();
58 58
        result.put("menu",menuJson);

+ 58 - 63
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/control/DbSqlMgmtController.java

@ -1,63 +1,58 @@
1
package com.ai.ipu.server.control;
2
3
import java.util.List;
4
import java.util.Map;
5
6
import com.ai.ipu.basic.util.IpuUtility;
7
import com.ai.ipu.server.util.RestScaffoldConstant;
8
import org.springframework.stereotype.Controller;
9
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.ResponseBody;
11
12
import com.ai.ipu.data.JMap;
13
import com.ai.ipu.data.impl.JsonMap;
14
import com.ai.ipu.sql.mgmt.ISqlMgmtDao;
15
import com.ai.ipu.sql.mgmt.SqlMgmtDaoFactory;
16
17
/**
18
 * @author huangbo@asiainfo.com
19
 * @team IPU
20
 * @date 2019年11月20日下午3:14:31
21
 * @desc SQL管理使用范例
22
 */
23
@Controller
24
@RequestMapping("/db/mgmt")
25
public class DbSqlMgmtController {
26
    private String connName = "test";
27
28
    @ResponseBody
29
    @RequestMapping("/select")
30
    public JMap executeSqlByFileRepository(JMap param) throws Exception {
31
        // TODO Auto-generated method stub
32
        JMap params = new JsonMap();
33
        params.put("pk", param.getInt("pk", 0));
34
        ISqlMgmtDao dao = SqlMgmtDaoFactory.createFileSqlMgmtDao(connName);
35
        List<Map<String, Object>> dataResult = dao.executeSelect("ipu.demo.ipu-db-demo", "select", params);
36
        JMap result = new JsonMap();
37
        result.put("result", dataResult);
38
        return result;
39
    }
40
41
    @ResponseBody
42
    @RequestMapping("/insert")
43
    public JMap inertByDbRepository(JMap param) throws Exception {
44
        // TODO Auto-generated method stub
45
        JMap params = new JsonMap();
46
        params.put("pk", 99);
47
        params.put("string_type", "字符串123");
48
        params.put("int_type", 999);
49
        params.put("decimal_type", 9.99);
50
        params.put("date_type", "2019-09-09");
51
        params.put("datetime_type", "2019-07-09 15:10:24");
52
        params.put("null_type", "");
53
        ISqlMgmtDao dao = SqlMgmtDaoFactory.createFileSqlMgmtDao(connName);
54
        JMap result = new JsonMap();
55
        try{
56
            int num = dao.executeInsert("ipu.demo.ipu-db-demo", "insert", params);
57
            result.put("result", num);
58
        }catch(Exception e){
59
            IpuUtility.errorCode(RestScaffoldConstant.SERVER_003);
60
        }
61
        return result;
62
    }
63
}
1
package com.ai.ipu.server.control;
2

3
import java.util.List;
4
import java.util.Map;
5

6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.stereotype.Controller;
8
import org.springframework.web.bind.annotation.RequestMapping;
9
import org.springframework.web.bind.annotation.ResponseBody;
10

11
import com.ai.ipu.data.JMap;
12
import com.ai.ipu.data.impl.JsonMap;
13
import com.ai.ipu.server.service.DbSqlMgmtService;
14

15
/**
16
 * @author huangbo@asiainfo.com
17
 * @team IPU
18
 * @date 2019年11月20日下午3:14:31
19
 * @desc 使用SQL管理工具来开发业务功能的使用范例
20
 */
21
@Controller
22
@RequestMapping("/db/mgmt")
23
public class DbSqlMgmtController {
24
    @Autowired
25
    private DbSqlMgmtService dbSqlMgmtService;
26
    
27
    @ResponseBody
28
    @RequestMapping("/select")
29
    public JMap select(JMap input) throws Exception {
30
        JMap params = new JsonMap();
31
        params.put("pk", input.getInt("pk", 0));
32
        
33
        List<Map<String, Object>> dataResult = dbSqlMgmtService.select(params);
34
        
35
        JMap result = new JsonMap();
36
        result.put("result", dataResult);
37
        return result;
38
    }
39

40
    @ResponseBody
41
    @RequestMapping("/insert")
42
    public JMap insert(JMap input) throws Exception {
43
        JMap param = new JsonMap();
44
        param.put("pk", 99);
45
        param.put("string_type", "字符串123");
46
        param.put("int_type", 999);
47
        param.put("decimal_type", 9.99);
48
        param.put("date_type", "2019-09-09");
49
        param.put("datetime_type", "2019-07-09 15:10:24");
50
        param.put("null_type", "");
51
        
52
        int num = dbSqlMgmtService.insert(param);
53
        
54
        JMap result = new JsonMap();
55
        result.put("result", num);
56
        return result;
57
    }
58
}

+ 11 - 0
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/service/DbSqlMgmtService.java

@ -0,0 +1,11 @@
1
package com.ai.ipu.server.service;
2

3
import java.util.List;
4
import java.util.Map;
5

6
public interface DbSqlMgmtService {
7

8
    public List<Map<String, Object>> select(Map<String, Object> param) throws Exception;
9
    
10
    public int insert(Map<String, Object> param) throws Exception;
11
}

+ 43 - 0
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/service/impl/DbSqlMgmtServiceImpl.java

@ -0,0 +1,43 @@
1
package com.ai.ipu.server.service.impl;
2

3
import java.util.List;
4
import java.util.Map;
5

6
import org.springframework.stereotype.Service;
7

8
import com.ai.ipu.basic.util.IpuUtility;
9
import com.ai.ipu.server.service.DbSqlMgmtService;
10
import com.ai.ipu.server.util.RestScaffoldConstant;
11
import com.ai.ipu.sql.mgmt.ISqlMgmtDao;
12
import com.ai.ipu.sql.mgmt.SqlMgmtDaoFactory;
13

14
/**
15
 * @author huangbo@asiainfo.com
16
 * @team IPU
17
 * @date 2020年10月12日下午6:56:15
18
 * @desc 没有增加注解@Service,DbSqlMgmtService无实现类报错:
19
 * expected at least 1 bean which qualifies as autowire candidate
20
 */
21
@Service
22
public class DbSqlMgmtServiceImpl implements DbSqlMgmtService{
23
    private String connName = "test";
24
    
25
    @Override
26
    public List<Map<String, Object>> select(Map<String, Object> param) throws Exception {
27
        ISqlMgmtDao dao = SqlMgmtDaoFactory.createFileSqlMgmtDao(connName);
28
        List<Map<String, Object>> dataResult = dao.executeSelect("ipu.demo.ipu-db-demo", "select", param);
29
        return dataResult;
30
    }
31

32
    @Override
33
    public int insert(Map<String, Object> param) throws Exception {
34
        ISqlMgmtDao dao = SqlMgmtDaoFactory.createFileSqlMgmtDao(connName);
35
        int num = 0;
36
        try{
37
            num = dao.executeInsert("ipu.demo.ipu-db-demo", "insert", param);
38
        }catch(Exception e){
39
            IpuUtility.errorCode(RestScaffoldConstant.SCAFFOLD_CODE_003);
40
        }
41
        return num;
42
    }
43
}

+ 7 - 7
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/util/RestScaffoldConstant.java

@ -9,11 +9,11 @@ package com.ai.ipu.server.util;
9 9
public class RestScaffoldConstant {
10 10

11 11
    /*异常编码*/
12
    public static final String SERVER_001 = "SERVER_001";
13
    public static final String SERVER_002 = "SERVER_002";
14
    public static final String SERVER_003 = "SERVER_003";
15
    public static final String SERVER_004 = "SERVER_004";
16
    public static final String SERVER_005 = "SERVER_005";
17
    public static final String SERVER_006 = "SERVER_006";
18
    public static final String SERVER_007 = "SERVER_007";
12
    public static final String SCAFFOLD_CODE_001 = "SCAFFOLD_CODE_001";
13
    public static final String SCAFFOLD_CODE_002 = "SCAFFOLD_CODE_002";
14
    public static final String SCAFFOLD_CODE_003 = "SCAFFOLD_CODE_003";
15
    public static final String SCAFFOLD_CODE_004 = "SCAFFOLD_CODE_004";
16
    public static final String SCAFFOLD_CODE_005 = "SCAFFOLD_CODE_005";
17
    public static final String SCAFFOLD_CODE_006 = "SCAFFOLD_CODE_006";
18
    public static final String SCAFFOLD_CODE_007 = "SCAFFOLD_CODE_007";
19 19
}

+ 7 - 7
ipu-rest-scaffold/src/main/resources/exception_messages_zh_CN.properties

@ -1,7 +1,7 @@
1
SERVER_001=条件参数不能为空
2
SERVER_002=[%v]参数异常
3
SERVER_003=业务办理失败:主键冲突
4
SERVER_004=查询失败
5
SERVER_005=插入失败
6
SERVER_006=更改失败
7
SERVER_007=删除失败
1
SCAFFOLD_CODE_001=条件参数不能为空
2
SCAFFOLD_CODE_002=[%v]参数异常
3
SCAFFOLD_CODE_003=业务办理失败:主键冲突
4
SCAFFOLD_CODE_004=查询失败
5
SCAFFOLD_CODE_005=插入失败
6
SCAFFOLD_CODE_006=更改失败
7
SCAFFOLD_CODE_007=删除失败