springboot +bootstrap完成一个用户注册的功能

springboot +bootstrap完成一个用户注册的功能

一,基本项目的搭建(参考)
二.引入bootstrap
在这里插入图片描述
三.一般情况下,注册也只需要完成注册的功能即可,所以在这里,把注册信息和用户的基本资料信息分开,这里只需要关心注册的业务功能,所以新建了一张logininfo表,保存用户注册的信息
在这里插入图片描述
四.使用mybatis-generator完成domain,mapper,mapper.xml的创建
1.引入依赖

<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.21</version>
					</dependency>
				</dependencies>
				<configuration>
					<!--配置文件的路径-->
					<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
					<verbose>true</verbose>
					<overwrite>false</overwrite>
				</configuration>
			</plugin>

4.2相关代码
domain

@Setter@Getter@ToString
public class Logininfo extends BaseDomain {
    public static final int STATE_NOMAL=0;//正常状态
    public static final int STATE_LOCK=1;//锁定状态
    private String username;
    private String password;
    private Integer state=STATE_NOMAL;

}

mapper

public interface LogininfoMapper {

    int insert(Logininfo record);

    Logininfo selectByPrimaryKey(Long id);


    int updateByPrimaryKey(Logininfo record);
    /**
     * 根据用户名查询用户数量
     * @param username 用户名
     * @return
     */
    int selectByUsername(String username);  
}

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="core.base.mapper.LogininfoMapper" >
  <resultMap id="BaseResultMap" type="core.base.domain.Logininfo" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="state" property="state" jdbcType="BIT" />
  </resultMap>
  <insert id="insert"  useGeneratedKeys="true" keyProperty="id" >
    insert into logininfo (username, password, state)
    values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{state,jdbcType=BIT})
  </insert>
  <update id="updateByPrimaryKey"  >
    update logininfo
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      state = #{state,jdbcType=BIT},
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap"  >
    select id, username, password, state
    from logininfo
    where id = #{id,jdbcType=BIGINT}
  </select>
   <select id="selectByUsername" resultType="int">
    SELECT COUNT(id) from logininfo WHERE  username=#{username}
  </select>
</mapper>

service需要我们手动去创建,因为需要处理具体的业务逻辑,不能直接拷贝mapper中的方法,简单的逻辑如下

@Service
@Transactional
public class LogininfoServiceImpl implements ILogininfoService {
    @Resource
    private  LogininfoMapper logininfoMapper;
    @Override
    public void register(String username, String password) {
        //1.根据用户名判断用户是否存在
         int count = logininfoMapper.selectByUsername(username);
        //如果不存在,就插入数据库
        if(count<=0){
            Logininfo logininfo = new Logininfo();
            logininfo.setUsername(username);
            logininfo.setPassword(MD5.encode(password));
            logininfo.setState(Logininfo.STATE_NOMAL);
            logininfoMapper.insert(logininfo);
        }else{
            //存在就抛出异常
            throw new  RuntimeException();
        }
    }
}

controller

@Controller
public class LoginInfoController {
    @Autowired
    private ILogininfoService service;

