springboot+mybatis实现登录注册

  • 项目目录结构
    springboot+mybatis实现登录注册_第1张图片
  • 添加好依赖


			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.2.0



			mysql
			mysql-connector-java
			5.1.41

  • 在application.properties中配置数据库信息和mybatis信息
spring.datasource.url=jdbc:mysql://119.29.168.212:3306/wang?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=wang
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


mybatis.type-type-aliases-package=com.example.demo.model
#mybatis.mapper-locations=classpath:mapper/UserMapper.xml

#配置静态资源访问路径

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/
    
  • 在UserDao.xml中配置实体类映射



    
        
        
        //字段名要和实体类中一致
        
        
        
    

//id的值要和实现的方法一致

    
    
        insert into user (username,password,id) values (#{username},#{password},#{id})
    

  • 在User.java中写实体类
package com.example.demo.model;

public class User {
    public String id;
    public String username;
	public String password;
	public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
	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;
	}
	public User() {
		super();
	}
	

}

  • UserController中实现登陆注册
package com.example.demo.controller;

import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.model.User;
import com.example.demo.service.UserService;


@Controller
@RequestMapping("/user")
public class Usercontroller {
    @Autowired
    UserService userService;
    
    
    
    @RequestMapping("/login")
    @ResponseBody
    public Object login(User user){
//        判断是否为空
        if(StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getPassword())) {
//           不为空,到user中查找
            User users = userService.loginQuery(user);
//           返回信息
            if(users != null) {
                System.out.println(users);
                return users;
            }else{
                return "fail";
            }
        }else {
            return "fail";
        }
    }
    @RequestMapping("/regist")
    @ResponseBody
    public Object insertQuery(User user) {
        if(StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getPassword())) {
            //随机生成32位码,用于逐主键,主键不为空
            user.setId(UUID.randomUUID().toString().replace("-", ""));
            int count = userService.insertQuery(user);
            if(count == 1) {
                return "success";
            }else {
               return "fail"; 
            }
        }else {
            return "fail"; 
        }
    }
    


}

  • UserDao.java 实现Dao层接口
    注意要写@Mapper注解
package com.example.demo.dao;

import org.apache.ibatis.annotations.Mapper;

import com.example.demo.model.User;

@Mapper
public interface UserDao {
//    登陆后显示所有用户信息
	public User loginQuery(User user);
//	插入一条用户信息,返回状态
	public int insertQuery(User user);

}

  • UserService接口和接口实现类UserServiceImpl
package com.example.demo.service;

import com.example.demo.model.User;

public interface UserService {

    User loginQuery(User user);

    int insertQuery(User user);
  
    

    

}

package com.example.demo.service;


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

import com.example.demo.dao.UserDao;
import com.example.demo.model.User;

@Service
public class USerServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
//登录
    @Override
    public User loginQuery(User user) {
//        返回dao层的查询结果
        return userDao.loginQuery(user);
    }
//    注册
    @Override
    public int insertQuery(User user) {
        return userDao.insertQuery(user);
    }

}

  • login.html




Insert title here


		
用户名
密   码
  • regist.html




Insert title here


        
用户名
密   码

静态资源,mybatis整合,springboot相关知识访问
https://blog.csdn.net/gsy_study/article/details/102840807.
注册:
springboot+mybatis实现登录注册_第2张图片
登录:
springboot+mybatis实现登录注册_第3张图片
springboot+mybatis实现登录注册_第4张图片

你可能感兴趣的:(登录注册)