springboot+jsp简单用户列表的增删改差

首先创建一个springboot项目网上有相对应的教程

springboot+jsp简单用户列表的增删改差_第1张图片

展示一下目录路径

springboot+jsp简单用户列表的增删改差_第2张图片

springboot+jsp简单用户列表的增删改差_第3张图片

接下来就是xml配置



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.testsystem
    demo
    0.0.1-SNAPSHOT
    demo
    war
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.1
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            9.0.12
        
        
            javax.servlet
            jstl
            1.2
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-beanutils
            commons-beanutils
            1.7.0
        
        
            commons-codec
            commons-codec
            1.7
        
        
            org.apache.commons
            commons-io
            1.3.2
        
        
            commons-lang
            commons-lang
            2.0
        
        
            commons-cli
            commons-cli
            1.3.1
        
        
            commons-collections
            commons-collections
            3.2
        
        
            commons-net
            commons-net
            3.0
        
        
            org.apache.commons
            commons-math3
            3.2
        
        
            commons-validator
            commons-validator
            1.4.0
        
        
            commons-httpclient
            commons-httpclient
            3.1
        
        
            commons-dbcp
            commons-dbcp
            1.4
        
        
            commons-logging
            commons-logging-api
            1.1
        
        
            commons-pool
            commons-pool
            1.6
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


