ssm demo,用户角色权限管理

SSM框架整合

Spring

SpringMVC

MyBatis

导包:
1, spring
2, MyBatis
3, mybatis-spring
4, fastjson
5, aspectweaver----AspectJ框架
6, log4j-----打印日志信息
7, ojdbc6.jar
8, jstl.jar, standard.jar----标准标签库
9, commons-logging-1.2.jar

src下建立包结构

配置web.xml,spring-mvc.xml,spring-all.xml

建立表结构,使用PowerDesigner

前台页面,后台方法等。

ssm demo,用户角色权限管理

ssm demo,用户角色权限管理

项目结构:

ssm demo,用户角色权限管理

ssm demo,用户角色权限管理

代码(引用,未完全实现):

配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <!-- 告诉web容器log4j的属性文件所在位置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> <!-- spring框架提供的字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 加载Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 加载SpringMVC的配置文件 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0"> <!-- 告诉web容器log4j的属性文件所在位置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> <!-- spring框架提供的字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 加载Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 加载SpringMVC的配置文件 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 配置扫描器 -->
<context:component-scan base-package="com.hanqi.controller" /> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- <property name="prefix" value="/WEB-INF/"></property> -->
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 上传文件的解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件的总大小, 单位是b -->
<property name="maxUploadSize" value="10240000"></property>
<!-- 单个文件的上传大小限制, 单位是b -->
<property name="maxUploadSizePerFile" value="1000000"></property>
<!-- 默认字符集 -->
<property name="defaultEncoding" value="utf-8"></property>
</bean> <!-- 开启SpringMVC注解驱动 -->
<mvc:annotation-driven>
<!-- 改成false, 原因是在spring中默认使用的json格式的转换器是Jackson.jar中的转换器, 但实际开发中更受欢迎的是fastjson.jar -->
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" />
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean id="fastJsonHttpMessagerConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 设置支持的媒体类型 -->
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=utf-8</value>
<value>application/json; charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>

src下:

