SpringBootMybatis02 mybatis-generator-gui|pageHelper|前后端分离|Filter权限实现

一、Mybatis-generator-gui

下载地址:https://github.com/LittlePageProgram/mybatis-generator-gui.git

使用方法:填写相关项,点击生成

注意项:

1.有关EntityExample,需要拖进mapper层

2.有关修改XML配置信息,相应的example也需要进行调整到mapper层

案例:

service层

package com.littlepage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.littlepage.entity.User;
import com.littlepage.mapper.UserExample;
import com.littlepage.mapper.UserMapper;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;
    
    public List<User> selectAll() {
        
        UserExample example=new UserExample();
        example.createCriteria();
        
        return userMapper.selectByExample(example);
        
    }
    
    public void add() {
        UserExample example=new UserExample();
        userMapper.deleteByExample(example);
    }
    
    public boolean selectByLoginNameAndPassword(String name,String password) {
        UserExample example=new UserExample();
        System.out.println(name);
        System.out.println(password);
        example.createCriteria().andNameEqualTo(name).andPasswordEqualTo(password);
        List<User> li=userMapper.selectByExample(example);
        System.out.println(li);
        return li.size()!=0 ;
    }
}

二、PageHelper 一个好用的分页插件

public List<User> selectAll(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UserExample example=new UserExample();
        example.createCriteria();
        
        return userMapper.selectByExample(example);
        
    }

快速传值分页,这个原理运用了AOP编程

三、前后端分离

理论:

后端仅仅提供接口,前端进行获取数据和进行跳转。

优点:

1、对服务器的压力减小到最小

2、后台错误不会直接反映到前台,错误接秒较为友好

3、前后台各尽其职可以最大程度的减少开发难度。

实现:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <link rel="stylesheet" href="/css/bootstrap.min.css" />
    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script>
        $(function (){
            $("#login_btn").click(function (){
                var loginName=$('#loginName').val();
                var password=$('#password').val();
                if(loginName==''||password==''){
                    $('#tip').html('用户名密码不能为空');
                    $('#tip').css('color','red');
                    return false;
                }else{
                    var url='/account/validateAccount';
                    var args={loginName:loginName,password:password};
                    
                    $.post(url,args,function(data){
                        if(data=='success'){
                            window.location.href='/account/success';
                        }else{
                            $('#tip').html('密码错误');
                            $('#tip').css('color','red');
                        }
                        console.log(data);
                    })
                    return false;
                }
            })
        })
    </script>
    <body>
        <div align="middle">
            <br />
            <br />
            <hr/>
            <br />
            <div align="middle">
                <h3>登录界面</h3>
            </div>
            <br />
            <br />
            <form action="/account/validateAccount" method="post">
                <input type="text" placeholder="用户名" id="loginName" /><br /><br />
                <input type="password" placeholder="密码" id="password" /><br /><br />
                <div><span id="tip"></span></div><br />
                <button id="login_btn" type="submit" >登录</button>
            </form>
        </div>
    </body>
</html>

HTML页面

package com.littlepage.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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.littlepage.entity.User;
import com.littlepage.service.UserService;

@Controller
@RequestMapping("/account")
public class AccountController {
    
    @Autowired
    UserService userService;
    
    @RequestMapping("/login")
    public String loginPage() {
        return "login";
    }
    
    @RequestMapping("/validateAccount")
    @ResponseBody
    public String list(@RequestParam("loginName")String loginName,@RequestParam("password")String password) {
        if(userService.selectByLoginNameAndPassword(loginName, password)) {
            return "success";
        }else {
            return "false";
        }
        
    }
    
    @RequestMapping("/success")
    public String successPage() {
        return "account/success";
    }
    
    @RequestMapping("/list")
    @ResponseBody
    public List<User> list() {
        return userService.selectAll(2, 2);
    }
}

控制器

四、权限实现

package com.littlepage.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

@WebFilter(urlPatterns = "/*")
@Component
public class AccountFilter implements Filter {
    
    private final String[] IGNORE_URI= {"index","css","js"};

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpReq=(HttpServletRequest)request;
        HttpServletResponse httpRes=(HttpServletResponse)response;
        
        //1.从session找Account对象
        //找到就全部放行
        //找不到就执行2
        
        
        //1.判断URI是不是在Ignore列表里,在就放行
        String uri=httpReq.getRequestURI();
        
        for (String string : IGNORE_URI) {
            if(uri.contains(string)) {
                System.out.println("contains");
                chain.doFilter(request, response);
            }
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("启动权限filter");
    }
}

 

posted @ 2019-07-08 04:13  SteveYu  阅读(362)  评论(0编辑  收藏  举报