用户登录相关功能实现

controller层

package com.zh.controller;

import com.zh.model.User;
import com.zh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/room/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public int addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public User login(@RequestBody Map<String, Object> params ){
        String username = params.get("username").toString();
        String password = params.get("password").toString();
        return userService.login(username,password);
}

    @ResponseBody
    @RequestMapping("/showall")
    public Map<String, Object> showAll(){
        return userService.showAll();
    }

    @ResponseBody
    @RequestMapping("/delete")
    public int delete(int id){
        return userService.delete(id);
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public int update(@RequestBody User user){
        return userService.update(user);
    }
}

service层

package com.zh.service;

import com.zh.model.User;

import java.util.List;
import java.util.Map;

public interface UserService {
    int addUser(User user);
    User login(String username,String password);
    Map<String,Object> showAll();
    int update(User user);
    int delete(int id);

}

service实现

package com.zh.service.impl;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    //用户注册
    public int addUser(User user) {
        List<Integer> userList = userMapper.allUsername();
        if (userList.indexOf(user.getUsername()) < 0) {
            return userMapper.insert(user);
        } else {
            return -1;
        }
    }

    @Override
    //用户登录
    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    @Override
    //返回所有用户信息
    public Map<String, Object> showAll() {
        List<User> userList = userMapper.showAll();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("data",userList);
        dataMap.put("state","success");
        return dataMap;
    }

    @Override
    //用户信息更新
    public int update(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    //删除用户
    public int delete(int id) {
        return userMapper.deleteByPrimaryKey(id);
    }
}

mapper

package com.zh.mapper;

import com.zh.model.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

    User login(@Param("username") String username,@Param("password") String password);

    List<User> showAll();

    List<Integer> allUsername();
}

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.zh.mapper.UserMapper">
  <resultMap id="BaseResultMap" type="com.zh.model.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="sex" property="sex" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="category" property="category" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, username, password, `name`, sex, email, category
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tb_user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <select id="allUsername" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    username
    from tb_user
  </select>
  <select id="login" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from tb_user
    where username = #{username} and password = #{password}
  </select>
  <select id="showAll" resultMap="BaseResultMap" parameterType="com.zh.model.User" >
    select
    <include refid="Base_Column_List" />
    from tb_user
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zh.model.User" >
    insert into tb_user (id, username, password, 
      `name`, sex, email, category
      )
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{category,jdbcType=INTEGER}
      )
  </insert>

  <insert id="insertSelective" parameterType="com.zh.model.User" >
    insert into tb_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="name != null" >
        `name`,
      </if>
      <if test="sex != null" >
        sex,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="category != null" >
        category,
      </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="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        #{sex,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="category != null" >
        #{category,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zh.model.User" >
    update tb_user
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        sex = #{sex,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="category != null" >
        category = #{category,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zh.model.User" >
    update tb_user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      `name` = #{name,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      category = #{category,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值