Java项目:校园社团管理系统(java+Jdbc+Servlet+jsp+mysql)

源码获取:博客首页 "资源" 里下载!

一、项目运行

环境配置:

Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)

项目技术:

Jdbc+ Servlert + jsp+ css + JavaScript + JQuery + Ajax 等等

Java项目:校园社团管理系统(java+Jdbc+Servlet+jsp+mysql)_第1张图片

 Java项目:校园社团管理系统(java+Jdbc+Servlet+jsp+mysql)_第2张图片

 Java项目:校园社团管理系统(java+Jdbc+Servlet+jsp+mysql)_第3张图片

 Java项目:校园社团管理系统(java+Jdbc+Servlet+jsp+mysql)_第4张图片

 Java项目:校园社团管理系统(java+Jdbc+Servlet+jsp+mysql)_第5张图片

后台用户管理控制器:

/**
 * 后台用户管理控制器
 */
@RequestMapping("/admin/user")
@Component
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;
    @Autowired
    private OperaterLogService operaterLogService;
    /**
     * 用户列表页面
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, User user, PageBean pageBean){
        model.addAttribute("usernmae",user.getUsername());
        model.addAttribute("pageBean",userService.findList(user,pageBean));
        model.addAttribute("title","用户列表");
        return "admin/user/list";
    }


    /**
     * 用户添加页面
     * @param model
     * @param
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(Model model){
        List all = roleService.findAll();
        model.addAttribute("roles",all);
        return "admin/user/add";
    }

    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(Model model,User user){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(user);
        if(validate.getCode()!=CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        //判断用户的角色是否选择
        if(user.getRole()==null ||user.getRole().getId()==null){
            return Result.error(CodeMsg.ADMIN_USER_ROLE_EMPTY);
        }
        //没有ID 传个0进去 0不是Long类型 所以加个0l L
        if(userService.isExistUsername(user.getUsername(),0l)){
            return Result.error(CodeMsg.ADMIN_USER_NAME_EXIST);
        }
        //到这说明一切符合条件进行数据库新增
        if(userService.save(user)==null){
            return Result.error(CodeMsg.ADMIN_USER_ADD_ERROR);
        }
        operaterLogService.add("添加用户,用户名:"+user.getUsername());
        return Result.success(true);
    }

    /**
     * 用户编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(Model model,@RequestParam(name = "id",required = true) Long id){
        model.addAttribute("user",userService.find(id));
        model.addAttribute("roles",roleService.findAll());
        return "admin/user/edit";
    }

    /**
     * 编辑用户信息表单提交处理
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(User user){
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(user);
        if(validate.getCode()!=CodeMsg.SUCCESS.getCode()){
            return Result.error(validate);
        }
        //判断用户的角色是否选择
        if(user.getRole()==null ||user.getRole().getId()==null){
            return Result.error(CodeMsg.ADMIN_USER_ROLE_EMPTY);
        }
        if(user.getId()==null||user.getId().longValue()<=0){
            return Result.error(CodeMsg.ADMIN_USER_NO_EXIST);
        }
        //判断数据库user表有没有这个用户名
        if(userService.isExistUsername(user.getUsername(),user.getId().longValue())){
                return Result.error(CodeMsg.ADMIN_USER_NAME_EXIST);
        }
        //将提交的用户信息指定字段复制到已存在的user对象中
        User findbyId = userService.find(user.getId());
        //把source原来的字段复制到目标对象当中ignoreProperties表示忽略哪些字段 该方法会覆盖新字段内容
        BeanUtils.copyProperties(user,findbyId,"id","createTime","updateTime");
        //到这说明一切通过 开始进行数据库编辑
        if(userService.save(findbyId)==null){
            return Result.error(CodeMsg.ADMIN_USER_EDIT_ERROR);
        }
        operaterLogService.add("编辑用户,用户名:"+user.getUsername());
        return Result.success(true);
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Result delete(@RequestParam(name = "id",required = true) Long id) {
        try {
        userService.delete(id);
        } catch (Exception e){
            return Result.error(CodeMsg.ADMIN_USER_DELETE_ERROR);
        }
        operaterLogService.add("删除用户,id为:"+id);
        return  Result.success(true);
    }
}

后台角色管理控制器:

/**
 * 后台角色管理控制器
 */

@RequestMapping("/admin/role")
@Controller
public class RoleController {

    @Autowired
    private MenuService menuService;
    private Logger log= LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private OperaterLogService operaterLogService;
    @Autowired
    private RoleService roleService;

