SSH框架下的登录+增删查改

SSH框架下的登录+增删查改

又到了CSDN时间
今天学习了maven下的SSH框架的登录界面以及对数据库的增删查改
具体软件为:idea+maven+mysql

这是整体的工程构成
SSH框架下的登录+增删查改_第1张图片

按照上图创好文件夹和包后
第一步:从以前的项目中把各个配置文件(.xml)Copy过来
因为我发现了所有的SSM框架都大同小异
拷贝过来后,按照路径更改一下扫描的位置就ok了
第二步:先写dao层的接口,

package com.zhongruan.web_demo.dao;

import com.zhongruan.web_demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

public interface IUserDao {

    List getUsers();

//    User getUserById(String username);

    User getUserById2(String username);

    User getUserByName(@Param("name") String username);

    int updateUser(User user);

    int addUser(User user);

    int deleteUser(User user);
}

在mapper文件夹下的xml文件中实现




    
        insert into tb_user (username, password) values (#{username},#{password});
    
    
        update tb_user
        
            
                username = #{username},
            
            
                password = #{password},
            
        
        where username = #{username}
    
    
        delete from tb_user where username = #{username}
    

    
    
        
    
    
    



接着在Controller中加注解,声明跳转路径。(至于业务层,等实现了功能再配置,影响不大)

package com.zhongruan.web_demo.controller;

import com.zhongruan.web_demo.dao.IUserDao;
import com.zhongruan.web_demo.entity.User;
import com.zhongruan.web_demo.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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    IUserDao userDao;
    @Autowired
    IUserService UserService;

    @RequestMapping("/jsonTest")
    public String jump(){
        return null;
    }

    @RequestMapping("/demo")
    public String toDemo(Model model) {
        model.addAttribute("users", userDao.getUsers());
        return "user-list";
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    public String Login(Model model, HttpServletRequest request, User user) {
        User rightUser = userDao.getUserById2(user.getUsername());
        //判断是否存在用户
        if (rightUser != null) {
            model.addAttribute("msg", "不存在该用户");
            return "login";
        }
        if (rightUser.getPassword() != user.getPassword()) {
            model.addAttribute("msg", "账号或密码错误");
            return "login";
        }
        HttpSession session = request.getSession();
        session.setAttribute("user", rightUser);
        return "index";
    }
    @RequestMapping("/loginTest")
    public String loginTest(Model model,@RequestParam("username") String name,
                            @RequestParam("password") String pwd){
        User user=userDao.getUserByName(name);
        if (user == null){
            model.addAttribute("msg","不存在该用户");
            return "login";
        }
        if (!user.getPassword().equals(pwd)){
            model.addAttribute("msg","账号或密码错误~!");
            return "login";
        }
        return "index";
    }

    /**
     * mybatis增删改查方法测试
     * @param model
     * @return
     */
    @RequestMapping("/test")
    public String test(Model model) {
        //model.addAttribute()
        User user = new User();
        user.setUsername("tusuzer2");
        user.setPassword("123");
        userDao.addUser(user);
        System.out.println(user);
        user.setPassword("456");
        System.out.println(userDao.updateUser(user));
        return "login";
    }

}

对应登录接口的jsp页面:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>



    ${basePath}


账号:
密码:

${msg}

想要源码的同学私信我哟
不过为了保证顺利跑起来 最好保证版本跟我一致
我的版本号可以参考我第一天的博客~

你可能感兴趣的:(实习日记,ssh,登录界面,增删查改)