Java三层架构实现用户注册逻辑[示例]

    软件实际开发中,都是采用分层思想,解耦,且方便维护/扩展,并提高代码重用性,实例层序分层结构与包名对应如下:

    本示例程序环境:

        1. 基本信息:Spring框架的JDBCTeamplate模板 / 阿里的DruiDruid连接池 / mysql数据库及其驱动jar包 / JDK版本1.8

        2. 工具类: Druid连接池工具类

以下是源代码:

Dao层(持久层):

package com.wen.dao;

import com.wen.domain.User;
import com.wen.utils.DateSourceDruid;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
//持久层  一般方法功能单一,以供业务层组合使用
//主要与数据库进行交互
public class UserDao {
    //新建JDBCTemplate模板,用到Druid连接池工具类
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(DateSourceDruid.getDateSource());

    private String sql=null;//新建一个sql字符串变量

    public User findUser(String userName){
        User user=null;//初始化用户对象
        sql="select * from users where uname=?";//查找语句
        try {
            user=jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),userName);
        } catch (DataAccessException e) {//若报错则返回null,代表未找到
        }
        return user;
    }

    public int saveUser(String userName,String userPassword){
        int result=0;//初始化返回值
        sql="insert into users values(null,?,?);";//sql语句存入用户
        try {
            result = jdbcTemplate.update(sql, userName, userPassword);//存入并返回结果
        } catch (DataAccessException e) {//若保存失败则返回0
        }
        return result;
    }
}

Service层(业务层):

package com.wen.service;

import com.wen.dao.UserDao;
//业务层:主要对持久层功能进行组合,供表现层使用,并返回符合逻辑的结果
//后端程序员主要战场
public class UserService {
    private UserDao userDao=new UserDao();//新建持久层对象

    public boolean userIsexist(String username){//判断用户是否存在
        return userDao.findUser(username)!=null;
    }

    public boolean saveUser(String userName,String userPassword){//保存用户
        return userDao.saveUser(userName,userPassword)>0;
    }
}

Action层(变现层):

package com.wen.action;

import com.wen.service.UserService;
//变现层:接收客户端数据并返回结果
//提供一个简明的方法池,供使用
//实际开发中该层为程序入口,接收客户端请求并返回数据
public class UserAction {
    private UserService userService=new UserService();//新建业务层对象

    public boolean findUser(String userName){//查找用户
        return userService.userIsexist(userName);
    }

    public boolean saveUser(String userName,String userPassword){//保存用户
        return userService.saveUser(userName,userPassword);
    }
}

模拟用户界面(View):

package com.wen.view;

import com.wen.action.UserAction;

import java.util.Scanner;

public class UserView {
    /*
    web界面用控制台代替,主要展现注册逻辑和三层架构基本思想
    实际开发都是分层思想,解耦,且便于后期维护和增加功能,并提高下一层代码的复用性
     */
    public static void main(String[] args) {
        System.out.println("欢迎来到Xxx的世界");//欢迎语
        Scanner scanner = new Scanner(System.in);//
        UserAction userAction = new UserAction();//新建表现层对象

        while (true) {//循环输入注册名,直到注册名可用为止
            System.out.println("请输入您想要注册的用户名:");
            String userName = scanner.nextLine();//输入注册名
            //注册
            if (userAction.findUser(userName)) {//判断注册名是否可用
                System.out.println("用户名已被注册,请重新输入:");
            } else {//若可用,开始你设置密码
                String userPassword;//以下用循环确认密码一致性,故该变量提出到循环外

                while (true) {
                    System.out.println("请输入注册密码");
                    userPassword = scanner.nextLine();
                    System.out.println("请确认密码:");
                    String repeat = scanner.nextLine();
                    if (repeat.equals(userPassword)) {
                        break;//若一致则退出循环开始注册
                    } else {
                        System.out.println("密码输入不一致,请再次输入");//否则再次输入密码并确认
                    }
                }
                boolean login = userAction.saveUser(userName, userPassword);//注册到数据库,并返回注册结果
                if (login) {
                    System.out.println("注册成功!");
                } else {
                    System.out.println("服务器忙,请稍后再试");
                }
                break;//注册完毕,退出外层循环
            }
        }
    }
}

用户实体类:

package com.wen.domain;

import java.util.Objects;

public class User {
    private int id;
    private String name;
    private int password;

    public User() {
    }

    public User(int id, String name, int password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPassword() {
        return password;
    }

    public void setPassword(int password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password=" + password +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id == user.id &&
                password == user.password &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, password);
    }
}

Druid工具类原码见上一篇博文:Spring框架中JdbcTemplate类的查表功能演示(基于Druid连接池)

本例运行结果逻辑展示如下:

欢迎来到Xxx的世界
请输入您想要注册的用户名:
黄晴
九月 15, 2018 3:12:20 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
用户名已被注册,请重新输入:
请输入您想要注册的用户名:
小文
请输入注册密码
12345
请确认密码:
123456
密码输入不一致,请再次输入
请输入注册密码
12345
请确认密码:
12345
注册成功!

猜你喜欢

转载自blog.csdn.net/weixin_42711325/article/details/82714603