springboot入门(7)springboot和freemarker实现用户管理

1、实现的基本功能,包括用户信息的列表展示,新增,修改,删除功能

2、界面演示

springboot入门(7)springboot和freemarker实现用户管理_第1张图片
image.png
springboot入门(7)springboot和freemarker实现用户管理_第2张图片
image.png

3、controller的代码

package com.example.demo.controller;

import com.example.demo.service.IUserService;

import org.springframework.stereotype.Controller;

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

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

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.example.demo.entity.User;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

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

import java.util.Map;

import java.util.List;

@Controller

public class TestController {

    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/index")

    public String home() {

        logger.info(">>> index");

        return "index";

    }

    @RequestMapping("/test")

    public String testFtl(Map map){

        map.put("name", "ftl");

        map.put("sj", "123456789");

        return "test";

    }

    @RequestMapping("/world")

    public String world(HttpServletRequest request) {

        request.setAttribute("name", "xxxxxxxxx");

        return "world";

    }

    @Autowired

    private IUserService userService;

//    @RequestMapping("/user")

//    @ResponseBody()

//    public String getItem(HttpServletRequest request) {

//

//        String id = request.getParameter("id");

//        logger.info("进入getItem方法,id:"+id);

//

//        User user = userService.selectByPrimaryKey(id);

//        request.setAttribute("user", user);

//

//        return user.getName();

//    }

    @RequestMapping(value = "/user-list")

    public String queryUser(HttpServletRequest request) {

        List user_list = userService.queryUser();

        request.setAttribute("user_list", user_list);

        return "user-list";

    }

    @RequestMapping(value = "/user-add")

//    @ResponseBody()

    public String addUser(HttpServletRequest request) {

        request.setAttribute("id", "");

        request.setAttribute("name", "");

        request.setAttribute("age", "");

        request.setAttribute("sex", "");

        request.setAttribute("mobile", "");

        return "user-add";

    }

    @RequestMapping(value = "/user-edit", method = RequestMethod.GET)

//    @ResponseBody()

    public String editUser(HttpServletRequest request) {

        String id = request.getParameter("id");

        Integer user_id = Integer.valueOf(id);

        User user = userService.selectByPrimaryKey(user_id);

        request.setAttribute("id", user.getId());

        request.setAttribute("name", user.getName());

        request.setAttribute("age", user.getAge());

        request.setAttribute("sex", user.getSex());

        request.setAttribute("mobile", user.getMobile());

        return "user-add";

    }

    @RequestMapping(value = "/user-add-save", method = RequestMethod.POST)

    public String saveUser(HttpServletRequest request) {

        String name = request.getParameter("name");

        String age = request.getParameter("age");

        String sex = request.getParameter("sex");

        String mobile = request.getParameter("mobile");

        if(request.getParameter("id") == null || request.getParameter("id").equals("")) {

            logger.info("==== insert =====");

            User record = new User();

            record.setName(name);

            record.setAge(age);

            record.setSex(sex);

            record.setMobile(mobile);

            userService.insert(record);

        }else {

            logger.info("===== update ======");

            Integer id = Integer.valueOf(request.getParameter("id"));

            User user = userService.selectByPrimaryKey(id);

            user.setName(name);

            user.setAge(age);

            user.setSex(sex);

            user.setMobile(mobile);

            userService.updateByPrimaryKey(user);

        }

        return "redirect:/user-list";

    }

    @RequestMapping(value = "/user-delete", method = RequestMethod.GET)

    public String deleteUser(HttpServletRequest request) {

        Integer id = Integer.valueOf(request.getParameter("id"));

        userService.deleteByPrimaryKey(id);

        return "redirect:/user-list";

    }

}

你可能感兴趣的:(springboot入门(7)springboot和freemarker实现用户管理)