亲宝软件园·资讯

展开

Spring5登录注册功能

用编程改改自己吧 人气:0

准备:

根据分析用户注册登录都需要的信息为
①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放入在这里就可以了。

加载全部内容

相关教程
猜你喜欢
用户评论