pom文件配置完成后就是application配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot_project?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.typeAliasesPackage=com.testsystem.demo.entity
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapperLocations=classpath:mapper/*.xml
server.port=8080
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

接下来是启动类文件配置,因为springboot内置了tomcat,所以外部tomcat要启动的时候需要修改配置(具体我也不是很了解)

package com.testsystem.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

下面就是常规代码了

1常亮文件夹

package com.testsystem.demo.constants;

public interface Contants {
    String SUCCESS="200";
    String FAIl="500";
}

2默认跳转

package com.testsystem.demo.controller;

import com.testsystem.demo.entity.User;
import com.testsystem.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

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

@Configuration
public class IndexController extends WebMvcConfigurerAdapter {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {

            registry.addViewController("/").setViewName("user");

            registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

            super.addViewControllers(registry);

        }

}

3控制层

package com.testsystem.demo.controller;

import com.testsystem.demo.constants.Contants;
import com.testsystem.demo.entity.User;
import com.testsystem.demo.service.UserService;
import com.testsystem.demo.vo.UserDetailVo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

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

@Controller
@RequestMapping(value = "/user")
@EnableAutoConfiguration
public class UserController {

    @Autowired
    private UserService userService;
    @GetMapping("/index")
    @ResponseBody
    public ModelAndView index(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("user");
        return modelAndView;
    }
    @GetMapping("/userDetail")

    public ModelAndView userDetail(String id){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("userDetail");
        if(!StringUtils.isBlank(id)){
            modelAndView.addObject("userId",id);
        }else{
            modelAndView.addObject("userId","");
        }

        return modelAndView;
    }
    @GetMapping("/queryUserMessage")
    @ResponseBody
    public Map queryUserMessage(){
        List list= userService.queryUserMessage();
        Map map=new HashMap<>();
        map.put("list",list);
        map.put("code",Contants.SUCCESS);
        return map;
    }
    @GetMapping("/queryUserMessageDetail")
    @ResponseBody
    public Map queryUserMessageDetail(@RequestParam String id){
        User user= userService.queryUserMessageDetail(id);
        Map map=new HashMap<>();
        map.put("data",user);
        map.put("code", Contants.SUCCESS);
        return map;
    }
    @PostMapping("/insertUserMessage")
    @ResponseBody
    public Map insertUserMessage(UserDetailVo userDetailVo){
        Map map = userService.insertUserMessage(userDetailVo);

        return map;
    }
    @PostMapping("/deleteUserMessage")
    @ResponseBody
    public Map deleteUserMessage(String id){
        Map map=userService.deleteUserMessage(id);
        return map;
    }

    @PostMapping("/updateUserMessage")
    @ResponseBody
    public Map updateUserMessage(UserDetailVo userDetailVo){
        Map map= userService.updateUserMessage(userDetailVo);
        return map;
    }

}

4实体层

package com.testsystem.demo.entity;

import java.util.Date;

public class User {
    private String id;
    private String userName;
    private String password;
    private String phone;
    private String email;
    private String account;
    private Date createTime;
    private String createId;
    private Date updateTime;
    private String updateId;

    public String getId() {
        return id;
    }

    public void setId(String 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;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCreateId() {
        return createId;
    }

    public void setCreateId(String createId) {
        this.createId = createId;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getUpdateId() {
        return updateId;
    }

    public void setUpdateId(String updateId) {
        this.updateId = updateId;
    }
}

5服务层和实现类

package com.testsystem.demo.service;

import com.testsystem.demo.entity.User;
import com.testsystem.demo.vo.UserDetailVo;

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

public interface UserService {
    List queryUserMessage();

    User queryUserMessageDetail(String id);

    Map insertUserMessage(UserDetailVo userDetailVo);

    Map deleteUserMessage(String id);

    Map updateUserMessage(UserDetailVo userDetailVo);
}
package com.testsystem.demo.service.impl;

import com.testsystem.demo.constants.Contants;
import com.testsystem.demo.dao.UserMapper;
import com.testsystem.demo.entity.User;
import com.testsystem.demo.service.UserService;
import com.testsystem.demo.utils.DateUtil;
import com.testsystem.demo.vo.UserDetailVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


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

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List queryUserMessage() {
        return userMapper.queryUserMessage();
    }

    @Override
    public User queryUserMessageDetail(String id) {
        return userMapper.queryUserMessageDetail(id);
    }

    @Override
    public Map insertUserMessage(UserDetailVo userDetailVo) {
        if(userDetailVo==null){
            userDetailVo=new UserDetailVo();
        }
        Map map=new HashMap<>();
        userDetailVo.setId(UUID.randomUUID().toString());
        userDetailVo.setCreateTime(DateUtil.now());
        userDetailVo.setUpdateTime(DateUtil.now());
        userMapper.insertUserMessage(userDetailVo);
        map.put("code", Contants.SUCCESS);
        map.put("msg","新增成功");

        return map;
    }

    @Override
    public Map deleteUserMessage(String id) {
        Map map=new HashMap<>();
        if(StringUtils.isBlank(id)){
            map.put("code", Contants.FAIl);
            map.put("msg","id不能为空");
        }else {
            userMapper.deleteUserMessage(id);
            map.put("code", Contants.SUCCESS);
            map.put("msg","删除成功");
        }
        return map;
    }

    @Override
    public Map updateUserMessage(UserDetailVo userDetailVo) {

        if(userDetailVo==null){
            userDetailVo=new UserDetailVo();
        }
        Map map=new HashMap<>();
        if(StringUtils.isBlank(userDetailVo.getId())){
            map.put("code", Contants.FAIl);
            map.put("msg","id不能为空");
        }else {
            userMapper.updateUserMessage(userDetailVo);
            userDetailVo.setUpdateTime(DateUtil.now());
            map.put("code", Contants.SUCCESS);
            map.put("msg","更新成功");
        }

        return map;
    }

}

6dao层和对应的xml,sql配置

package com.testsystem.demo.dao;

import com.testsystem.demo.entity.User;
import com.testsystem.demo.vo.UserDetailVo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List queryUserMessage();

    User queryUserMessageDetail(String id);

    void insertUserMessage(UserDetailVo userDetailVo);

    void deleteUserMessage(String id);

    void updateUserMessage(UserDetailVo userDetailVo);
}



    
        
        
        
        
        
        
        
        
        
        
    
    
    
    
        INSERT INTO t_user
        (id,user_name,password, phone, email, account,create_time,create_id,update_time,update_id)
        VALUES (#{id},#{userName},#{password}, #{phone},#{email},
        #{account},#{createTime},#{createId},#{updateTime},#{updateId});

    
    
        delete from t_user where id=#{id}
    
    
        UPDATE t_user SET
        user_name = #{userName},password = #{password},
        update_time = #{updateTime}, update_id= #{updateId} WHERE id = #{id};
    

7工具类

package com.testsystem.demo.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date now() {
        return new Date();
    }

    /**
     * Date转String
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }


}

8vo对象

package com.testsystem.demo.vo;

import java.util.Date;

public class UserDetailVo {
    private String id;
    private String userName;
    private String password;
    private String phone;
    private String email;
    private String account;
    private Date createTime;
    private String createId;
    private Date updateTime;
    private String updateId;

    public String getId() {
        return id;
    }

    public void setId(String 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;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCreateId() {
        return createId;
    }

    public void setCreateId(String createId) {
        this.createId = createId;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getUpdateId() {
        return updateId;
    }

    public void setUpdateId(String updateId) {
        this.updateId = updateId;
    }
}

接下来是创建数据库

CREATE TABLE `t_user` (
  `id` varchar(36) NOT NULL,
  `user_name` varchar(64) DEFAULT NULL COMMENT '用户名',
  `password` varchar(64) DEFAULT NULL COMMENT '密码',
  `phone` varchar(11) DEFAULT NULL COMMENT '电话',
  `email` varchar(64) DEFAULT NULL COMMENT '邮箱',
  `account` varchar(36) DEFAULT NULL COMMENT '账号',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `create_id` varchar(36) DEFAULT NULL COMMENT '创建人',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `update_id` varchar(36) DEFAULT NULL COMMENT '更新人',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `springboot_project`.`t_user`
(`id`, `user_name`, `password`, `phone`, `email`, `account`, `create_time`, `create_id`, `update_time`, `update_id`) 
VALUES ('1', '帅', '123456', '123456', '[email protected]', '123123', '2020-01-05 08:48:34', NULL, '2020-01-05 08:48:37', NULL);

jsp简单的table列表

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>


    Title


用户名 电话 邮箱 账号 创建时间 更新时间 操作

编辑新增页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title



用户名:

密码:

 

你可能感兴趣的:(springboot+jsp简单用户列表的增删改差)