首頁 > 軟體

基於Spring5實現登入註冊功能

2022-09-06 18:06:52

本文範例為大家分享了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層中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--掃描包-->
    <context:component-scan base-package="MyPackage"></context:component-scan>
    <!--資料庫連線池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/review"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    </bean>
    <!--設定JdbcTemplate物件,把資料庫dataSource注入進去-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--要把資料庫注入到JdbcTemplate物件中-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--到了這一步我們就連線好了資料庫了-->
</beans>

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>(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","654321@qq.com"));
        userService.Register(new User("lucy",1,"123456","123456@qq.com"));
    }
    //登入功能測試
    @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","123456@qq.com"));
    }
}

執行截圖

遇到的問題:

剛開始把bean1.xml組態檔直接是在src目錄下生成的,所以當執行時,就會發現找不到xml檔案
解決方法:

在main目錄下新建一個resources檔案,且把該檔案變成Resources型別,然後把bean1.xml放入在這裡就可以了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com