ssm增删改查

项目结构如下
在这里插入图片描述
在这里插入图片描述
“bean”中“User”代码如下,并使用“Getter and Setter”和“toString”方法

	private int id;
    private String username;
    private String password;

“IUserDao”代码如下

package com.zhongruan.dao;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserDao {
    List<User> findAll();
    User selectByUserName(String name);
    void deleteById(int id);
    void insertById(User user);
    void updateById(User user);
}

“UserService”代码如下

package com.zhongruan.service.impl;

import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserService {
    @Autowired
    private IUserDao userDao;

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public Boolean login(String username, String password) {
        User user=userDao.selectByUserName(username);
        if (user!=null&&password.equals(user.getPassword())){
            return true;
        }
        return false;
    }

    @Override
    public void deleteById(int id) {
       userDao.deleteById(id);
    }

    @Override
    public void insertById(User user) {
        userDao.insertById(user);
    }

    @Override
    public void updateById(User user) {
        userDao.updateById(user);
    }
}

“IUserService”代码如下

package com.zhongruan.service;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserService {
    List<User> findAll();
    Boolean login(String username,String password);
    void deleteById(int id);
    void insertById(User user);
    void updateById(User user);
}

“UserController”代码如下

package com.zhongruan.controller;

import com.zhongruan.bean.User;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        List<User> users=userService.findAll();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("allUser.jsp");
        modelAndView.addObject("users",users);
        return modelAndView;
    }
    @RequestMapping("/login.do")
    public String login(User user){
        Boolean flag=userService.login(user.getUsername(),user.getPassword());
        if (flag){
            return "redirect:/findAll.do";
        }else {
            return "redirect:/failure.jsp";
        }

    }
    @RequestMapping("delete.do")
    public String deleteById(int id){
        userService.deleteById(id);
        return "redirect:/findAll.do";
    }
    @RequestMapping("insert.do")
    public String insertById(User user){
        userService.insertById(user);
        return "redirect:/findAll.do";
    }
    @RequestMapping("update.do")
    public String updateById(User user){
        userService.updateById(user);
        return "redirect:/findAll.do";
    }
}