    @RequestMapping("userRegister")
    @ResponseBody
    public AjaxResult register(String username, String password) {
        AjaxResult result = new AjaxResult();
        try {
            service.register(username, password);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }

工具类代码:
AjaxResult

@Setter@Getter@ToString@NoArgsConstructor@AllArgsConstructor
public class AjaxResult {
    private boolean success=true;
    private String msg;
    public void setMsg(String msg){
        this.success=false;
        this.msg=msg;
    }
}

MD5

public class MD5 {
	static final int S11 = 7;

	static final int S12 = 12;

	static final int S13 = 17;

	static final int S14 = 22;

	static final int S21 = 5;

	static final int S22 = 9;

	static final int S23 = 14;

	static final int S24 = 20;

	static final int S31 = 4;

	static final int S32 = 11;

	static final int S33 = 16;

	static final int S34 = 23;

	static final int S41 = 6;

	static final int S42 = 10;

	static final int S43 = 15;

	static final int S44 = 21;

	static final byte PADDING[] = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0 };

	public static long b2iu(byte b) {
		return (b >= 0 ? b : b & 0xff);
	}

	public static String byteHEX(byte ib) {
		char Digit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		char ob[] = new char[2];
		ob[0] = Digit[ib >>> 4 & 0xf];
		ob[1] = Digit[ib & 0xf];
		String s = new String(ob);
		return s;
	}

	public static String encode(String s) {
		MD5 m = new MD5();
		return m.getMD5ofStr(s);
	}

	private long state[];

	private long count[];

	private byte buffer[];

	public String digestHexStr;

	private byte digest[];

	public MD5() {
		state = new long[4];
		count = new long[2];
		buffer = new byte[64];
		digest = new byte[16];
		md5Init();
	}

	private void Decode(long output[], byte input[], int len) {
		int i = 0;
		for (int j = 0; j < len; j += 4) {
			output[i] = b2iu(input[j]) | b2iu(input[j + 1]) << 8
					| b2iu(input[j + 2]) << 16 | b2iu(input[j + 3]) << 24;
			i++;
		}

	}

	private void Encode(byte output[], long input[], int len) {
		int i = 0;
		for (int j = 0; j < len; j += 4) {
			output[j] = (byte) (int) (input[i] & 255L);
			output[j + 1] = (byte) (int) (input[i] >>> 8 & 255L);
			output[j + 2] = (byte) (int) (input[i] >>> 16 & 255L);
			output[j + 3] = (byte) (int) (input[i] >>> 24 & 255L);
			i++;
		}

	}

	private long F(long x, long y, long z) {
		return x & y | (x ^ 0L - 1L) & z;
	}

	private long FF(long a, long b, long c, long d, long x, long s, long ac) {
		a += F(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32 - s);
		a += b;
		return a;
	}

	private long G(long x, long y, long z) {
		return x & z | y & (z ^ 0L - 1L);
	}

	public String getMD5ofStr(String inbuf) {

		md5Init();

		md5Update(inbuf.getBytes(), inbuf.length());
		md5Final();
		digestHexStr = "";
		for (int i = 0; i < 16; i++)
			digestHexStr = String.valueOf(digestHexStr)
					+ String.valueOf(byteHEX(digest[i]));

		return digestHexStr;
	}

	private long GG(long a, long b, long c, long d, long x, long s, long ac) {
		a += G(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32 - s);
		a += b;
		return a;
	}

	private long H(long x, long y, long z) {
		return x ^ y ^ z;
	}

	private long HH(long a, long b, long c, long d, long x, long s, long ac) {
		a += H(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32 - s);
		a += b;
		return a;
	}

	private long I(long x, long y, long z) {
		return y ^ (x | z ^ 0L - 1L);
	}

	private long II(long a, long b, long c, long d, long x, long s, long ac) {
		a += I(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32 - s);
		a += b;
		return a;
	}

	private void md5Final() {
		byte bits[] = new byte[8];
		Encode(bits, count, 8);
		int index = (int) (count[0] >>> 3) & 0x3f;
		int padLen = index >= 56 ? 120 - index : 56 - index;
		md5Update(PADDING, padLen);
		md5Update(bits, 8);
		Encode(digest, state, 16);
	}

	private void md5Init() {
		count[0] = 0L;
		count[1] = 0L;
		state[0] = 0x67452301L;
		state[1] = 0xefcdab89L;
		state[2] = 0x98badcfeL;
		state[3] = 0x10325476L;
	}

	private void md5Memcpy(byte output[], byte input[], int outpos, int inpos,
			int len) {
		for (int i = 0; i < len; i++)
			output[outpos + i] = input[inpos + i];

	}

	private void md5Transform(byte block[]) {
		long a = state[0];
		long b = state[1];
		long c = state[2];
		long d = state[3];
		long x[] = new long[16];
		Decode(x, block, 64);
		a = FF(a, b, c, d, x[0], 7L, 0xd76aa478L);
		d = FF(d, a, b, c, x[1], 12L, 0xe8c7b756L);
		c = FF(c, d, a, b, x[2], 17L, 0x242070dbL);
		b = FF(b, c, d, a, x[3], 22L, 0xc1bdceeeL);
		a = FF(a, b, c, d, x[4], 7L, 0xf57c0fafL);
		d = FF(d, a, b, c, x[5], 12L, 0x4787c62aL);
		c = FF(c, d, a, b, x[6], 17L, 0xa8304613L);
		b = FF(b, c, d, a, x[7], 22L, 0xfd469501L);
		a = FF(a, b, c, d, x[8], 7L, 0x698098d8L);
		d = FF(d, a, b, c, x[9], 12L, 0x8b44f7afL);
		c = FF(c, d, a, b, x[10], 17L, 0xffff5bb1L);
		b = FF(b, c, d, a, x[11], 22L, 0x895cd7beL);
		a = FF(a, b, c, d, x[12], 7L, 0x6b901122L);
		d = FF(d, a, b, c, x[13], 12L, 0xfd987193L);
		c = FF(c, d, a, b, x[14], 17L, 0xa679438eL);
		b = FF(b, c, d, a, x[15], 22L, 0x49b40821L);
		a = GG(a, b, c, d, x[1], 5L, 0xf61e2562L);
		d = GG(d, a, b, c, x[6], 9L, 0xc040b340L);
		c = GG(c, d, a, b, x[11], 14L, 0x265e5a51L);
		b = GG(b, c, d, a, x[0], 20L, 0xe9b6c7aaL);
		a = GG(a, b, c, d, x[5], 5L, 0xd62f105dL);
		d = GG(d, a, b, c, x[10], 9L, 0x2441453L);
		c = GG(c, d, a, b, x[15], 14L, 0xd8a1e681L);
		b = GG(b, c, d, a, x[4], 20L, 0xe7d3fbc8L);
		a = GG(a, b, c, d, x[9], 5L, 0x21e1cde6L);
		d = GG(d, a, b, c, x[14], 9L, 0xc33707d6L);
		c = GG(c, d, a, b, x[3], 14L, 0xf4d50d87L);
		b = GG(b, c, d, a, x[8], 20L, 0x455a14edL);
		a = GG(a, b, c, d, x[13], 5L, 0xa9e3e905L);
		d = GG(d, a, b, c, x[2], 9L, 0xfcefa3f8L);
		c = GG(c, d, a, b, x[7], 14L, 0x676f02d9L);
		b = GG(b, c, d, a, x[12], 20L, 0x8d2a4c8aL);
		a = HH(a, b, c, d, x[5], 4L, 0xfffa3942L);
		d = HH(d, a, b, c, x[8], 11L, 0x8771f681L);
		c = HH(c, d, a, b, x[11], 16L, 0x6d9d6122L);
		b = HH(b, c, d, a, x[14], 23L, 0xfde5380cL);
		a = HH(a, b, c, d, x[1], 4L, 0xa4beea44L);
		d = HH(d, a, b, c, x[4], 11L, 0x4bdecfa9L);
		c = HH(c, d, a, b, x[7], 16L, 0xf6bb4b60L);
		b = HH(b, c, d, a, x[10], 23L, 0xbebfbc70L);
		a = HH(a, b, c, d, x[13], 4L, 0x289b7ec6L);
		d = HH(d, a, b, c, x[0], 11L, 0xeaa127faL);
		c = HH(c, d, a, b, x[3], 16L, 0xd4ef3085L);
		b = HH(b, c, d, a, x[6], 23L, 0x4881d05L);
		a = HH(a, b, c, d, x[9], 4L, 0xd9d4d039L);
		d = HH(d, a, b, c, x[12], 11L, 0xe6db99e5L);
		c = HH(c, d, a, b, x[15], 16L, 0x1fa27cf8L);
		b = HH(b, c, d, a, x[2], 23L, 0xc4ac5665L);
		a = II(a, b, c, d, x[0], 6L, 0xf4292244L);
		d = II(d, a, b, c, x[7], 10L, 0x432aff97L);
		c = II(c, d, a, b, x[14], 15L, 0xab9423a7L);
		b = II(b, c, d, a, x[5], 21L, 0xfc93a039L);
		a = II(a, b, c, d, x[12], 6L, 0x655b59c3L);
		d = II(d, a, b, c, x[3], 10L, 0x8f0ccc92L);
		c = II(c, d, a, b, x[10], 15L, 0xffeff47dL);
		b = II(b, c, d, a, x[1], 21L, 0x85845dd1L);
		a = II(a, b, c, d, x[8], 6L, 0x6fa87e4fL);
		d = II(d, a, b, c, x[15], 10L, 0xfe2ce6e0L);
		c = II(c, d, a, b, x[6], 15L, 0xa3014314L);
		b = II(b, c, d, a, x[13], 21L, 0x4e0811a1L);
		a = II(a, b, c, d, x[4], 6L, 0xf7537e82L);
		d = II(d, a, b, c, x[11], 10L, 0xbd3af235L);
		c = II(c, d, a, b, x[2], 15L, 0x2ad7d2bbL);
		b = II(b, c, d, a, x[9], 21L, 0xeb86d391L);
		state[0] += a;
		state[1] += b;
		state[2] += c;
		state[3] += d;
	}

	private void md5Update(byte inbuf[], int inputLen) {
		byte block[] = new byte[64];
		int index = (int) (count[0] >>> 3) & 0x3f;
		if ((count[0] += inputLen << 3) < (inputLen << 3))
			count[1]++;
		count[1] += inputLen >>> 29;
		int partLen = 64 - index;
		int i;
		if (inputLen >= partLen) {
			md5Memcpy(buffer, inbuf, index, 0, partLen);
			md5Transform(buffer);
			for (i = partLen; i + 63 < inputLen; i += 64) {
				md5Memcpy(block, inbuf, 0, i, 64);
				md5Transform(block);
			}

			index = 0;
		} else
			i = 0;
		md5Memcpy(buffer, inbuf, index, i, inputLen - i);
	}
	public static void main(String[] args) {
		System.out.println(MD5.encode("1"));
	}
}

五.这里在前台还需要做一些基本的验证,使用的是validate
代码如下:

<script type="text/javascript">
    $(function(){
        $("#registerForm").validate({
            rules: {
                "username": {
                    required: true,
                    minlength: 4,
                    maxlength: 16,
                    remote: {
                        url: "checkUsername",
                        type: "post",
                        data: {
                            username: function () {
                                return $("#username").val();
                            }
                        }
                    }
                },
                "password":{
                    required:true,
                    minlength:4,
                    maxlength:16
                },
                "confirmPwd":{
                    required:true,
                    equalTo:"#password"
                }
            },
            messages:{
                "username":{
                    required:"用户名不能为空",
                    minlength:"用户名最短 4 位",
                    maxlength:"用户名最长 16 位",
					remote:"用户名已经存在"
                },
                "password":{
                    required:"密码不能为空",
                    minlength:"密码最短 4 位",
                    maxlength:"密码最长 16 位"
                },
                "confirmPwd":"两次填写的密码不一致"
            },
            //自定义错误样式
            errorClass:"text-danger",
			//未通过验证,进行高亮处理或其他处理;
            highlight:function(input){
                $(input).closest(".form-group").addClass("has-error");
            },
			//通过验证,清除高亮效果或其他处理;
            unhighlight:function(input){
                $(input).closest(".form-group").removeClass("has-error");
            }
        });
    });
</script>

六.到这里基本的注册功能就完成了,效果如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
点击确定,即跳转到登录页面
七,到这里基本功能完成,后续又完善了一下,就是当用户在输入完用户名之后,就需要给用户提示信息,当前的用户名是否存在,而不是在提交的时候再去给用户显示"此用户已存在"
页面代码:

rules: {
                "username": {
                    required: true,
                    minlength: 4,
                    maxlength: 16,
                    remote: {
                        url: "checkUsername",
                        type: "post",
                        data: {
                            username: function () {
                                return $("#username").val();
                            }
                        }
                    }
                },

后台代码:
controller

  /**
     * 判断用户名是否存在
     * @param username 用户名
     * @return
     */
    @RequestMapping("checkUsername")
    @ResponseBody
    public boolean checkUsername(String username){
        //在页面上,存在是表示false,不存在是表示true,所以这里要取反
        return !service.checkUsername(username);
    }

service

/**
     * 检查用户名是否存在
     * true:存在
     * false:不存在
     * @param username
     * @return
     */
    boolean checkUsername(String username);

impl,直接使用上面的检查用户名是否存在的方法,用返回的值是否>0作判断即可

  @Override
    public boolean checkUsername(String username) {
        //根据用户名判断用户是否存在.返回的结果是一个int
        return logininfoMapper.selectByUsername(username)>0;
    }

效果如下:
在这里插入图片描述
至此,完成了注册的功能

最后,新建一个用户的基本信息表,在用户做注册操作的时候,同时完成基本信息表的初始化(这里主要只的是两张表中的id要一致)
代码很简单
在这里插入图片描述
在这里插入图片描述

发布了52 篇原创文章 · 获赞 2 · 访问量 204

猜你喜欢

转载自blog.csdn.net/weixin_41588751/article/details/104006081
今日推荐