| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import java.sql.*;
- public class InsertMenuForBusinessScene {
- public static void main(String[] args) {
- String url = "jdbc:dm://192.168.0.145:30236?charset=utf8";
- String username = "WATERSHED";
- String password = "WaterShed./1224";
-
- Connection conn = null;
-
- try {
- Class.forName("dm.jdbc.driver.DmDriver");
- conn = DriverManager.getConnection(url, username, password);
-
- // 1. 添加业务场景管理子菜单
- String menuSql = "INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, query, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark) " +
- "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate, ?, ?, ?)";
-
- // 业务场景管理菜单
- insertMenu(conn, menuSql, 2027, "业务场景", 1, 10, "businessScene", "system/businessScene/index", "", 1, 0, "C", "0", "0", "system:businessScene:list", "scene", "admin", "", null, "业务场景管理菜单");
-
- // 按钮权限
- insertMenu(conn, menuSql, 2028, "业务场景查询", 2027, 1, "", "", "", 1, 0, "F", "0", "0", "system:businessScene:query", "#", "admin", "", null, "");
- insertMenu(conn, menuSql, 2029, "业务场景新增", 2027, 2, "", "", "", 1, 0, "F", "0", "0", "system:businessScene:add", "#", "admin", "", null, "");
- insertMenu(conn, menuSql, 2030, "业务场景修改", 2027, 3, "", "", "", 1, 0, "F", "0", "0", "system:businessScene:edit", "#", "admin", "", null, "");
- insertMenu(conn, menuSql, 2031, "业务场景删除", 2027, 4, "", "", "", 1, 0, "F", "0", "0", "system:businessScene:remove", "#", "admin", "", null, "");
- insertMenu(conn, menuSql, 2032, "业务场景导出", 2027, 5, "", "", "", 1, 0, "F", "0", "0", "system:businessScene:export", "#", "admin", "", null, "");
-
- // 2. 给管理员角色分配权限
- String roleMenuSql = "INSERT INTO sys_role_menu (role_id, menu_id) VALUES (?, ?)";
- int[] menuIds = {2027, 2028, 2029, 2030, 2031, 2032};
- for (int menuId : menuIds) {
- insertRoleMenu(conn, roleMenuSql, 1, menuId);
- }
-
- System.out.println("\n业务场景管理菜单配置完成!");
-
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (conn != null) conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
-
- private static void insertMenu(Connection conn, String sql, int menuId, String menuName, int parentId, int orderNum,
- String path, String component, String query, int isFrame, int isCache,
- String menuType, String visible, String status, String perms, String icon,
- String createBy, String updateBy, String updateTime, String remark) {
- try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
- pstmt.setInt(1, menuId);
- pstmt.setString(2, menuName);
- pstmt.setInt(3, parentId);
- pstmt.setInt(4, orderNum);
- pstmt.setString(5, path);
- pstmt.setString(6, component);
- pstmt.setString(7, query);
- pstmt.setInt(8, isFrame);
- pstmt.setInt(9, isCache);
- pstmt.setString(10, menuType);
- pstmt.setString(11, visible);
- pstmt.setString(12, status);
- pstmt.setString(13, perms);
- pstmt.setString(14, icon);
- pstmt.setString(15, createBy);
- pstmt.setString(16, updateBy);
- pstmt.setString(17, updateTime);
- pstmt.setString(18, remark);
-
- pstmt.executeUpdate();
- System.out.println("菜单[" + menuName + "] - 成功");
- } catch (SQLException e) {
- if (e.getErrorCode() == -2626) {
- System.out.println("菜单[" + menuName + "] - 已存在,跳过");
- } else {
- System.out.println("菜单[" + menuName + "] - 失败: " + e.getMessage());
- }
- }
- }
-
- private static void insertRoleMenu(Connection conn, String sql, int roleId, int menuId) {
- try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
- pstmt.setInt(1, roleId);
- pstmt.setInt(2, menuId);
- pstmt.executeUpdate();
- System.out.println("角色权限(role=" + roleId + ", menu=" + menuId + ") - 成功");
- } catch (SQLException e) {
- if (e.getErrorCode() == -2626) {
- System.out.println("角色权限(role=" + roleId + ", menu=" + menuId + ") - 已存在,跳过");
- } else {
- System.out.println("角色权限(role=" + roleId + ", menu=" + menuId + ") - 失败: " + e.getMessage());
- }
- }
- }
- }
|