基于Spring5实现登录注册功能

本文实例为大家分享了Spring5实现登录注册功能的具体代码,供大家参考,具体内容如下

准备:

根据分析用户注册登录都需要的信息为
①username(String)
②userid(Int)
③userpassword(String)
④useremail(String)

1.生成数据库、表

2.编写实体类

import org.springframework.stereotype.Component;
@Component
public class User {
    private String UserName;
    private int UserId;
    private String UserPassWord;
    private String UserEmail;

    public User(String userName, int userId, String userPassWord, String userEmail) {
        UserName = userName;
        UserId = userId;
        UserPassWord = userPassWord;
        UserEmail = userEmail;
    }

    public User() {
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public int getUserId() {
        return UserId;
    }

    public void setUserId(int userId) {
        UserId = userId;
    }

    public String getUserPassWord() {
        return UserPassWord;
    }

    public void setUserPassWord(String userPassWord) {
        UserPassWord = userPassWord;
    }

    public String getUserEmail() {
        return UserEmail;
    }

    public void setUserEmail(String userEmail) {
        UserEmail = userEmail;
    }
}

3.配置xml文件(jdbcTemplate注入到dao层中)




    
    
    
    
        
        
        
        
    
    
    
        
        
    
    

4.编写Userdao(dao层)

import MyPackage.pojo.User;
import org.springframework.stereotype.Component;
@Component
public interface UserDao {
    void Register(User user);

    User login(Integer id);
}

UserDaoImpl类

ckage.dao;

import MyPackage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class UserDaoImpl implements UserDao{

    @Autowired
    JdbcTemplate jdbcTemplate;//自动注入数据库

    @Override
    public void Register(User user) {
        String sql = "insert into t_user (`username`,`userid`,`userpassword`,`useremail`)" +
                "values(?,?,?,?)";
        Object[] args = {user.getUserName(), user.getUserId(), user.getUserPassWord(), user.getUserEmail()};
        int update = jdbcTemplate.update(sql,args);
        System.out.println("注册成功:"+update);
    }

    @Override
    public User login(Integer id) {
        String sql = "select *from t_user where `userid`=?";
        User user1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(User.class), id);
        return user1;
    }
}

5.编写service层

import MyPackage.dao.UserDao;
import MyPackage.dao.UserDaoImpl;
import MyPackage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "userService")
public class UserService {
    @Autowired
    public UserDao userDao;
    //用户注册
    public void Register(User user){
        System.out.println("Service Register.....");
        userDao.Register(user);
    }

    //用户登录
    public void Login(User user){
        System.out.println("Service Login.....");
        User loginsuccess = userDao.login(user.getUserId());
        if (loginsuccess!=null)
            System.out.println("登录成功");
        else
            System.out.println("登录失败");
    }
}

6.测试

import MyPackage.pojo.User;
import MyPackage.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    //注册功能测试
    @org.junit.Test
    public void testJdbcTemplate1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = (UserService) context.getBean("userService", UserService.class);
//        userService.Register(new User("mary",2,"654321","[email protected]"));
        userService.Register(new User("lucy",1,"123456","[email protected]"));
    }
    //登录功能测试
    @org.junit.Test
    public void testJdbcTemplate2() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = (UserService) context.getBean("userService", UserService.class);
        userService.Login(new User("lucy",1,"123456","[email protected]"));
    }
}

运行截图

遇到的问题:

刚开始把bean1.xml配置文件直接是在src目录下生成的,所以当运行时,就会发现找不到xml文件
解决方法:

基于Spring5实现登录注册功能_第1张图片

在main目录下新建一个resources文件,且把该文件变成Resources类型,然后把bean1.xml放入在这里就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(基于Spring5实现登录注册功能)