model层实体类:

 package com.hanqi.model;

 public class SrUserRole {
private Integer id;
private Integer userid;
private Integer roleid; public SrUserRole() {
super();
} public SrUserRole(Integer id, Integer userid, Integer roleid) {
super();
this.id = id;
this.userid = userid;
this.roleid = roleid;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getUserid() {
return userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public Integer getRoleid() {
return roleid;
} public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
}
 package com.hanqi.model;

 public class SrRoleMenu {
private Integer id;
private Integer roleid;
private Integer menuid; public SrRoleMenu(Integer id, Integer roleid, Integer menuid) {
super();
this.id = id;
this.roleid = roleid;
this.menuid = menuid;
} public SrRoleMenu() {
super();
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getRoleid() {
return roleid;
} public void setRoleid(Integer roleid) {
this.roleid = roleid;
} public Integer getMenuid() {
return menuid;
} public void setMenuid(Integer menuid) {
this.menuid = menuid;
}
}
 package com.hanqi.model;

 public class SrRole {
private Integer id;
private String rolename;
private String status;
private boolean checked; public SrRole() {
super();
} public SrRole(Integer id, String rolename, String status) {
super();
this.id = id;
this.rolename = rolename;
this.status = status;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getRolename() {
return rolename;
} public void setRolename(String rolename) {
this.rolename = rolename;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status;
} public boolean getChecked() {
return checked;
} public void setChecked(boolean checked) {
this.checked = checked;
}
}
 package com.hanqi.model;

 public class SrMenu {
private Integer id;
private String menuname;
private String url;
private String icon;
private String status;
private Integer parentid; public SrMenu() {
super();
} public SrMenu(Integer id, String menuname, String url, String icon, String status) {
super();
this.id = id;
this.menuname = menuname;
this.url = url;
this.icon = icon;
this.status = status;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getMenuname() {
return menuname;
} public void setMenuname(String menuname) {
this.menuname = menuname;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getIcon() {
return icon;
} public void setIcon(String icon) {
this.icon = icon;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status;
} public Integer getParentid() {
return parentid;
} public void setParentid(Integer parentid) {
this.parentid = parentid;
}
}

dao层接口:

 package com.hanqi.dao;

 import java.util.List;

 import com.hanqi.model.SrUser;

 public interface SrUserDao {

     SrUser login(SrUser srUser);

     int insertSrUser(SrUser srUser);

     List<SrUser> selectAll();

 }
 package com.hanqi.dao;

 import java.util.List;

 import com.hanqi.model.SrRole;

 public interface SrRoleDao {

     List<SrRole> selectAllRoles(String userid);

     List<SrRole> selectAll();

 }
 package com.hanqi.dao;

 import java.util.List;

 import com.hanqi.model.SrMenu;
import com.hanqi.model.SrUser; public interface SrMenuDao { List<SrMenu> selectMenus(SrUser currentUser); }

dao层实现方法:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanqi.dao.SrUserDao"> <select id="login" parameterType="SrUser" resultType="SrUser">
select * from Sr_User s
<where>
s.username=#{username} and s.password=#{password}
</where>
</select> <select id="selectAll" resultType="SrUser">
select * from sr_user
</select> <insert id="insertSrUser" parameterType="SrUser">
insert into sr_user values
(testsq.nextval, #{username}, #{password}, #{realname}, #{status}, null)
</insert>
</mapper>
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanqi.dao.SrRoleDao"> <select id="selectAllRoles" resultType="SrRole">
SELECT * FROM sr_role sr WHERE sr.id IN
(SELECT sur.roleid FROM sr_user_role sur WHERE sur.userid=#{userid})
</select> <select id="selectAll" resultType="SrRole">
select * from sr_role
</select>
</mapper>
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanqi.dao.SrMenuDao"> <select id="selectMenus" parameterType="SrUser" resultType="SrMenu">
SELECT * FROM SR_MENU SM
WHERE SM.ID IN (SELECT SRM.MENUID
FROM SR_ROLE_MENU SRM
WHERE SRM.ROLEID = (SELECT SUR.ROLEID
FROM SR_USER_ROLE SUR
WHERE SUR.USERID = #{id}))
</select> </mapper>

controller层控制器:

 package com.hanqi.controller;

 import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes; import com.alibaba.fastjson.JSONObject;
import com.hanqi.dao.SrUserDao;
import com.hanqi.model.SrUser; @Controller
@SessionAttributes("currentUser")
@RequestMapping("sruser")
public class SrUserController { @Autowired
private SrUserDao srUserDao; @RequestMapping("/login")
public String login(SrUser srUser, Model model) {
String vname = "login";
model.addAttribute("user", srUser);
SrUser su = srUserDao.login(srUser); if(su!=null) {
vname = "index";
model.addAttribute("currentUser", su);
} return vname;
} @RequestMapping("/register")
public String register(SrUser srUser) {
srUser.setStatus("1");
int a = srUserDao.insertSrUser(srUser); return "regsuccess";
} @ResponseBody
@RequestMapping("selectAll")
public JSONObject selectAll() {
JSONObject jo = new JSONObject();
List<SrUser> list = srUserDao.selectAll();
jo.put("total", list.size());
jo.put("rows", list); return jo;
} }
package com.hanqi.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.hanqi.dao.SrRoleDao;
import com.hanqi.model.SrRole; @Controller
@RequestMapping("/role")
public class SrRoleController { @Autowired
private SrRoleDao srRoleDao; @ResponseBody
@RequestMapping("/selectAllRoles")
public List<SrRole> selectAllRoles(String userid) { // 查询当前用户拥有的角色
List<SrRole> list = srRoleDao.selectAllRoles(userid);
// 查询所有的角色
List<SrRole> listAll = srRoleDao.selectAll(); for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < listAll.size(); j++) {
if (listAll.get(j).getId() == list.get(i).getId()) {
listAll.get(j).setChecked(true);
}
}
}
return listAll;
}
}
 package com.hanqi.controller;

 import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.hanqi.dao.SrMenuDao;
import com.hanqi.model.SrMenu;
import com.hanqi.model.SrUser; @Controller
@RequestMapping("/menu")
public class SrMenuController { @Autowired
private SrMenuDao srMenuDao; @ResponseBody
@RequestMapping("/selectMenus")
public List<SrMenu> selectMenus(HttpServletRequest request) { SrUser currentUser = (SrUser)request.getSession().getAttribute("currentUser"); List<SrMenu> list = new ArrayList<SrMenu>();
if(currentUser!=null) {
list = srMenuDao.selectMenus(currentUser);
} return list;
} }

前台jsp页面:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
String basePath = request.getContextPath();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="<%=basePath %>/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="<%=basePath %>/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<link type="text/css" rel="stylesheet" href="<%=basePath %>/jquery-easyui-1.5.1/themes/icon.css"></link>
<link type="text/css" rel="stylesheet" href="<%=basePath %>/jquery-easyui-1.5.1/themes/default/easyui.css"></link>
<script type="text/javascript" src="<%=basePath %>/jquery-easyui-1.5.1/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="<%=basePath %>/js/index.js"></script>
<script type="text/javascript">
var __ctx = "<%=basePath %>";
</script>
<title>Insert title here</title>
</head>
<body class="easyui-layout">
<div data-options="region:'north',title:'North Title',split:true,height:100">
<h1>${currentUser.realname }</h1>
</div>
<div data-options="region:'west',title:'West',split:true,width:150">
<ul id="treemenu" class="easyui-tree"
data-options="url:'<%=basePath %>/menu/selectMenus.do',idField:'id',
parentField:'parentid',textField:'menuname'"></ul>
</div>
<div data-options="region:'center'">
<div id="tabs_userrole" class="easyui-tabs" data-options="fit:true">
</div>
</div>
</body>
</html>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
String basePath = request.getContextPath();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript"
src="<%=basePath%>/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript"
src="<%=basePath%>/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<link type="text/css" rel="stylesheet"
href="<%=basePath%>/jquery-easyui-1.5.1/themes/icon.css"></link>
<link type="text/css" rel="stylesheet"
href="<%=basePath%>/jquery-easyui-1.5.1/themes/default/easyui.css"></link>
<script type="text/javascript"
src="<%=basePath%>/jquery-easyui-1.5.1/locale/easyui-lang-zh_CN.js"></script>
<title>Insert title here</title>
</head>
<body>
<table id="userinfo" class="easyui-datagrid"
data-options="toolbar:'#tools',url:'<%=basePath%>/sruser/selectAll.do',fit:true,fitColumns:true,singleSelect:true">
<thead>
<tr>
<th data-options="field:'',width:100,checkbox:true"></th>
<th data-options="field:'id',width:100">id</th>
<th data-options="field:'username',width:100">用户名</th>
<th data-options="field:'realname',width:100">姓名</th>
<th data-options="field:'status',width:100">状态</th>
<th data-options="field:'mark',width:100">备注</th>
</tr>
</thead>
</table>
<div id="tools">
<a class="easyui-linkbutton" iconCls="icon-add"
onclick="showRoleInfos()">修改角色</a>
</div> <div id="dialog_roles" class="easyui-dialog"
data-options="closed:true,width:200,height:300,title:'更改角色',modal:true">
<ul id="role_tree"></ul>
</div> <script type="text/javascript">
function showRoleInfos() {
var data = $("#userinfo").datagrid("getSelected");
$("#role_tree").tree({
url:"role/selectAllRoles.do",
checkbox:true,
idField:"id",
parentField:"id",
textField:"rolename",
queryParams:{
userid:data.id
},
onCheck:function(node, checked) {
console.log(node);
console.log(checked);
}
});
$("#dialog_roles").dialog("open");
} //easyui实现自定义simpleData加载
$.fn.tree.defaults.loadFilter = function (data) {
var opt = $(this).data().tree.options;//整个tree的dom对象
var idField, textField, parentField;
if (opt.parentField) {
idField = opt.idField || 'id';
textField = opt.textField || 'text';
parentField = opt.parentField;
var i,l,
treeData = [],
tmpMap = [];
for (i = 0, l = data.length; i < l; i++) {
tmpMap[data[i][idField]] = data[i];
}
for (i = 0, l = data.length; i < l; i++) {
if (tmpMap[data[i][parentField]] && data[i][idField] != data[i][parentField]) {
if (!tmpMap[data[i][parentField]]['children'])
tmpMap[data[i][parentField]]['children'] = [];
data[i]['text'] = data[i][textField];
tmpMap[data[i][parentField]]['children'].push(data[i]);
} else {
data[i]['text'] = data[i][textField];
treeData.push(data[i]);
}
}
return treeData;
}
return data;
};
</script> </body>
</html>
 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="sruser/login.do" method="post">
<table>
<tr>
<td>用户名: </td>
<td><input name="username" value="${user.username }" /></td>
</tr>
<tr>
<td>密码: </td>
<td><input name="password" value="${user.password }" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录" /></td>
</tr>
</table>
</form>
</body>
</html>
 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="sruser/register.do" method="post">
<table>
<tr>
<td>用户名: </td>
<td><input name="username" /></td>
</tr>
<tr>
<td>密码: </td>
<td><input name="password" /></td>
</tr>
<tr>
<td>确认密码: </td>
<td><input name="password1" /></td>
</tr>
<tr>
<td>姓名: </td>
<td><input name="realname" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="注册" /></td>
</tr>
</table>
</form>
</body>
</html>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
String basePath = request.getContextPath();
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
注册成功 !
<a href="<%=basePath %>/login.jsp">返回登录</a>
</body>
</html>

我写的不完全版……补全之后再贴上来

上一篇:python pip install 报错TypeError: unsupported operand type(s) for -=: 'Retry' and 'int' Command "python setup.py egg_info" failed with error code 1 in


下一篇:FP-growth算法发现频繁项集(二)——发现频繁项集