前言:

1:吐槽一下csdn上的环境,可以说用脏乱差来形容也是不为过了,除了大佬们以及一些良心博主写得真的很好,大部分就是断章取义的转载或者含糊不清的描述以及各种错误,杀人诛心的是不把源码给全,实在是让人头大。甚至我之前写过的一篇原创都被人家抄去收费栏目了。不过转念一想,如果初心是用这个来记录学习历程,就不想较真了,毕竟在入门的时候,csdn真的帮了我很多。说真的,我希望知识是无界的,所以我坚持我的原则,分享、开源、互相学习。环境更好的还是首推博客园。

2:这里只实现了简单的用户登录校验,至于其他功能,参考我的上一篇博文(67条消息) Spring boot 整合mybatis 对数据库进行增删改查操作——test类测试+实体类测试_爱吃卷心菜的搞笑男的博客-CSDN博客

 直接应用过来就好了。当然,实现的是一些基本功能,也不太成熟,仅仅用于记录我的入门历程并进行巩固。


本篇曾困扰我的知识点主要有:

Map<>类型的应用、Data数据类型格式的转化、端口访问的传参和后台接收、将数据存入redis、redis响应连接等、java的书写格式——驼峰

仍然存在的问题:

数据库的重复查询问题、端口返回数据格式和成功响应案例有出入的问题。


正文:

1:目录结构(和我的上篇文章基本一样,当然,和实际项目的出入还是很大的)

springboot实现用户管理_springboot实现用户管理


 

2:user层

package com.example.usermanager.user;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;

public class Info implements Serializable
{
    private Integer userId;
    private String userName;
    private String userPasswd;
    private String userSex;
    private Integer userAge;
    private String userAddress;
    private String token;

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date applyTime;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8",locale="zh")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date fixTime;

    public void setuserId(Integer userId)
    {
        this.userId=userId;
    }
    public Integer getuserId()
    {
        return this.userId;
    }

    public void setuserName(String userName)
    {
        this.userName=userName;
    }
    public String getuserName()
    {
        return this.userName;
    }
    public void setuserPasswd(String userPasswd)
    {
        this.userPasswd=userPasswd;
    }
    public String getuserPasswd()
    {
        return this.userPasswd;
    }

    public void setuserSex(String userSex)
    {
        this.userSex=userSex;
    }
    public String getUserSex()
    {
        return this.userSex;
    }

    public void setuserAge(Integer userAge)
    {
        this.userAge=userAge;
    }
    public Integer get()
    {
        return this.userAge;
    }
    public void setapplyTime(Date applyTime)
    {
        this.applyTime=applyTime;
    }
    public Date getapplyTime()
    {
        return this.applyTime;
    }

    public void setfixTime(Date fixTime)
    {
        this.fixTime=fixTime;
    }
    public Date getfixTime()
    {
        return this.fixTime;

    }
    public void setuserAddress(String userAddress)
    {
        this.userAddress=userAddress;
    }
    public String getuserAddress()
    {
        return this.userAddress;
    }
    public void settoken(String token)
    {
        this.token=token;
    }
    public String gettoken()
    {
        return this.token;
    }

}
package com.example.usermanager.user;

import java.util.Date;

public class loginjudge //用于端口显示的提示性信息
{
    private String msg;
    private Integer code;
    public void setcode(Integer code)
    {
        this.code=code;
    }
    public Integer getcode()
    {
        return this.code;
    }
    public void setmsg(String msg)
    {
        this.msg=msg;
    }
    public String getmsg()
    {
        return this.msg;
    }

}

1:在做完这个功能后我突然发现我之前的理解是有问题的,以为实体类里面的属性一定要完完全全的对应到数据库中的字段,当其实不然,他们之间没有必然的内在联系,甚至可以说是独立的,只是习惯里把这些趋于统一化;

2:其次在这里我遇到了一个问题,就是关于java的书写格式规范,只能说以前没有养成良好的习惯,接触的东西也太低级了,没在意,在这个地方卡了好久才就突然警醒了我,一定要规范,尤其是后面涉及到的.xml配置数据库语句,更是如此。


3:dao层

package com.example.usermanager.Dao;

import com.example.usermanager.user.Info;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;


@Mapper
public interface UserDao {
    public List<Info> findAllUser();
    public Info login(@Param("userName") String userName, @Param("userPasswd") String userPasswd);
}

4:resource下mappers的.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.example.usermanager.Dao.UserDao">
    <select id="findAllUser" resultType="Info">
        select * from info
    </select>
    <select id="login" resultType="Info">
        select userId, userName, userAddress, userAge, userSex, applyTime, fixTime from info
        <where>
            <if test="userName!=null and userName!=''">
                  and userName=#{userName}
            </if>
            <if test="userPasswd!=null and userPasswd!=''">
                and userPasswd=#{userPasswd}
            </if>
        </where>
    </select>
