Springboot2模块系列:thymeleaf+Bootstrap(三)前后端结合--业务逻辑

1 Interface

用户信息增删查改接口.

package com.personal.microspersonaluser.mapper;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.personal.microspersonaluser.po.UserInfos;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author xdq
 * @description 用户信息增删查改.
 * @date 2020/6/6
 */
public interface UserInfosMapper {
    // 添加用户
    public Integer addUser(UserInfos params);
    // 删除用户
    public Integer deleteUser(Integer id );
    // 修改用户
    public Integer editUser(UserInfos params);
    // 查询用户信息
    public List<UserInfos> queryUser(Map params);
    // 查询单个用户
    public UserInfos queryUserWithId(Integer id);
    // 用户登录
    public UserInfos loginByEmail(String email);
}

2 Service

用户信息增删查改实现.

package com.personal.microspersonaluser.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.personal.microspersonaluser.mapper.UserInfosMapper;
import com.personal.microspersonaluser.po.UserInfos;

/**
 * @author xdq
 * @description 用户信息增删查改实现.
 * @date 2020/6/6
 */
@Service
@Transactional
public class UserInfosService {
    @Autowired
    private UserInfosMapper userInfosMapper;
    // 新建用户
    public Integer addUser(UserInfos params){
        return userInfosMapper.addUser(params);
    }
    // 删除用户
    public Integer deleteUser(Integer id){
        return userInfosMapper.deleteUser(id);
    }
    // 编辑用户
    public Integer editUser(UserInfos params){
        return userInfosMapper.editUser(params);
    }
    // 查询所有用户
    public List queryUser(Map params){
        return userInfosMapper.queryUser(params);
    }
    // 查询单个用户
    public UserInfos queryUserWithId(Integer id){
        return userInfosMapper.queryUserWithId(id);
    }
    // 用户登录
    public UserInfos loginByEmail(String email){
        return userInfosMapper.loginByEmail(email);
    }
}

3 Controller

用户信息增删查改页面操作.

package com.personal.microspersonaluser.controller;

import com.personal.microspersonaluser.po.UserInfos;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.http.ResponseEntity; 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personal.microspersonaluser.service.UserInfosService;

/**
 * @author xdq
 * @description 用户信息API及页面加载.
 * @date 2020/6/6
 */

@CrossOrigin(origins="*", maxAge=3600)
@Controller 
@RequestMapping("/api/page")
public class PageController {
    static Logger logger = LoggerFactory.getLogger(PageController.class);
    @Autowired
    private UserInfosService userInfosService;
    
    // 用户登录
    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String login(ModelMap map, Model model, @ModelAttribute UserInfos params, HttpSession session){
        UserInfos userInfos = new UserInfos();
        // map.put("userInfo", userInfos);
        session.setAttribute("loginFlag", "loginFlag");
        model.addAttribute("userInfo", params);
        return "sign-in/index";
    }
    // 新建用户
    @RequestMapping(value="/ui/user/add", method=RequestMethod.GET)
    public String addUserWithPage(ModelMap map, Model model){
        model.addAttribute("userInfo", new UserInfos());
        return "user-info/add";
    }
    // 用户信息保存
    @RequestMapping(value="/ui/user/save", method=RequestMethod.POST)
    public String saveAddUserInfo(@ModelAttribute UserInfos params){
        Integer resFlag = userInfosService.addUser(params);
        return "redirect:/api/page/datasshow";
    }
    // 删除用户
    @RequestMapping(value="/ui/user/delete", params="id", method=RequestMethod.GET)
    public String deleteUserWithId(@RequestParam("id") Integer id, ModelMap map, Model model){
        System.out.println("id:"+id);
        Integer resFlagDel = userInfosService.deleteUser(id);
        return "redirect:/api/page/datasshow";
    }
    // 修改用户
    @RequestMapping(value="/ui/user/edit", params="id", method=RequestMethod.GET)
    public String editUserInfo(@RequestParam("id") Integer id, ModelMap map, Model model){
        UserInfos userDatas = userInfosService.queryUserWithId(id);
        System.out.println("id:"+userDatas.getId());
        // params.setUsername("xiaohua");
        model.addAttribute("userInfo", userDatas);
        // model.addAttribute("userInfo", params);
        // System.out.println("username:"+params.getUsername());
        // map.addAttribute("userInfo", new UserInfos());
        return "user-info/edit";
    }
    // 保存修改用户数据
    @RequestMapping(value="/ui/user/update", method=RequestMethod.POST)
    public String editSaveUserInfo(@ModelAttribute UserInfos params, Model model){
        model.addAttribute("userInfo", params);
        Integer resFlag = userInfosService.editUser(params);
        return "redirect:/api/page/datasshow";
    }
    // 查询所有用户信息
    @RequestMapping(value="/ui/user/query", method=RequestMethod.GET)
    public ModelAndView dataQuery(@RequestParam("username") String username, ModelMap map){
        // System.out.println("email from sign:"+params.getEmail());
        Integer pageNum=0,pageSize=5;
        PageHelper.startPage(pageNum, pageSize);
        map.put("username", username);
        List resFlag = userInfosService.queryUser(map);
        PageInfo pageInfo = new PageInfo(resFlag, pageSize);
        ModelAndView mav = new ModelAndView("datasshow/index");
        // mav.addObject("resultLi", resFlag);
        mav.addObject("resultLi", pageInfo);
        mav.addObject("total", pageInfo.getTotal());
        mav.addObject("pageNum", pageInfo.getPageNum());
        mav.addObject("pageSize", pageInfo.getPageSize());
        mav.addObject("isFirstPage", pageInfo.isIsFirstPage());
        mav.addObject("totalPages", pageInfo.getPages());
        mav.addObject("isLastPage", pageInfo.isIsLastPage());
        return mav;
    }
    // 数据展示
    @RequestMapping(value="/datasshow",method=RequestMethod.GET)
    public String datasShow(@ModelAttribute UserInfos params, ModelMap map, Model model, HttpSession session,
    @RequestParam(value="pageNum", defaultValue="0")Integer pageNum,
    @RequestParam(value="pageSize", defaultValue="5") Integer pageSize){
        String url = "sign-in/index";
        Object loginFlag = session.getAttribute("loginFlag");
        System.out.println("login flag:"+loginFlag);
        if(loginFlag == null||!loginFlag.toString().equals("loginFlag")){
            model.addAttribute("userInfo", params);
            url = "redirect:/api/page/login";
        }else{
            map.put("username", "");
            System.out.println("pageSize:"+pageSize);
            PageHelper.startPage(pageNum, pageSize);
            List resFlag = userInfosService.queryUser(map);
            PageInfo pageInfo = new PageInfo(resFlag, pageSize);
            model.addAttribute("resultLi", pageInfo);
            model.addAttribute("total", pageInfo.getTotal());
            model.addAttribute("pageNum", pageInfo.getPageNum());
            model.addAttribute("pageSize", pageInfo.getPageSize());
            model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
            model.addAttribute("totalPages", pageInfo.getPages());
            model.addAttribute("isLastPage", pageInfo.isIsLastPage());
            url = "datasshow/index"; 
        }
        return url;  
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天然玩家

坚持才能做到极致

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

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

打赏作者

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

抵扣说明:

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

余额充值