    /**
     * 分页搜索角色列表
     * @param model
     * @param role
     * @param pageBean
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(Model model, Role role, PageBean pageBean){
        model.addAttribute("title","角色列表");
        model.addAttribute("name",role.getName());
        model.addAttribute("pageBean",roleService.findByName(role,pageBean));
        return "admin/role/list";
    }

    /**
     * 角色添加页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(Model model){
        List all = menuService.findAll();
        model.addAttribute("title","添加角色");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(all));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(all));
        model.addAttribute("thirdMenus", MenuUtil.getThirdMenus(all));
        return "admin/role/add";
    }
    /**
     * 角色添加表单提交处理
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(Role role, HttpServletRequest request){
        CodeMsg validate = ValidateEntityUtil.validate(role);
        if(validate.getCode()!=CodeMsg.SUCCESS.getCode()){
            return  Result.error(validate);
        }
        if(roleService.save(role)==null){
            return  Result.error(CodeMsg.ADMIN_ROLE_ADD_ERROR);
        }
        log.info("添加角色["+role+"]");
        operaterLogService.add("添加角色["+role.getName()+"]");
        return Result.success(true);
    }


    /**
     * 角色编辑页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(@RequestParam(name = "id",required = true) Long id, Model model){
        List all = menuService.findAll();
        model.addAttribute("title","添加角色");
        model.addAttribute("topMenus", MenuUtil.getTopMenus(all));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(all));
        model.addAttribute("thirdMenus", MenuUtil.getThirdMenus(all));
        Role role = roleService.find(id);
        model.addAttribute("role",role);
        //list转换为数组
        model.addAttribute("authorities", JSONArray.toJSON(role.getAuthorities()).toString());
        return "admin/role/edit";
    }


    /**
     * 角色修改表单提交处理
     * @param role
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(Role role, HttpServletRequest request){
        CodeMsg validate = ValidateEntityUtil.validate(role);
        if(validate.getCode()!=CodeMsg.SUCCESS.getCode()){
            return  Result.error(validate);
        }
        Role existRole = roleService.find(role.getId());
        if(existRole==null){
            return  Result.error(CodeMsg.ADMIN_ROLE_NO_EXIST);
        }
        existRole.setName(role.getName());
        existRole.setRemark(role.getRemark());
        existRole.setStatus(role.getStatus());
        existRole.setAuthorities(role.getAuthorities());
        if(roleService.save(existRole)==null){
            return  Result.error(CodeMsg.ADMIN_ROLE_EDIT_ERROR);
        }
        log.info("编辑角色["+role+"]");

        operaterLogService.add("编辑角色["+role.getName()+"]");
        return Result.success(true);
    }
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Result delete(@RequestParam(name = "id",required = true) Long id,HttpServletRequest request){
        try {
            roleService.delete(id);
        }catch (Exception e){
            return Result.error(CodeMsg.ADMIN_ROLE_DELETE_ERROR);
        }
        log.info("删除角色ID["+id+"]");
        operaterLogService.add("删除角色ID["+id+"]");
        return Result.success(true);
    }

}

后台学生管理:

/**
 * 后台学生管理
 */

@RequestMapping("/admin/student/")
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService ;

    @Autowired
    private OperaterLogService operaterLogService;

    /**
     * 学生管理列表
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, Student student, PageBean pageBean){
        model.addAttribute("pageBean",studentService.findList(student, pageBean));
        model.addAttribute("studentLoginName",student.getLoginName());
        model.addAttribute("title","学生列表");
        return "/admin/student/list";
    }

    /**
     * 后台学生添加页面
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "/admin/student/add";
    }

    /**
     * 后台学生添加信息操作
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(Model model,Student student){
        CodeMsg validate = ValidateEntityUtil.validate(student);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        if(studentService.findByLoginName(student.getLoginName())!=null){
            return Result.error(CodeMsg.ADMIN_STUDENT_ISEXIST_ERROR);
        }
        if(studentService.save(student)==null){
            return Result.error(CodeMsg.ADMIN_STUDENT_ADD_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 编辑学生页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(Model model, @RequestParam("id")Long id){
        if(studentService.findById(id)!=null){
            model.addAttribute("student",studentService.findById(id));
        }
        return "/admin/student/edit";
    }


    /**
     * 编辑后台学生信息
     * @param student
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result edit(Student student, HttpServletRequest request) {
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(student);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
      //将提交的学生信息指定字段复制到已存在的student对象中
        Student findbyId = studentService.findById(student.getId());
        //把source原来的字段复制到目标对象当中ignoreProperties表示忽略哪些字段 该方法会覆盖新字段内容
        BeanUtils.copyProperties(student, findbyId, "id", "createTime", "updateTime");
        //到这说明一切通过 开始进行数据库编辑
        if (studentService.save(findbyId) == null) {
            return Result.error(CodeMsg.ADMIN_STUDENT_EDIT_ERROR);
        }
        operaterLogService.add("编辑学生,学生姓名:" + student.getStuName());
        return Result.success(true);
    }


    /**
     * 学生删除操作
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestParam(name = "ids", required = true) String ids) {
        if (!StringUtils.isEmpty(ids)) {
            String[] splitIds = ids.split(",");
            for (String id : splitIds) {
                Student student = studentService.findById(Long.valueOf(id));
                if (student != null) {
                    try {
                        studentService.delete(Long.valueOf(id));
                        operaterLogService.add("删除学生,id为:" + id);
                    }catch (Exception e){
                        return Result.error(CodeMsg.ADMIN_STUDENT_DELETE_ERROR);
                    }
                }

            }

        }
        return Result.success(true);
    }

}

源码获取:博客首页 "资源" 里下载!

你可能感兴趣的:(Java毕业设计项目实战篇,java,mysql,Servlet,jdbc,jsp)