简易的echarts+java可视化展示平台

目录

entity层

User

People

 dao层

UserDao

 PeopleDao

 service层

UserService

 PeopleService

 service层实现类

UserServiceImpl

 PeopleServiceImpl

 dto层

requestDto

LoginRequestDto

 PasswordRequestDto

 RegistRequestDto

responseDto

LoginResponseDto

 controller层

BaseController

 EchartsController

LoginController

PeopleController

 RegistController

UserController

interceptor登陆拦截器

LoginInterceptor

config配置类

SwaggerConfig

util工具类

CodeUtil

Md5Util  

mybatis映射文件

PeopleDao.xml

UserDao.xml

 spring配置文件

application.xml

 数据库配置文件

jdbc.properties

sql 


 源码下载地址:

https://download.youkuaiyun.com/download/q1425857916/85663959https://download.youkuaiyun.com/download/q1425857916/85663959

GitHub - jingningbl/echartsContribute to jingningbl/echarts development by creating an account on GitHub.https://github.com/jingningbl/echarts

 

 

 

entity层

User

package com.echarts.entity;

import java.io.Serializable;

/**
 * (User)实体类
 *
 * @author makejava
 * @since 2022-05-23 18:31:05
 */
public class User implements Serializable {
    private static final long serialVersionUID = 627029103871267959L;
    
    private Integer id;
    
    private String username;
    
    private String password;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

People

package com.echarts.entity;

import java.io.Serializable;

/**
 * (People)实体类
 *
 * @author makejava
 * @since 2022-06-15 16:21:56
 */
public class People implements Serializable {
    private static final long serialVersionUID = -60863944794822282L;
    
    private Integer id;
    
    private String province;
    
    private Integer number;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

}

 dao层

UserDao

package com.echarts.dao;

import com.echarts.dto.responseDto.LoginResponseDto;
import com.echarts.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;

/**
 * (User)表数据库访问层
 *
 * @author makejava
 * @since 2022-05-23 18:31:06
 */
public interface UserDao {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    User queryById(Integer id);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<User> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param user 实例对象
     * @return 对象列表
     */
    List<User> queryAll(User user);

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 影响行数
     */
    int insert(User user);

    /**
     * 批量新增数据(MyBatis原生foreach方法)
     *
     * @param entities List<User> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<User> entities);

    /**
     * 批量新增或按主键更新数据(MyBatis原生foreach方法)
     *
     * @param entities List<User> 实例对象列表
     * @return 影响行数
     */
    int insertOrUpdateBatch(@Param("entities") List<User> entities);

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 影响行数
     */
    int update(User user);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Integer id);

    LoginResponseDto getUserInfoByUsernameAndPassword(@Param("username") String username,
                                                      @Param("password") String password);

    User queryByUsername(String username);

    void updatePassword(@Param("username") String userName,@Param("password") String newPassword);
}

 PeopleDao

package com.echarts.dao;

import com.echarts.entity.People;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * (People)表数据库访问层
 *
 * @author makejava
 * @since 2022-06-15 16:23:35
 */
public interface PeopleDao {

    People queryById(Integer id);

    List<People> queryAll();

    List<People> queryAllBySort();

    int update(@Param("province") String province, @Param("number") Integer number);

    Double queryNumber(@Param("province") String province);

}

 service层

UserService

package com.echarts.service;

import com.echarts.dto.responseDto.LoginResponseDto;
import com.echarts.entity.User;
import java.util.List;

/**
 * (User)表服务接口
 *
 * @author makejava
 * @since 2022-05-23 18:31:07
 */
public interface UserService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    User queryById(Integer id);

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<User> queryAllByLimit(int offset, int limit);

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    User insert(User user);

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    User update(User user);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    boolean deleteById(Integer id);

    LoginResponseDto getUserInfoByUsernameAndPassword(String username, String password);

    User queryByUsername(String username);

    void updatePassword(String userName, String newPassword);
}

 PeopleService

package com.echarts.service;

import com.echarts.entity.People;
import io.swagger.models.auth.In;

import java.util.List;

/**
 * (People)表服务接口
 *
 * @author makejava
 * @since 2022-06-15 16:23:43
 */