</mapper>

1:这两个部分联系极为紧密,根据我的个人习惯喜欢当作一个部分来处理

2:.xml文件特别要注意namespace后的地址和每个语句后面的返回类型,不然特别容易报错,一定要细心


5:service层

package com.example.usermanager.service;

import com.example.usermanager.user.Info;

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

public interface UserService {
    public List<Info> getUsers();
    public Info LoginJudge(Info info);


}
package com.example.usermanager.service.impl;

import com.example.usermanager.Dao.UserDao;
import com.example.usermanager.service.UserService;
import com.example.usermanager.user.Info;
import io.lettuce.core.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


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

@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserDao userDao;
    @Override
    public List<Info> getUsers()
    {// TODO Auto-generated method stub
        return userDao.findAllUser();
    }
    @Override
    public Info LoginJudge(Info info)
    {
       return userDao.login(info.getuserName(),info.getuserPasswd());
    }




}

6:controller层

package com.example.usermanager.controller;

import com.example.usermanager.service.UserService;
import com.example.usermanager.user.Info;
import com.example.usermanager.user.loginjudge;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;


@RestController
@RequestMapping("/info")
@ResponseBody
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;



    @RequestMapping("/judge")
    @ResponseBody
    public Map<String, Object> LoginJudge(HttpServletRequest request) {
        String userName = request.getParameter("userName");
        String userPasswd = request.getParameter("userPasswd");
        String token = request.getHeader("token");
        Map<String, Object> map = new HashMap<>();
        loginjudge user = new loginjudge();
        Info info = new Info();
        try {
            //连接数据库的驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "mima");
            PreparedStatement ps = conn.prepareStatement("select * from info where username=?");
            ps.setString(1, userName);
            ResultSet rs = ps.executeQuery();
            //查询的用户下面没有内容,返回用户不存在
            if (!rs.next()) {
                user.setmsg("登陆失败,用户不存在");
                user.setcode(404);
                map.put("code", user.getcode());
                map.put("msg", user.getmsg());
                map.put("data", null);
                throw new TimeoutException("登陆失败,用户不存在");
            }

            else if (!(rs.getString("userPasswd")).equals(userPasswd)) {
                user.setmsg("登陆失败,密码错误");
                user.setcode(404);
                map.put("code", user.getcode());
                map.put("msg", user.getmsg());
                map.put("data", null);
                throw new TimeoutException("登陆失败,密码错误");
            }
            else {
                user.setmsg("登录成功");
                user.setcode(1314);
                map.put("code", user.getcode());
                map.put("msg", user.getmsg());

                info.settoken(token);
                info.setapplyTime(rs.getDate("applyTime"));
                info.setfixTime(rs.getDate("fixTime"));
                info.setuserId(rs.getInt("userId"));
                info.setuserAddress(rs.getString("userAddress"));
                info.setuserSex(rs.getString("userSex"));
                info.setuserPasswd(rs.getString("userPasswd"));
                info.setuserName(rs.getString("userName"));
                map.put("data", info);

                //redis
                redisTemplate.opsForHash().putAll("data", map);
                System.out.println("存入redis成功");
            }
            conn.close();
            ps.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}

1:可能有人在存redis的时候会遇到报null的异常,这个时候一下上面的属性有没有缺少注解,应为多个属性不能公用一个注解,其他的也是同理;

2:其次是有一个拒绝连接的异常,这个时候基本就是你没有把本地的redis启动,去上网抗议下如何启动redis就ok;

3:可能有人发现了我在这个里面又连接了一次数据库,这里就是我之说的问题,重复连接了,我在配置文件已经连接过了,可是在这里又连接了一次,有冗余的嫌疑,暂时还没有想到如何解决,有知道的朋友可以评论里告知一下。


 7:.properties配置文件

server.port= 8080

spring.datasource.url= jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=jytyyds123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.type-aliases-package=com.example.usermanager.user
mybatis.mapper-locations= classpath:mappers/*.xml
mybatis.configuration.call-setters-on-nulls=true
mybatis.configuration.map-underscore-to-camel-case=true

spring.redis.host=127.0.0.1
spring.redis.port=6379

这里没什么好说的,注意地址就行,其次是redis有密码一定要写,没有密码就不要写了


 8:结果:

springboot实现用户管理_springboot实现用户管理_02

token是再header里面传

再看一下成功响应案例:

springboot实现用户管理_springboot实现用户管理_03

对比之下发现token其实是不太对劲的,应为我是把token存入了Info 对象中作为属性来写的,但是我看到好多都不是这样的,这个点我不太会,有会的朋友还请评论里交流一下。

本篇就到这里了,我会基于现有的基础和问题继续进行学习和摸索,特别感谢一直指导我实习的同事。