• 增删改查的SSM小项目


        经过将近一个月的摸索,终于算是勉强完成了关于增删改查的SSM项目。

      github源码地址:https://github.com/123456abcdefg/Login

      好了,话不多说,写一下具体代码。

      

     实现代码:

     1.controller:

    登录:

    package com.controller;
    import java.util.List;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import com.entity.user;
    import com.service.userService;
    
    @Controller
    @RequestMapping("/admin")
    
    public class userController {
      // 注入UserService
      @Autowired
      private userService userService;
    
      @RequestMapping("/index")
      public String index(){
        return "index";
      }
      
      // 登录
      @RequestMapping("/login")
      public String login(HttpServletRequest request,String username,String password){
        user user =userService.getUser(username,password);
        ModelAndView modelandview = new ModelAndView();
        modelandview.addObject(user);
        if(user.getIdent().equals("1"))
          return "redirect:/admin/user/userlist.action";
        else
          return "redirect:/admin/index.action";
      }
    
      @RequestMapping("/error")
      public String error(){
        return "error";
      }
    }
    userlistController:
    package com.controller;
    import java.util.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    import com.entity.user;
    import com.service.userService;
    
    @Controller
    @RequestMapping("/admin/user")
    
    public class userlistController {
      // 注入UserService
      @Autowired
      private userService userService;
      
      @RequestMapping("/userlist")
      public ModelAndView userlist(HttpServletRequest request,Integer search){
        user user = new user();
        user.setId(search);
        List<user> userlist = userService.findUserList(user);
        ModelAndView modelandview = new ModelAndView();
        modelandview.addObject("userlist",userlist);
        modelandview.setViewName("login");
        return modelandview;
      }
          
      //新增页面
      @RequestMapping("/add")
      public String add(HttpServletRequest request){
        return "add_do"; 
      }
    
      //增加用户
      @RequestMapping("/add_do")
      public String add_do(HttpServletRequest request,
        String username,String password,String ident){
        user user = new user();
        user.setUsername(username);
        user.setPassword(password);
        user.setIdent(ident);
        userService.addUser(user);
        return  "forward:userlist.action";
      }
          
      //删除用户
      @RequestMapping("/delete_do")
      public String delete_do(HttpServletRequest request,Integer id){
        userService.deleteUser(id);
        return  "forward:userlist.action";
      }
          
      //修改页面
      @RequestMapping("/update")
      public ModelAndView update(Integer id){
        ModelAndView modelandview = new ModelAndView();
        user user=userService.getUserById(id);
        modelandview.addObject(user);
        modelandview.setViewName("add_do");
        return modelandview;
      }
      
      //修改用户信息
      @RequestMapping("/update_do")
      public String update_do(HttpServletRequest request,Integer id,String username,String password,String ident){
        user user = new user();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setIdent(ident);
        userService.updateUser(user);
        return  "forward:userlist.action";
      }
    }

    2.user:

    package com.entity;
    import java.util.List;
    
    public class user {
      private Integer id;
      private String username;
      private String password;
      private String ident;
      
      public String getIdent() {
        return ident;
      }
      public void setIdent(String ident) {
        this.ident = ident;
      }
      public Integer getId() {
        return id;
      }
      public void setId(Integer 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;
      }
    }

    3.mapper:

    package com.mapper;
    import java.util.List;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import com.entity.user;
    
    public interface userMapper {
      public user getUser(String username,String password);
      public List<user> findUserList(user user);
      public void addUser(user user);
      public void deleteUser(Integer id);
      public user getUserById(Integer id);
      public void updateUser(user user);
    }

     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="com.mapper.userMapper">
      <resultMap type="com.entity.user" id="userMapper">
        <id column="id" property="id" jdbcType="INTEGER"  />
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="ident" property="ident" jdbcType="VARCHAR"/>
      </resultMap>
      
      <select id="getUser" parameterType="com.entity.user" resultType="com.entity.user">
        select * 
        from user 
        where username=#{0} and password=#{1}
      </select>
      
      <select id="findUserList" parameterType="com.entity.user" resultType="com.entity.user">
        select * 
        from user
        where 1=1 and id like '%${id}%'
      </select>
      
      <insert id="addUser" parameterType="com.entity.user">
      insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
          <if test="id != null">
            id,
          </if>
          <if test="username != null">
            username,
          </if>
          <if test="password != null">
            password,
          </if>
          <if test="ident != null">
            ident,
          </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
          <if test="id != null">
            #{id,jdbcType=INTEGER},
          </if>
          <if test="username != null">
            #{username,jdbcType=VARCHAR},
          </if>
          <if test="password != null">
            #{password,jdbcType=VARCHAR},
          </if>
          <if test="ident != null">
            #{ident,jdbcType=VARCHAR},
          </if>
        </trim>
      </insert>
      
      <delete id="deleteUser" parameterType="com.entity.user">
        delete 
        from user
        where id=#{id,jdbcType=INTEGER}
      </delete>
      
      <select id="getUserById" parameterType="com.entity.user" resultType="com.entity.user">
        select *
        from user
        where id=#{id}
      </select>
      
      <update id="updateUser" parameterType="com.entity.user">
        update user
        <set>
          <if test="username != null">
            username = #{username,jdbcType=VARCHAR},
          </if>
          <if test="password != null">
            password = #{password,jdbcType=VARCHAR},
          </if>
          <if test="ident != null">
            ident = #{ident,jdbcType=VARCHAR},
          </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
      </update>
      
    </mapper>

    4.service:

    userService:

    package com.service;
    import java.util.List;
    import com.entity.user;
    
    public interface userService {
      public user getUser(String username,String password);
      public List<user> findUserList(user user);
      public void addUser(user user);
      public void deleteUser(Integer id);
      public user getUserById(Integer id);
      public void updateUser(user user);
    }

    userServiceimp:

    package com.service;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import com.entity.user;
    import com.mapper.userMapper;
    
    public class userServiceimp implements userService{
      // 注入User
      @Autowired
      private userMapper usermapper;
    
      @Override
      public user getUser(String username,String password){
        return usermapper.getUser(username,password);
      }
      
      @Override
      public List<user> findUserList(user user){
        return usermapper.findUserList(user);
      }
      
      @Override
      public void addUser(user user){
        usermapper.addUser(user); 
      }
      
      @Override
      public void deleteUser(Integer id){
        usermapper.deleteUser(id);
      }
      
      @Override
      public user getUserById(Integer id){
        return usermapper.getUserById(id);
      }
      
      @Override
      public void updateUser(user user){
        usermapper.updateUser(user);
      } 
    }

    剩下的配置文件我就不粘了,大家可以去我github上下载源码查看。

    登陆页面:  

    登陆成功页面: 

        新增 页面:

                                                     

    大致就是这样了,实现了基本的增删改查操作,以后要是写出好的项目也会同样放在github上,欢迎大家下载讨论。

    项目就先告一段落了,接下来的任务就是把SSM从头到尾看一遍,彻底搞懂SSM框架具体怎么实现。

     

  • 相关阅读:
    git学习(一)
    访问注解(annotation)的几种常见方法
    对于元数据的理解
    常用注解介绍
    深入理解Java的接口和抽象类
    POI依据类型设置导出格式
    java泛型
    java 向上转型和向下转型
    cell设置背景颜色为啥不起作用
    Java反射获取对象VO的属性值(通过Getter方法)
  • 原文地址:https://www.cnblogs.com/5201314m/p/9605760.html
Copyright © 2020-2023  润新知