public interface PeopleService {

    People queryById(Integer id);

    List<People> queryAll();

    List<People> queryAllBySort();

    Integer update(String province,Integer number);

    Double queryNumber(String province);
}

 service层实现类

UserServiceImpl

package com.echarts.service.impl;

import com.echarts.dto.responseDto.LoginResponseDto;
import com.echarts.entity.User;
import com.echarts.dao.UserDao;
import com.echarts.service.UserService;
import com.echarts.util.Md5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2022-05-23 18:31:07
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userDao.deleteById(id) > 0;
    }

    @Override
    public LoginResponseDto getUserInfoByUsernameAndPassword(String username, String password) {
        return userDao.getUserInfoByUsernameAndPassword(username, Md5Util.getMd5(password));
    }

    @Override
    public User queryByUsername(String username) {
        return userDao.queryByUsername(username);
    }

    @Override
    public void updatePassword(String userName, String newPassword) {
        userDao.updatePassword(userName,Md5Util.getMd5(newPassword));
    }
}

 PeopleServiceImpl

package com.echarts.service.impl;

import com.echarts.entity.People;
import com.echarts.dao.PeopleDao;
import com.echarts.service.PeopleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (People)表服务实现类
 *
 * @author makejava
 * @since 2022-06-15 16:23:43
 */
@Service("peopleService")
public class PeopleServiceImpl implements PeopleService {
    @Resource
    private PeopleDao peopleDao;

    @Override
    public People queryById(Integer id) {
        return this.peopleDao.queryById(id);
    }

    @Override
    public List<People> queryAll() {
        return peopleDao.queryAll();
    }

    @Override
    public List<People> queryAllBySort() {
        return peopleDao.queryAllBySort();
    }

    @Override
    public Integer update(String province,Integer number) {
        return peopleDao.update(province,number);
    }

    @Override
    public Double queryNumber(String province) {
        return peopleDao.queryNumber(province);
    }
}

 dto层

requestDto

LoginRequestDto

package com.echarts.dto.requestDto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author: bai
 * @date: 2022/5/23 20:44
 * @description:
 */
@ApiModel(value = "登录",description = "用户登录dto")
public class LoginRequestDto implements Serializable {

    private static final long serialVersionUID = 2259675252596867416L;

    @ApiModelProperty(value = "用户名", required = true, example = "lisi")
    private String username;

    @ApiModelProperty(value = "密码", required = true, example = "1513")
    private String password;

    @ApiModelProperty(value = "验证码是否需要校验", required = false, example = "false")
    private Boolean needVerify;

    @ApiModelProperty(value = "验证码", required = false)
    private String verifyCodeActual;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean getNeedVerify() {
        return needVerify;
    }

    public void setNeedVerify(Boolean needVerify) {
        this.needVerify = needVerify;
    }

    public String getVerifyCodeActual() {
        return verifyCodeActual;
    }

    public void setVerifyCodeActual(String verifyCodeActual) {
        this.verifyCodeActual = verifyCodeActual;
    }

    @Override
    public String toString() {
        return "LoginRequestDto{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", needVerify=" + needVerify +
                ", verifyCodeActual='" + verifyCodeActual + '\'' +
                '}';
    }
}

 PasswordRequestDto

package com.echarts.dto.requestDto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author: bai
 * @date: 2022/5/23 20:44
 * @description:
 */
@ApiModel(value = "用户",description = "用户dto")

public class PasswordRequestDto implements Serializable {

    private static final long serialVersionUID = -5777813532773470194L;

    @ApiModelProperty(value = "旧密码", required = true, example = "1513")
    private String oldPassword;

    @ApiModelProperty(value = "新密码", required = true, example = "1513")
    private String newPassword;

    @ApiModelProperty(value = "确认密码", required = true, example = "1513")
    private String rePassword;

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getRePassword() {
        return rePassword;
    }

    public void setRePassword(String rePassword) {
        this.rePassword = rePassword;
    }
}

 RegistRequestDto

package com.echarts.dto.requestDto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author: bai
 * @date: 2022/5/23 20:44
 * @description:
 */
