2020-09-30 rbac权限设计初学

在这里插入图片描述
通过这个表知道怎样去设计数据库
需要三个实体类,用户,角色,节点
还有两个中间数据库,权限,用户与角色的中间表
这是我数据库的设计
在这里插入图片描述
一条重要的重要的查询语句
老师的建议是多使用一些子查询和内连接查询

String sql="SELECT * from gnode where id in(SELECT nid FROM gnr where rid in(SELECT rid FROM ggr where gid=?) )";

代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="https://cdn.jsdelivr.net/npm/vue"></script>
		<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
		<title></title>
	</head>
	<body>
		<div class="box1">
			账号:<input type="text" name="username"  v-model="zh"/><br />
			密码:<input type="password" name="password"   v-model="ma"/><br>
			<button type="button" class="mui-btn mui-btn-blue mui-btn-block" @click="dd">登录</button>
		</div>
		<script>
			var app=new Vue({
				el:".box1",
				data:{
					zh:"",
					ma:""
				},
				methods:{
					dd:function(){
						axios.get("logins?username="+this.zh+"&password="+this.ma)
						.then(function(response){
							console.dir(response.data[0].id)
							window.localStorage.setItem("id",response.data[0].id);
							alert(response.data[0].id)
							window.location.href="index"
						},function(err){
							alert(err.data+"错误")
						})
					}
				}
			})
		</script>		
		
	</body>
</html>

<!DOCTYPE html>
<html xmlns:v-bind="http://www.w3.org/1999/xhtml">
	<script src="https://cdn.jsdelivr.net/npm/vue"></script>
	<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
				
			* {
				margin: 0px;
			}
			.box21 {
				width: 1600px;
			}
			
			.box1 {
				width: 1600px;
				height: 100px;
				border: 1px solid wheat;
			}
			.box2 {
				/* display: flex; */
				float: left;
				height: 600px;
				width: 200px;
				border: 1px solid wheat;
			}
			.box22 {
			
				float: left;
				width: 1250px;
				height: 600px;
				border: 1px solid wheat;
				background-color: antiquewhite;
			}
			li {
				list-style: none;
				height: 30px;

			}
			a {
				list-style: none;
			}
		</style>
	</head>
	<body>
		<div class="box0">
			<div class="box1">
				
			</div>
			<div class="box21">
				<div class="box2">
					<ul>
						<li v-for="a in node"><a :href="a.url" target="f1">{{a.ntext}}</a></li>
					</ul>
				</div>
				<div class="box22">
					<iframe id="f1" name="f1" src="wecome" style="width: 1220px; height: 100%;">

					</iframe>
				</div>
			</div>
		</div>
		<script>
			var app1=new Vue({
				el:".box0",
				data:{
					node:[]
				},
				created:function(){
					 var uid=window.localStorage.getItem("id")
					axios.get("index01?id="+uid)
					.then(function(response){
						console.dir(response.data)
						app1.node=response.data;

					},function(err){
						alert("错误")
					})
				}
				
			})
		</script>
	</body>
</html>

package com.dz.mvvm.control;

import com.alibaba.fastjson.JSON;
import com.dz.mvvm.service.LoginService;
import com.dz.mvvm.service.RBAC;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class loginControl {
    @RequestMapping(value = "index01")
    @ResponseBody
    public String login00(String id){
        RBAC r1=new RBAC();
        List list= r1.rbac(id);
        String str= JSON.toJSONString(list);
        System.out.println(str.trim());
        return str;
    }

    @RequestMapping(value = "login")
    public String login01(){

        return "login";
    }
    @RequestMapping(value = "wecome")
    public String login03(){

        return "wecome";
    }
    @RequestMapping(value = "xuanchuan")
    public String login06(){

        return "xuanchuan";
    }
    @RequestMapping(value = "xuanchuanfanghsi")
    public String login05(){

        return "xuanchuanfanghsi";
    }
    @RequestMapping(value = "bianji")
    public String login04(){

        return "bianji";
    }
    @RequestMapping(value = "index")
    public String login02(){

        return "index";
    }

    @RequestMapping("/logins")
    @ResponseBody
    public List login(HttpServletRequest request){
        String  username=request.getParameter("username");
        String password=request.getParameter("password");
        LoginService l1=new LoginService();
        List getall = l1.islogin(username,password);
        String str= JSON.toJSON(getall).toString();
        System.out.println(str);
        if(getall.size()==0){
            request.getSession().setAttribute("msg","登录失败");
            System.out.println("错误");
            return null;
        }else {
            request.getSession().setAttribute("msg","登录成功");
            System.out.println("---"+getall);
            return getall;
        }
    }

}

DbUtils数据库的封装

package com.dz.mvvm.dao;

import java.sql.Connection;


import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;


public class DH {
	
	public static final String connstr="com.mysql.cj.jdbc.Driver";
	public static final String dr="jdbc:mysql://localhost:3306/ssmshujuku?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false";
	public static final String uid="root";
	public static final String pwd="123";

	public static List getall(String sql,Object o,String[] args){
		List li=new ArrayList();
		Connection conn=null;
		DbUtils.loadDriver(DH.connstr);
		try {
			conn=DriverManager.getConnection(DH.dr, DH.uid,DH.pwd);
			QueryRunner qr=new QueryRunner();
			li=(List)qr.query(conn, sql,args, new BeanListHandler(o.getClass()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			DbUtils.closeQuietly(conn);
			
		}
		
		
		return li;
	}
	
	public static int update(String sql,String[] args){
		int num=0;
		Connection conn=null;
		DbUtils.loadDriver(DH.connstr);
		try {
			conn=DriverManager.getConnection(DH.dr, DH.uid,DH.pwd);
			QueryRunner qr=new QueryRunner();
			num=qr.update(conn, sql, args);
			//System.out.println(num);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			DbUtils.closeQuietly(conn);
			
		}
		return num;
	}
	public static int del(String sql,Integer[] args){
	    int num=0;
	    Connection conn=null;
	    DbUtils.loadDriver(DH.connstr);
	    try {
	        conn=DriverManager.getConnection(DH.dr, DH.uid,DH.pwd);
	        QueryRunner qr=new QueryRunner();
	        num=qr.update(conn, sql, args);
	        //System.out.println(num);
	    } catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }finally{
	        
	        DbUtils.closeQuietly(conn);
	        
	    }
	    return num;
	}
	
	public static Connection getconn(){
		Connection conn=null;
		try {
			Class.forName(DH.connstr);
			conn=DriverManager.getConnection(DH.dr, DH.uid,DH.pwd);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void closeall(Statement st,Connection conn){
		try {
			st.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void closeall(ResultSet rs,Statement st,Connection conn){
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			st.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}

package com.dz.mvvm.service;

import com.dz.mvvm.dao.DH;
import com.dz.mvvm.pojo.gnode;

import java.util.List;

public class RBAC {
    public List rbac(String gid){
        String sql="SELECT * from gnode where id in(SELECT nid FROM gnr where rid in(SELECT rid FROM ggr where gid=?) )";
        List getall =  DH.getall(sql, new gnode(), new String[]{gid});
        return getall;
    }

}

pom依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值