文章目录

SSM框架搭建

  1. 修改pom.xml文件,添加项目所需要的依赖

  2. 修改项目结构,如下图所示:

  3. 在bean目录下新建User类:

    package com.zhongruan.bean;
    
    public class User {
    
        private int id;
        private String username;
        private String password;
    
        public User() {}
    
        public User(int id, String username, String password) {
            this.id = id;
            this.username = username;
            this.password = password;
        }
    
        public User(String username, String password) {
            this.username = username;
            this.password = password;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int 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;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    
    }
    
  4. 在dao目录下新建IUserDao接口:

    package com.zhongruan.dao;
    
    import com.zhongruan.bean.User;
    
    import java.util.List;
    
    public interface IUserDao {
    
        public List<User> searchAllUsers();
    
        public User searchUserById(int id);
    
        public boolean insertUser(User user);
    
        public boolean updateUser(User user);
    
        public boolean deleteUserById(int id);
    
    }
    
  5. 在service目录下新建IUserService接口:

    package com.zhongruan.service;
    
    import com.zhongruan.bean.User;
    
    import java.util.List;
    
    public interface IUserService {
    
        public List<User> searchAllUsers();
    
        public User searchUserById(int id);
    
        public boolean insertUser(User user);
    
        public boolean updateUser(User user);
    
        public boolean deleteUserById(int id);
    
    }
    
  6. 在mapper目录下新建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="searchAllUsers" resultType="com.zhongruan.bean.User">select * from tb_user;</select>
    
        <select id="searchUserById" resultType="com.zhongruan.bean.User">select * from tb_user where id = #{id};</select>
    
        <delete id="deleteUserById" >delete from tb_user where id = #{id};</delete>
    
        <update id="updateUser" parameterType="com.zhongruan.bean.User">update tb_user set username = #{username} , password = #{password} where id = #{id};</update>
    
        <insert id="insertUser" parameterType="com.zhongruan.bean.User">insert into tb_user(username, password) values (#{username}, #{password});</insert>
    
    </mapper>
    
  7. 在service目录下新建impl目录,在impl下新建IUserService接口的实现类UserServiceImpl

    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 UserServiceImpl implements IUserService {
    
        @Autowired
        IUserDao userDao;
    
        @Override
        public List<User> searchAllUsers() {
    
            return userDao.searchAllUsers();
        }
    
        @Override
        public User searchUserById(int id) {
    
            return userDao.searchUserById(id);
        }
    
        @Override
        public boolean insertUser(User user) {
    
            return userDao.insertUser(user);
        }
    
        @Override
        public boolean updateUser(User user) {
    
            return userDao.updateUser(user);
        }
    
        @Override
        public boolean deleteUserById(int id) {
    
            return userDao.deleteUserById(id);
        }
    
    }
    
  8. 在resources目录下新建db.properties文件

   jdbc.driver=com.mysql.cj.jdbc.Driver
   jdbc.url=jdbc:mysql://localhost:3306/db_test?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8
   jdbc.username=root
   jdbc.password=123456
  1. 在resources目录下新建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"/>
            <!--配置加载映射文件 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/>
    
    </beans>
    
  2. 在resources目录下新建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">
            <property name="prefix" value="/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
    
  3. 在webapp中创建如下目录结构:

  4. 修改index.jsp

    <%@page contentType="text/html;UTF-8" pageEncoding="UTF-8" %>
    <%
        pageContext.setAttribute("path", request.getContextPath());
    %>
    <html>
    <head>
        <title>首页</title>
    </head>
    <body>
        <h2>Hello World!</h2>
        <hr>
        <a href="${path}/user/searchAllUser.do">点击进入查找用户</a>
    </body>
    </html>
    
    
  5. 在controller目录下创建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.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import java.util.List;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private IUserService userService;
    
        @RequestMapping("/searchAllUser.do")
        public ModelAndView searchAllUser() {
    
            List<User> userList = userService.searchAllUsers();
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("users", userList);
            modelAndView.setViewName("allUser");
            return modelAndView;
        }
    
        @RequestMapping("/toAddUser.do")
        public String toAddUser() {
    
            return "addUser";
        }
    
        @RequestMapping("/addUser.do")
        public ModelAndView addUser(User user) {
    
            ModelAndView modelAndView = new ModelAndView();
            userService.insertUser(user);
            modelAndView.setViewName("redirect:/user/searchAllUser.do");
            return modelAndView;
        }
    
        @RequestMapping("/delete.do")
        public String deleteUser(@PathVariable() int id) {
    
            userService.deleteUserById(id);
            return "redirect:/user/searchAllUser.do";
        }
    
        @RequestMapping("/toUpdate.do")
        public String toUpdateUser(Model model, int id) {
    
            model.addAttribute("user", userService.searchUserById(id));
            return "updateUser";
        }
    
        @RequestMapping("/update.do")
        public ModelAndView updateUser(User user) {
    
            ModelAndView modelAndView = new ModelAndView();
            userService.updateUser(user);
            modelAndView.setViewName("redirect:/user/searchAllUser.do");
            return modelAndView;
        }
    
    }
    
  6. 编辑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>
                        基于SSM框架的管理系统:简单实现增、删、改、查。
                    </h1>
                </div>
            </div>
        </div>
    
        <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="${pageContext.request.contextPath}/user/toAddUser.do">新增</a>
            </div>
        </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="userInfo">
                        <tr>
                            <td>${userInfo.id}</td>
                            <td>${userInfo.username}</td>
                            <td>${userInfo.password}</td>
                            <td>
                                <a href="${pageContext.request.contextPath}/user/toUpdate.do?id=${userInfo.id}">更改</a> |
                                <a href="${pageContext.request.contextPath}/user/delete.do?id=${userInfo.id}">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    
  7. 编辑addUser.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>新增用户</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>
                        基于SSM框架的管理系统:简单实现增、删、改、查。
                    </h1>
                </div>
            </div>
        </div>
    
        <div class="row clearfix">
            <div class="col-md-12 column">
                <div class="page-header">
                    <h1>
                        <small>新增用户</small>
                    </h1>
                </div>
            </div>
        </div>
        <form action="${pageContext.request.contextPath}/user/addUser.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>
    
  8. 编辑updateUser.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>修改论文</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>
                        基于SSM框架的管理系统:简单实现增、删、改、查。
                    </h1>
                </div>
            </div>
        </div>
    
        <div class="row clearfix">
            <div class="col-md-12 column">
                <div class="page-header">
                    <h1>
                        <small>修改用户</small>
                    </h1>
                </div>
            </div>
        </div>
    
        <form action="${pageContext.request.contextPath}/user/update.do" method="post">
            <input type="hidden" name="id" value="${user.id}"/>
            用户姓名:<input type="text" name="username" value="${user.username}"/>
            用户密码:<input type="text" name="password" value="${user.password}"/>
            <input type="submit" value="提交" />
        </form>
    </div>
    
  9. 部署项目