JEECG新建用户不用系统用户表的实现
首先新增组织机构 和 角色:
-- 新增 组织机构
INSERT INTO `t_s_depart` VALUES ('dept001', '你所在的机构', '你所在的机构的描述', null, 'A04', '1', '', '', '', null);
-- 新增 角色
INSERT INTO `t_s_role` VALUES ('role001', 'tz_admin', '台账管理员', null, null, null, '管理员', '2017-10-25 11:01:16', 'admin');
INSERT INTO `t_s_role` VALUES ('role002', 'tz_branch', '支行管理员', null, null, null, '管理员', '2017-10-25 11:01:33', 'admin');
INSERT INTO `t_s_role` VALUES ('role003', 'tz_am', '客户经理', null, null, null, '管理员', '2017-10-25 11:01:45', 'admin');
然后在代码中实现:
package com.zifeiy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.jeecgframework.core.util.PasswordUtil;
import org.jeecgframework.core.util.oConvertUtils;
/*
* 用于传输额外的信息到 系统用户表中
*/
public class AdditionalAddToDbHelper {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/jeecg?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";
private static String userName = "root";
private static String password = "password";
// private static Connection conn = null;
// private static Statement stmt = null;
public static boolean addMoreUserInfo(String fullname, String username, String passwd, String rightString) throws ClassNotFoundException, SQLException {
int right = 3;
try {
right = Integer.parseInt(rightString.trim());
} catch (Exception e) {
right = 3;
}
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, userName, password);
System.out.println("connect database successful!begin to add or update");
Statement stmt = conn.createStatement();
String[] roleNameList = new String[]{"台账管理员", "支行管理员", "客户经理"};
String[] roleIdList = new String[]{"role001", "role002", "role003"};
// String[] roleList = new String[]{"tz_admin", "tz_branch", "tz_am"};
String dept = "dept001";
passwd = oConvertUtils.getString(passwd);
String encryptedPasswd = PasswordUtil.encrypt(username, passwd, PasswordUtil.getStaticSalt());
String delete_sql1 = "DELETE FROM `t_s_role_user` WHERE ID='" + username + "'";
String delete_sql2 = "DELETE FROM `t_s_user` WHERE ID='" + username + "'";
String delete_sql3 = "DELETE FROM `t_s_base_user` WHERE ID='" + username + "'";
String delete_sql4 = "DELETE FROM `t_s_user_org` WHERE ID='" + username + "'";
String insert_sql3 = "INSERT INTO `t_s_role_user` VALUES ('" + username + "', '"
+ roleIdList[right-1] + "', '" + username + "');";
String insert_sql2 = "INSERT INTO `t_s_user` VALUES ('" + username
+ "', '', '', '', null, '管理员', '2017-10-25 11:08:52', 'admin', '管理员', '2017-10-25 11:08:20', 'admin', null, null, '1');";
String insert_sql1 = "INSERT INTO `t_s_base_user` VALUES ('" + username
+ "', null, null, '" + encryptedPasswd
+ "', '" + username
+ "', null, '1', '" + roleNameList[right-1] +"', '"
+ username + "', null, '0');";
String insert_sql4 = "INSERT INTO `t_s_user_org` VALUES ('" + username + "', '" + username + "', '" + dept + "');";
String[] sqlList = new String[]{delete_sql1, delete_sql2, delete_sql3, delete_sql4,
insert_sql1, insert_sql2, insert_sql3, insert_sql4};
for (int i = 0; i < 8; i ++) {
String sql = sqlList[i];
System.out.println("sql: " + sql);
stmt.addBatch(sql);
}
stmt.executeBatch();
stmt.close();
if (conn != null)
conn.close();
return false;
}
public static boolean doMoreUserDelete(String username) throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, userName, password);
System.out.println("connect database successful!begin to delete");
Statement stmt = conn.createStatement();
String delete_sql1 = "DELETE FROM `t_s_role_user` WHERE ID='" + username + "'";
String delete_sql2 = "DELETE FROM `t_s_user` WHERE ID='" + username + "'";
String delete_sql3 = "DELETE FROM `t_s_base_user` WHERE ID='" + username + "'";
String delete_sql4 = "DELETE FROM `t_s_user_org` WHERE ID='" + username + "'";
String[] sqlList = new String[]{delete_sql1, delete_sql2, delete_sql3, delete_sql4};
for (int i = 0; i < 4; i ++) {
String sql = sqlList[i];
System.out.println("sql: " + sql);
stmt.addBatch(sql);
}
stmt.executeBatch();
stmt.close();
if (conn != null)
conn.close();
return false;
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
}
}