“UserMapper.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="com.zhongruan.dao.IUserDao">
    <select id="findAll" resultType="com.zhongruan.bean.User">
        select * from tb_user
    </select>
    <select id="selectByUserName" parameterType="String" resultType="com.zhongruan.bean.User">
        select * from tb_user where username=#{username};
    </select>
   <delete id="deleteById" parameterType="int">
        delete from tb_user where id=#{id}
    </delete>
     <update id="updateById" parameterType="com.zhongruan.bean.User">
        update tb_user set username=#{username},password=#{password} where id=#{id}
    </update>
    <insert id="insertById" parameterType="com.zhongruan.bean.User">
        insert into tb_user(username,password) value (#{username},#{password})
    </insert>

</mapper>

“applicationContext.xml”代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!-- 1.配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 2.配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="2"/>
    </bean>

    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 扫描bean包 使用别名 -->
        <property name="typeAliasesPackage" value="com.zhongruan.bean"></property>

        <!--配置加载映射文件 UserMapper.xml-->
      <!--  <property name="mapperLocations" value="classpath:mapper/*.xml"/>-->

    </bean>

    <!-- 自动生成dao,mapper-->
    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 给出需要扫描Dao接口包 -->
        <property name="basePackage" value="com.zhongruan.dao"/>
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>



    <!--自动扫描-->
    <context:component-scan base-package="com.zhongruan"/>


    <!-- 配置事务-->
    <!-- 5.配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 6.开启事务注解-->
    <tx:annotation-driven></tx:annotation-driven>

</beans>

“spring-mvc.xml”代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 1.注解扫描位置-->
    <context:component-scan base-package="com.zhongruan.controller" />

    <!-- 2.配置映射处理和适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>


    <!-- 3.视图的解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    </bean>
</beans>

“db.properties”代码如下

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/zjgm
jdbc.username=root
jdbc.password=123456

“log4j.properties”代码如下

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

“web.xml”代码如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <display-name>Archetype Created Web Application</display-name>

  <!-- 配置加载类路径的配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>

  <!-- 配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <!-- 前端控制器(加载classpath:spring-mvc.xml 服务器启动创建servlet) -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

“allUser.jsp”代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>user列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>员工列表 —— 显示所有员工</small>
                </h1>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 column">
            <a class="btn btn-primary" href="insertUser.jsp">新增</a>
            <%--<a class="btn btn-primary" href="/findgood">商品列表</a>--%>
        </div>
        <form action="/findbyupage" method="post">
            <input name="searchname" type="text"/>
            <input type="submit" value="搜索"/>
        </form>
    </div>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>id</th>
                    <th>用户账号</th>
                    <th>密码</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${users}" var="user">
                    <tr>
                        <td>${user.id}</td>
                        <td>${user.username}</td>
                        <td>${user.password}</td>
                        <td>
                            <a class="btn btn-primary" href="/delete.do?id=${user.id}">删除</a>
                            <a class="btn btn-primary" href="/updateUser.jsp?id=${user.id}">修改</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
    <div align="center">
        <a href="/findbyupage?currentPage=1&size=5">首页</a>

        <c:if test="${pageInfo.currentPage==1}">
            <a href="/findbyupage?currentPage=1&size=5">上一页</a>
        </c:if>
        <c:if test="${pageInfo.currentPage>1}">
            <a href="/findbyupage?currentPage=${pageInfo.currentPage-1}&size=5">上一页</a>
        </c:if>

        <c:forEach begin="1" end="${pageInfo.totalPage}" var="i">
            <a href="/findbyupage?currentPage=${i}&size=5">${i}</a>
        </c:forEach>

        <c:if test="${pageInfo.currentPage==pageInfo.totalPage}">
            <a href="/findbyupage?currentPage=${pageInfo.totalPage}&size=5">下一页</a>
        </c:if>
        <c:if test="${pageInfo.currentPage<pageInfo.totalPage}">
            <a href="/findbyupage?currentPage=${pageInfo.currentPage+1}&size=5">下一页</a>
        </c:if>

        <a href="/findbyupage?currentPage=${pageInfo.totalPage}&size=5">尾页</a>
    </div>
</div>

“failure.jsp”代码如下

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/25
  Time: 11: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>
登录失败
</body>
</html>

“index.jsp”代码如下

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/25
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>login</title>
  <link rel="stylesheet" type="text/css" href="css/normalize.css" />
  <link rel="stylesheet" type="text/css" href="css/demo.css" />
  <!--必要样式-->
  <link rel="stylesheet" type="text/css" href="css/component.css" />
  <!--[if IE]>
  <script src="js/html5.js"></script>
  <![endif]-->
</head>
<body>
<div class="container demo-1">
  <div class="content">
    <div id="large-header" class="large-header">
      <canvas id="demo-canvas"></canvas>
      <div class="logo_box">
        <h3>welcome</h3>
        <form action="/login.do" name="f" method="post">
          <div class="input_outer">
            <span class="u_user"></span>
            <input name="username" class="text" style="color: #FFFFFF !important" type="text" placeholder="请输入账户">
          </div>
          <div class="input_outer">
            <span class="us_uer"></span>
            <input name="password" class="text" style="color: #FFFFFF !important; position:absolute; z-index:100;"value="" type="password" placeholder="请输入密码">
          </div>
          <div class="mb2"><input class="act-but submit" style="color: #FFFFFF" type="submit" value="登录"></div>
        </form>
      </div>
    </div>
  </div>
</div><!-- /container -->
<script src="js/TweenLite.min.js"></script>
<script src="js/EasePack.min.js"></script>
<script src="js/rAF.js"></script>
<script src="js/demo-1.js"></script>
<div style="text-align:center;">
</div>
</body>
</html>

“insertUser.jsp”代码如下

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/25
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增用户</title>
    <link rel="stylesheet" type="text/css" href="css/normalize.css" />
    <link rel="stylesheet" type="text/css" href="css/demo.css" />
    <!--必要样式-->
    <link rel="stylesheet" type="text/css" href="css/component.css" />
    <!--[if IE]>
    <script src="js/html5.js"></script>
    <![endif]-->
</head>
<body>
<div class="container demo-1">
    <div class="content">
        <div id="large-header" class="large-header">
            <canvas id="demo-canvas"></canvas>
            <div class="logo_box">
                <div class="container" align="center">
                    <div class="row clearfix">
                        <div class="col-md-12 column">
                            <div class="page-header">
                                <h1>
                                    <small>新增用户</small>
                                </h1>
                            </div>
                        </div>
                    </div>
                    <form action="/insert.do" method="post">
                        <!--用&nbsp;&nbsp;户&nbsp;&nbsp;id:<input type="text" name="id"><br><br><br>-->
                        用户姓名:<input type="text" name="username"><br><br><br>
                        用户密码:<input type="text" name="password"><br><br><br>
                        <input type="submit" value="添加" >
                    </form>
                </div>
            </div>
        </div>
    </div>
</div><!-- /container -->
<script src="js/TweenLite.min.js"></script>
<script src="js/EasePack.min.js"></script>
<script src="js/rAF.js"></script>
<script src="js/demo-1.js"></script>
<div style="text-align:center;">
</div>
</body>
</html>
</body>
</html>

“updateUser.jsp”代码如下

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/9/25
  Time: 10:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改用户</title>
    <link rel="stylesheet" type="text/css" href="css/normalize.css" />
    <link rel="stylesheet" type="text/css" href="css/demo.css" />
    <!--必要样式-->
    <link rel="stylesheet" type="text/css" href="css/component.css" />
    <!--[if IE]>
    <script src="js/html5.js"></script>
    <![endif]-->
</head>
<body>
<div class="container demo-1">
    <div class="content">
        <div id="large-header" class="large-header">
            <canvas id="demo-canvas"></canvas>
            <div class="logo_box">
                <div class="container" align="center">
                    <div class="row clearfix">
                        <div class="col-md-12 column">
                            <div class="page-header">
                                <h1>
                                    <small>修改用户</small>
                                </h1>
                            </div>
                        </div>
                    </div>
                    <form action="/update.do" method="post">
                        <input type="text" name="id" value="${user.id}"><br><br><br>
                        新的用户姓名:<input type="text" name="username" value="${user.username}"><br><br><br>
                        新的用户密码:<input type="text" name="password" value="${user.password}"><br><br><br>
                        <input type="submit" value="修改" >
                    </form>
                </div>
            </div>
        </div>
    </div>
</div><!-- /container -->
<script src="js/TweenLite.min.js"></script>
<script src="js/EasePack.min.js"></script>
<script src="js/rAF.js"></script>
<script src="js/demo-1.js"></script>
<div style="text-align:center;">
</div>
</body>
</html>
</body>
</html>

实现效果
登录
在这里插入图片描述
列表页
在这里插入图片描述
新增页
在这里插入图片描述
修改页
在这里插入图片描述
点击“删除”直接删除该行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值