@ApiModel(value = "注册",description = "用户注册dto")
public class RegistRequestDto implements Serializable {

    private static final long serialVersionUID = 6124069851392868773L;

    @ApiModelProperty(value = "用户名", required = true, example = "lisi")
    private String username;

    @ApiModelProperty(value = "密码", required = true, example = "1513")
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "RegistRequestDto{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

responseDto

LoginResponseDto

package com.echarts.dto.responseDto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @author: bai
 * @date: 2022/5/23 20:57
 * @description:
 */
@ApiModel(value = "登录",description = "用户登录dto")
public class LoginResponseDto implements Serializable {

    private static final long serialVersionUID = -5205628512118749168L;

    @ApiModelProperty(value = "主键id")
    private Integer id;

    @ApiModelProperty(value = "姓名")
    private String userName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "LoginResponseDto{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                '}';
    }
}

 controller层

BaseController

package com.echarts.controller;

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

import javax.servlet.http.HttpServletRequest;

/**
 * @author: bai
 * @date: 2022/5/23 17:17
 * @description:
 */
@Controller
public class BaseController {

    @Autowired
    HttpServletRequest request;
}

 EchartsController

package com.echarts.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author: bai
 * @date: 2022/5/23 22:54
 * @description:
 */
@Api(tags = "Echarts路由")
@Controller
public class EchartsController extends BaseController{

    @ApiOperation(value = "echarts1")
    @GetMapping("echarts1")
    public String echarts1() {
        request.getSession().setAttribute("pageName", "Echarts示例");
        return "echarts1";
    }

    @ApiOperation(value = "echarts2")
    @GetMapping("echarts2")
    public String echarts2() {
        request.getSession().setAttribute("pageName", "Echarts树图");
        return "echarts2";
    }

    @ApiOperation(value = "echarts3")
    @GetMapping("echarts3")
    public String echarts3() {
        request.getSession().setAttribute("pageName", "人口普查");
        return "echarts3";
    }
}

LoginController

package com.echarts.controller;

import com.echarts.dto.requestDto.LoginRequestDto;
import com.echarts.dto.responseDto.LoginResponseDto;
import com.echarts.entity.User;
import com.echarts.service.UserService;
import com.echarts.util.CodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: bai
 * @date: 2022/5/23 17:11
 * @description:
 */
@Api(tags = "登陆登出")
@Controller
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "登录页面")
    @GetMapping("login")
    public String login() {
        return "login";
    }

    @ApiOperation(value = "跳转主页面")
    @GetMapping("main")
    public String toMain() {
        request.getSession().setAttribute("pageName", "Echarts官网");
        return "main";
    }

    @ApiOperation(value = "登录验证")
    @PostMapping("/checkLogin")
    @ResponseBody
    public Map<String, Object> checkLogin(@RequestBody LoginRequestDto loginRequestDto) {
        Map<String, Object> modelMap = new HashMap<>();
        //是否需要校验验证码
        Boolean needVerify = loginRequestDto.getNeedVerify();
        //获取验证码
        String verifyCodeActual = loginRequestDto.getVerifyCodeActual();
        //获取用户名
        String username = loginRequestDto.getUsername();
        //获取密码
        String password = loginRequestDto.getPassword();
        if (needVerify && !CodeUtil.checkVerifyCode(request, verifyCodeActual)) {
            //验证码错误
            modelMap.put("success", false);
            modelMap.put("errMsg", "验证码错误");
            return modelMap;
        }
        if ("".equals(username) || "".equals(password)) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "用户名或密码不可为空");
        } else {
            //通过用户名密码查库
            LoginResponseDto userInfo = userService.getUserInfoByUsernameAndPassword(username, password);
            if (null != userInfo) {
                //userInfo不为空,登陆成功
                modelMap.put("success", true);
                modelMap.put("username", userInfo.getUserName());
                //将信息存入到session
                request.getSession().setAttribute("userInfo", userInfo);
            } else {
                User user = userService.queryByUsername(username);
                modelMap.put("success", false);
                if (null != user){
                    modelMap.put("errMsg", "用户名或者密码错误");
                }else {
                    modelMap.put("errMsg", "用户不存在");
                }
            }
        }
        return modelMap;
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("logout")
    @ResponseBody
    public Map<String, Object> logout() {
        Map<String, Object> modelMap = new HashMap<>();
        request.getSession().setAttribute("userInfo", null);
        modelMap.put("success", true);
        return modelMap;
    }
}

