二、第一次整合

第一次整合

pojo

package com.lxj.pojo;

public class User {

    private String username;

    private String password;

    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;
    }
}

UserMapper

package com.lxj.mapper;

import com.lxj.pojo.User;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {

    User select(User user);

    int delete(@Param("username") String username);
}

UserController

package com.lxj.controller;

import com.lxj.pojo.User;
import com.lxj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session){
        System.out.println("進入控制器");
        //數(shù)據(jù)庫檢查
        user = userService.get(user);
        System.out.println("login..");
        if(user != null){

            session.setAttribute("SESSION_USER",user);
            //將用戶信息存儲到會話中
            return "user";
        }else{
            return "redirect:/login.jsp";
        }


    }

    @RequestMapping("/delete/{username}")
    @ResponseBody
    public Map<String, Integer> delete(@PathVariable("username")String username){
        System.out.println("delete..");
        Map<String ,Integer> map = new HashMap<>();

        boolean deleteSuc = userService.delete(username);
        Integer code = deleteSuc ? 2000:2003;
        map.put("code",code);
        return map;
    }
}

service接口

package com.lxj.service;

import com.lxj.pojo.User;

public interface UserService {

    /**
     * 根據(jù)user信息去檢查數(shù)據(jù)庫是否存在該用戶
     * @param user
     * @return
     */
    User get(User user);

    /**
     *
     * @param username
     * @return
     */
    boolean delete(String username);
}

service實現(xiàn)類

package com.lxj.service.impl;

import com.lxj.mapper.UserMapper;
import com.lxj.pojo.User;
import com.lxj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {

    //注入mapper
    @Autowired
    private UserMapper userMapper;

    @Override
    public User get(User user) {
        //提前檢查是否為空


        return userMapper.select(user);
    }

    @Override
    public boolean delete(String username) {

        int row = userMapper.delete(username);
        return row == 1 ? true:false;
    }
}

UserMapper映射文件

<?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.lxj.mapper.UserMapper">

    <select id="select" resultType="com.lxj.pojo.User">
        select * from t_user
        <where>
          <if test="username != null">
              and username = #{username}
          </if>
          <if test="password != null">
              and password = #{password}
          </if>
        </where>
    </select>
    
    <delete id="delete">
        delete from t_user
        <where>
            <if test="username != null">
                and username = #{username}
            </if>
        </where>
    </delete>

</mapper>
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容