我是靠谱客的博主 开放荔枝,最近开发中收集的这篇文章主要介绍SpringBoot,Maven集成JSP简单实现用户登录,列表增删改查,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

SpringBoot,Maven集成JSP简单实现用户登录,列表增删改查

    • 项目结构
    • pom.xml
    • springboot启动类
    • controller
    • entity
    • mapper
    • service 及实现
    • applicati.xml
    • mapper.xml
    • 前端:login.jsp
    • userAdd.jsp
    • userEdit.jsp
    • userList.jsp

只是简单实现了SpringBoot,Maven集成JSP的一个基本项目,仅做学习。有不对的地方请大佬指正

项目结构

在这里插入图片描述

pom.xml

大部分包其实不需要,根据自己需求选择即可
.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.hft</groupId>
    <artifactId>userTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <zipkin.version>2.3.1</zipkin.version>
        <zipkin-reporter.version>1.1.2</zipkin-reporter.version>
        <zipkin-reporter2.version>2.1.4</zipkin-reporter2.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- spring-boot整合mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>

        <!--JavaServer Pages Standard Tag Library,JSP标准标签库-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--内置tomcat对Jsp支持的依赖,用于编译Jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

        <!--swagger配置-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

        <!--处理json-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.myfun</groupId>
            <artifactId>framework</artifactId>
            <version>2.2.50</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatisExt</artifactId>
            <version>3.4.1.15</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.8.3</version>
        </dependency>
        <!-- 数据库相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.13</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- 添加servlet依赖模块 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- 添加jstl标签库依赖模块 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!--添加tomcat依赖模块.-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId><!--sqlserver依赖  驱动jar-->
            <scope>runtime</scope>
            <version>6.4.0.jre8</version>
        </dependency>
    </dependencies>

</project>

springboot启动类

import org.mybatis.spring.annotation.MapperScan;
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
@MapperScan(basePackages = "cn.hft.user.mapper.*")
public class UserApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(UserApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}

controller



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping(path = "/save",method = RequestMethod.POST)
    public String save(User user){
        service.save(user);
        return "redirect:/user/list";
    }

    @RequestMapping(path = "/add",method = RequestMethod.GET)
    public String add() throws ServletException,IOException{
        return "/userAdd";
    }

    @RequestMapping(path = "/upd",method = RequestMethod.POST)
    public String upd(@ModelAttribute User user) throws ServletException,IOException{
        service.upd(user);
        return "redirect:/user/list";
    }

    @RequestMapping(path = "/login")
    //@ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse response,User user)throws IOException,ServletException{
        if(service.login(user)){
            return "redirect:/user/list";
        }
        return "redirect:/user/loginpage";
    }

    @RequestMapping(path = "/del/{id}",method = RequestMethod.GET)
    public String del(@PathVariable("id") int id){
        service.del(id);
        return "redirect:/user/list";
    }

    @RequestMapping(path = "/index",method = RequestMethod.GET)
    public String index(Model model){
        List<User> list = service.list();
        return "/userList";
    }

    @ResponseBody
    @RequestMapping(path = "/list",method = RequestMethod.GET)
    public ModelAndView list()throws IOException,ServletException{
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/userList");
        modelAndView.addObject("List",service.list());
        return modelAndView;
    }

    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable("id") int id){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/userEdit");
        User byid = service.findByid(id);
        System.out.println(byid.getUserName());
        System.out.println(byid.getUserPassword());
        System.out.println(byid.getId());
        modelAndView.addObject("user",service.findByid(id));
        return modelAndView;
    }

    @GetMapping("/loginpage")
    public String loginPage(){
        return "/login";
    }

}

entity


import lombok.Data;

@Data
public class User {

    private String userName;
    private String userPassword;
    private int delFlag;
    private int id;
    private String sysToken;
}

mapper


import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
public interface UserMapper {
    User login(User user);

    void add(User user);

    void upd(User user);

    void del(int id);

    List<User> list();

    User findByid(int id);
}

service 及实现


import java.util.List;

public interface IUserService {

    boolean login(User user);

    void save(User user);

    void upd(User user);

    void del(int id);

    List<User> list();