PeopleController

package com.echarts.controller;

import com.echarts.entity.People;
import com.echarts.service.PeopleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (People)表控制层
 *
 * @author makejava
 * @since 2022-06-15 16:23:35
 */
@Api(tags = "人口普查")
@Controller
@RequestMapping("people")
public class PeopleController {
    /**
     * 服务对象
     */
    @Autowired
    private PeopleService peopleService;

    @ApiOperation("人口普查")
    @PostMapping("/chinaPeople")
    @ResponseBody
    public Map<String, List<String>> chinaPeople() {
        Map<String, List<String>> chinaMap = new HashMap<>();
        List<People> peopleList = peopleService.queryAll();
        List<String> province = new ArrayList<>();
        List<String> number = new ArrayList<>();
        List<String> id = new ArrayList<>();
        for (People people : peopleList) {
            province.add(people.getProvince());
            number.add(people.getNumber().toString());
            id.add(people.getId().toString());
        }
        chinaMap.put("province", province);
        chinaMap.put("number", number);
        chinaMap.put("id", id);
        return chinaMap;
    }

    @ApiOperation("数据修改")
    @PostMapping("/peopleList")
    @ResponseBody
    public Map<String, List<People>> peopleList() {
        Map<String, List<People>> chinaMap = new HashMap<>();
        List<People> peopleList = peopleService.queryAll();
        chinaMap.put("peopleList", peopleList);
        return chinaMap;
    }

    @ApiOperation("数据修改")
    @PostMapping("/updatePeople")
    @ResponseBody
    public void updatePeople(@RequestBody People people) {
        peopleService.update(people.getProvince(),people.getNumber());
    }

    @ApiOperation("人口普查")
    @PostMapping("/sortPeople")
    @ResponseBody
    public Map<String, List<String>> sortPeople() {
        Map<String, List<String>> chinaMap = new HashMap<>();
        List<People> peopleList = peopleService.queryAllBySort();
        List<String> province = new ArrayList<>();
        List<String> number = new ArrayList<>();
        List<String> id = new ArrayList<>();
        for (People people : peopleList) {
            province.add(people.getProvince());
            number.add(people.getNumber().toString());
            id.add(people.getId().toString());
        }
        chinaMap.put("province", province);
        chinaMap.put("number", number);
        chinaMap.put("id", id);
        return chinaMap;
    }
}

 RegistController

package com.echarts.controller;

import com.echarts.dto.requestDto.RegistRequestDto;
import com.echarts.dto.responseDto.LoginResponseDto;
import com.echarts.entity.User;
import com.echarts.service.UserService;
import com.echarts.util.Md5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: bai
 * @date: 2022/5/23 17:11
 * @description:
 */
@Api(tags = "注册")
@Controller
public class RegistController extends BaseController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "注册页面")
    @GetMapping("regist")
    public String login() {
        return "regist";
    }


    @ApiOperation(value = "注册验证")
    @PostMapping("/checkRegist")
    @ResponseBody
    public Map<String, Object> checkRegist(@RequestBody RegistRequestDto registRequestDto) {
        Map<String, Object> modelMap = new HashMap<>();
        //获取用户名
        String username = registRequestDto.getUsername();
        //获取密码
        String password = registRequestDto.getPassword();
        if ("".equals(username) || "".equals(password)) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "用户名或密码不可为空");
        } else {
            //通过用户名密码查库
            LoginResponseDto userInfo = userService.getUserInfoByUsernameAndPassword(username, password);
            if (null != userInfo) {
                modelMap.put("success", false);
                modelMap.put("errMsg", "用户已存在");
            } else {
                modelMap.put("success", true);
                User user = new User();
                user.setUsername(username);
                user.setPassword(Md5Util.getMd5(password));
                userService.insert(user);
            }
        }
        return modelMap;
    }
}

UserController

package com.echarts.controller;

import com.echarts.dto.requestDto.PasswordRequestDto;
import com.echarts.dto.responseDto.LoginResponseDto;
import com.echarts.entity.User;
import com.echarts.service.UserService;
import com.echarts.util.Md5Util;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-05-23 18:31:08
 */
@Controller
@RequestMapping("user")
public class UserController extends LoginController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    @ApiOperation(value = "用户信息")
    @GetMapping("userInfo")
    public String userInfo() {
        request.getSession().setAttribute("pageName", "个人信息");
        return "userInfo";
    }

    @ApiOperation(value = "修改密码")
    @GetMapping("userPassword")
    public String userPassword() {
        request.getSession().setAttribute("pageName", "修改密码");
        return "userPassword";
    }

    @PostMapping("queryUser")
    @ApiOperation(value = "查看指定用户信息")
    @ResponseBody
    public Map<String, Object> queryUserByUsername() {
        Map<String, Object> map = new HashMap<>();
        User user = null;
        try {
            LoginResponseDto userInfo = (LoginResponseDto) request.getSession().getAttribute("userInfo");
            String username = userInfo.getUserName();
            user = userService.queryByUsername(username);
        } catch (Exception e) {
            map.put("success", false);
            map.put("errMsg", e.getMessage());
            return map;
        }
        map.put("success", true);
        map.put("data", user);
        return map;
    }

    @PostMapping("editPassword")
    @ApiOperation(value = "修改指定用户信息")
    @ResponseBody
    public Map<String, Object> editPassword(@RequestBody PasswordRequestDto requestDto) {
        Map<String, Object> map = new HashMap<>();
        String oldPassword = requestDto.getOldPassword();
        String newPassword = requestDto.getNewPassword();
        String rePassword = requestDto.getRePassword();
        LoginResponseDto userInfo = (LoginResponseDto) request.getSession().getAttribute("userInfo");
        String userName = userInfo.getUserName();
        User user = userService.queryByUsername(userName);
        String resultPassword = user.getPassword();
        if (resultPassword.equals(Md5Util.getMd5(oldPassword))) {
            if (!oldPassword.equals(newPassword)) {
                if (newPassword.equals(rePassword)) {
                    try {
                        userService.updatePassword(userName, newPassword);
                    } catch (Exception e) {
                        map.put("success", false);
                        map.put("errMsg", e.getMessage());
                        return map;
                    }
                } else {
                    map.put("success", false);
                    map.put("errMsg", "确认密码不一致");
                    return map;
                }
            } else {
                map.put("success", false);
                map.put("errMsg", "不能与初始密码相同");
                return map;
            }
        } else {
            map.put("success", false);
            map.put("errMsg", "旧密码错误");
            return map;
        }
        map.put("success", true);
        return map;
    }
}

interceptor登陆拦截器

LoginInterceptor

package com.echarts.interceptor;

import com.echarts.dto.responseDto.LoginResponseDto;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: bai
 * @date: 2022/5/23 18:26
 * @description:
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object userObject = request.getSession().getAttribute("userInfo");
        if (userObject != null) {
            LoginResponseDto userInfo = (LoginResponseDto) userObject;
            if (userInfo.getId() != null) {
                //如果用户已经登录,url为/login或者/,就自动跳转/main
                if (request.getRequestURI().toLowerCase().contains("login") || "/".equals(request.getRequestURI())) {
                    response.sendRedirect("/main");
                    return false;
                }
                return true;
            }
        }
        if (request.getRequestURI().toLowerCase().contains("login") || "/".equals(request.getRequestURI())) {
            return true;
        }
        response.sendRedirect("login");
        return false;
    }
}

config配置类

SwaggerConfig

package com.echarts.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author: bai
 * @date: 2022/5/23 18:38
 * @description:
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Echarts")
                .version("v1.0")
                .termsOfServiceUrl("http://localhost:8080/main")
                .build();
    }
}

util工具类

CodeUtil

package com.echarts.util;

import com.google.code.kaptcha.Constants;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: bai
 * @date: 2022/4/17 16:28
 * @description: 验证码校验
 */