    User findByid(int id);
}


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

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public boolean login(User user) {
        try {
            User loginUser = mapper.login(user);
            if(loginUser != null){
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void save(User user) {
        mapper.add(user);
    }

    @Override
    public void upd(User user) {
        mapper.upd(user);
    }

    @Override
    public void del(int id) {
        mapper.del(id);
    }

    @Override
    public List<User> list() {
        return mapper.list();
    }

    @Override
    public User findByid(int id) {
        return mapper.findByid(id);
    }

}

applicati.xml

#端口号设置
server.port=10086
#数据库连接
spring.datasource.url=jdbc:sqlserver://IP:端口;DatabaseName=数据库名
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.username=账号
spring.datasource.password=密码
#连接池配置
spring.datasource.initialSize=5
spring.datasource.maxActive=50
spring.datasource.minIdle=5
#连接等待超时时间
spring.datasource.maxWait=60000

mybatis.type-aliases-package=USER类完全限定名

mybatis.mapper-locations=classpath:mapper/*.xml
实体类包名 (例子项目的包名是com.shizhao.project.springdemo.model)
spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp

mapper.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="cn.hft.user.mapper.UserMapper" >
    <resultMap id="BaseResultMap" type="返回类型完全限定名" >
        <!--@mbggenerated-->
        <id column="ID" property="id" jdbcType="INTEGER" />
        <result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
        <result column="USER_PASSWORD" property="userPassword" jdbcType="VARCHAR" />
        <result column="DEL_FLAG" property="delFlag" jdbcType="INTEGER" />
        <result column="SYS_TOKEN" property="sysToken" jdbcType="VARCHAR" />
    </resultMap>
    <sql id="Base_User_List" >
        <!--@mbggenerated-->
        ID, USER_NAME, USER_PASSWORD, DEL_FLAG, SYS_TOKEN
    </sql>

    <select id="login" resultType="返回类型完全限定名" parameterType="传入参数类型完全限定名" >
        <!--@mbggenerated-->
        select
        <include refid="Base_User_List" />
        from TEST_USER
        where USER_NAME = #{userName} and USER_PASSWORD = #{userPassword}
    </select>

    <select id="list" resultMap="BaseResultMap" >
        <!--@mbggenerated-->
        select
        <include refid="Base_User_List" />
        from TEST_USER
    </select>

    <select id="findByid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        <!--@mbggenerated-->
        select
        <include refid="Base_User_List" />
        from TEST_USER
        where id = #{id}
    </select>

    <delete id="del" parameterType="java.lang.Integer" >
        <!--@mbggenerated-->
        delete from TEST_USER
        where ID = #{id}
    </delete>

    <insert id="add" parameterType="传入参数类型完全限定名" >
        insert into TEST_USER
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="userName != null" >
                USER_NAME,
            </if>
            <if test="userPassword != null" >
                USER_PASSWORD,
            </if>
            <if test="delFlag != null" >
                DEL_FLAG,
            </if>
            <if test="sysToken != null" >
                SYS_TOKEN,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="userName != null" >
                #{userName},
            </if>
            <if test="userPassword != null" >
                #{userPassword},
            </if>
            <if test="delFlag != null" >
                #{delFlag},
            </if>
            <if test="sysToken != null" >
                #{sysToken},
            </if>
        </trim>
    </insert>


    <update id="upd" parameterType="传入参数类型完全限定名" >
        <!--@mbggenerated-->
        update TEST_USER
        <set >
            <if test="userName != null" >
                USER_NAME = #{userName},
            </if>
            <if test="userPassword != null" >
                USER_PASSWORD = #{userPassword},
            </if>
            <if test="delFlag != null" >
                DEL_FLAG = #{delFlag},
            </if>
            <if test="sysToken != null" >
                SYS_TOKEN = #{sysToken},
            </if>
        </set>
        where ID = #{id}
    </update>
</mapper>

前端:login.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/4/19 0019
  Time: 9:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%request.setAttribute("pn", pageContext.getServletContext().getContextPath());%>
<html>
<head>
    <title>UserTest</title>
</head>
<body>
<form name="base" method="post" action="${pn}/user/login">
    <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td height="100%" align="center"><br><br>
                <table width="200" border="0" cellpadding="0" cellspacing="0" class="loginTable">
                    <caption><span style="color: red;">${hasErr}</span></h2></caption>
                    <tr>
                        <td>&nbsp;&nbsp;户:</td>
                        <td><input type="text" name="userName" style="width:100px"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;&nbsp;码:</td>
                        <td><input name="userPassword" type="password" style="width:100px"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td><button type="submit" name="Submit">登录</button></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

userAdd.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/29
  Time: 12:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/user/save" method="post">
    userName:<input type="text" name="userName"/><br/>
    userPassword:<input type="text" name="userPassword"/><br/>
    <input type="submit" name="提交"/>
</form>
</body>
</html>

userEdit.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/29
  Time: 12:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/user/save" method="post">
    ID:<input type="text" name="id" value="${user.id}" readonly/><br/>
    userName:<input type="text" name="userName" value="${user.userName}"/><br/>
    userPassword:<input type="text" name="userPassword" value="${user.userPassword}"/><br/>
    <input type="submit" name="提交"/>
</form>
</body>
</html>

userList.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/29
  Time: 10:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%request.setAttribute("pn", pageContext.getServletContext().getContextPath());%>
<html>
<head>
    <title>userList</title>
</head>
<body>
<h1>用户信息</h1>
<table>
    <tr>
        <th>用户编号</th>
        <th>用户姓名</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${List}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.userName}</td>
            <td>
                <a href="/user/findById/${user.id}">修改</a>
                <a href="/user/del/${user.id}">删除</a>
            </td>
        </tr>
    </c:forEach>
</table>
<a href="${pn}/user/add">添加用户</a>
</body>
</html>

最后

以上就是开放荔枝为你收集整理的SpringBoot,Maven集成JSP简单实现用户登录,列表增删改查的全部内容,希望文章能够帮你解决SpringBoot,Maven集成JSP简单实现用户登录,列表增删改查所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部