public class CodeUtil {
    /**
     * @param request:获取实际的验证码
     * @param verifyCodeActual:用户输入的验证码
     * @return: boolean
     * @description:
     */
    public static boolean checkVerifyCode(HttpServletRequest request, String verifyCodeActual) {
        //生成的验证码
        String verifyCodeExpected = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        //用户输入验证码
        verifyCodeActual = verifyCodeActual.toUpperCase();
        if (verifyCodeActual == null || !verifyCodeExpected.equalsIgnoreCase(verifyCodeActual)){
            return false;
        }
        return true;
    }
}

Md5Util  

package com.echarts.util;

import java.security.MessageDigest;

/**
 * @author: bai
 * @date: 2022/4/30 12:50
 * @description: md5加密, 无解密
 */
public class Md5Util {

    public static String getMd5(String origin) {

        //自定义数组,相当于盐
        char[] hexArray = {
                '5', 'a', '4', 'b', '9', '6', '8', 'f', 'e', '2', '2', '7', 'e', 'o', 'g', '0'
        };
        try {
            byte[] originBytes = origin.getBytes();
            //md5加密实例
            MessageDigest md5Instance = MessageDigest.getInstance("MD5");
            md5Instance.update(originBytes);
            //加密后数组
            byte[] digest = md5Instance.digest();
            //加盐数组
            char[] str = new char[digest.length * 2];
            int k = 0;
            //对加密数组加盐
            //判断数组长度,遍历数组,对每个数组进行移位运算(二进制)
            for (byte b : digest) {
                //0xf(16进制):1111(2进制)
                str[k++] = hexArray[b >>> 4 & 0xf];
                str[k++] = hexArray[b & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return "123456";
        }
    }

    public static void main(String[] args) {
        System.out.println(Md5Util.getMd5("1513"));
    }
}

mybatis映射文件

PeopleDao.xml

<?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.echarts.dao.PeopleDao">

    <resultMap type="com.echarts.entity.People" id="PeopleMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="province" column="province" jdbcType="VARCHAR"/>
        <result property="number" column="number" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="PeopleMap">
        select id,
               province,
               number
        from people
        where id = #{id}
    </select>

    <select id="queryAll" resultMap="PeopleMap">
        select id, province, number
        from echarts.people
    </select>

    <select id="queryNumber" resultType="java.lang.Double">
        select number
        from echarts.people
        where province = #{province}
    </select>
    <select id="queryAllBySort" resultType="com.echarts.entity.People">
        select id, province, number
        from echarts.people
        order by number desc
    </select>

    <!--通过主键修改数据-->
    <update id="update">
        update echarts.people
        set number = #{number}
        where province = #{province}
    </update>

</mapper>

UserDao.xml

<?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.echarts.dao.UserDao">

    <resultMap type="com.echarts.entity.User" id="UserMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="UserMap">
        select id,
               username,
               password
        from echarts.user
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="UserMap">
        select id,
               username,
               password
        from echarts.user
        limit #{offset}, #{limit}
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="queryAll" resultMap="UserMap">
        select
        id, username, password
        from echarts.user
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="username != null and username != ''">
                and username = #{username}
            </if>
            <if test="password != null and password != ''">
                and password = #{password}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into echarts.user(username, password)
        values (#{username}, #{password})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into echarts.user(username, password)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.username}, #{entity.password})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into echarts.user(username, password)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.username}, #{entity.password})
        </foreach>
        on duplicate key update
        username = values(username) , password = values(password)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update echarts.user
        <set>
            <if test="username != null and username != ''">
                username = #{username},
            </if>
            <if test="password != null and password != ''">
                password = #{password},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="updatePassword">
        update echarts.user
        <set>
            <if test="password != null and password != ''">
                password = #{password},
            </if>
        </set>
        where username=#{username}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from echarts.user
        where id = #{id}
    </delete>

    <resultMap type="com.echarts.dto.responseDto.LoginResponseDto" id="UserInfoMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="userName" column="username" jdbcType="VARCHAR"/>
        <!--        <result property="password" column="password" jdbcType="VARCHAR"/>-->
    </resultMap>

    <select id="getUserInfoByUsernameAndPassword" resultMap="UserInfoMap">
        select id, username, password
        from echarts.user
        where username = #{username}
          and password = #{password}
    </select>
    <select id="queryByUsername" resultType="com.echarts.entity.User">
        select username, password
        from echarts.user
        where username = #{username}
    </select>
</mapper>

 spring配置文件

application.xml

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.echarts"/>
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" id="resolver">
        <property name="suffix" value=".ftl"/>
        <property name="contentType" value="text/html;charset=UTF-8"/>
    </bean>
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" id="messageConverter">
                <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>
    <mvc:default-servlet-handler/>
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" id="freeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/ftl"/>
        <property name="freemarkerSettings">
            <props>
                <prop key="defaultEncoding">UTF-8</prop>
            </props>
        </property>
    </bean>
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
    </bean>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/checkLogin"/>
            <mvc:exclude-mapping path="/checkRegist"/>
            <mvc:exclude-mapping path="/regist"/>
            <mvc:exclude-mapping path="/*.ico"/>
            <mvc:exclude-mapping path="/css/**"/>
            <mvc:exclude-mapping path="/fonts/**"/>
            <mvc:exclude-mapping path="/images/**"/>
            <mvc:exclude-mapping path="/js/**"/>
            <mvc:exclude-mapping path="/case03/**"/>
            <mvc:exclude-mapping path="/case04/**"/>
            <mvc:exclude-mapping path="/swagger-ui.html/**"/>
            <mvc:exclude-mapping path="/webjars/**"/>
            <mvc:exclude-mapping path="/swagger-resources/**"/>
            <mvc:exclude-mapping path="/v2/**"/>
            <mvc:exclude-mapping path="/csrf/**"/>
            <bean class="com.echarts.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    <mvc:cors>
        <mvc:mapping path="/**" allowed-origins="*" allowed-methods="GET,POST" allowed-headers="Content-Type,Access-Control-Allow-Headers,Authorization,X-Request-With" allow-credentials="true"/>
    </mvc:cors>
    <bean class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:mappers/*Dao.xml"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="scannerConfigurer">
        <property name="basePackage" value="com.echarts.dao"/>
    </bean>
</beans>

 数据库配置文件

jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/echarts?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
jdbc.username=root
jdbc.password=1513

sql 

use echarts;
drop table if exists user;
create table user(
    id integer primary key auto_increment,
    username varchar(20),
    password varchar(20)
);
drop table if exists people;
create table people(
    id integer primary key auto_increment,
    province varchar(20),
    number integer
);
insert into people(province, number) values('北京','1961');
insert into people(province, number) values('辽宁','4374');
insert into people(province, number) values('吉林','2745');
insert into people(province, number) values('黑龙江','3831');
insert into people(province, number) values('四川','8041');
insert into people(province, number) values('湖北','5723');
insert into people(province, number) values('福建','3689');
insert into people(province, number) values('广东','10432');
insert into people(province, number) values('重庆','2884');
insert into people(province, number) values('湖南','6570');
insert into people(province, number) values('上海','2301');
insert into people(province, number) values('江苏','7866');
insert into people(province, number) values('浙江','5442');
insert into people(province, number) values('安徽','5950');
insert into people(province, number) values('天津','1293');
insert into people(province, number) values('山东','9579');
insert into people(province, number) values('山西','3571');
insert into people(province, number) values('河南','9402');
insert into people(province, number) values('河北','7185');
insert into people(province, number) values('内蒙古','2470');
insert into people(province, number) values('江西','4456');
insert into people(province, number) values('贵州','3474');
insert into people(province, number) values('云南','4596');
insert into people(province, number) values('西藏','300');
insert into people(province, number) values('陕西','3732');
insert into people(province, number) values('甘肃','2557');
insert into people(province, number) values('青海','562');
insert into people(province, number) values('宁夏','630');
insert into people(province, number) values('新疆','2181');
insert into people(province, number) values('广西','4602');
insert into people(province, number) values('海南','867');
insert into people(province, number) values('台湾','2356');

前端代码省略

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生气要吃糖吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值