【SpringMVC】使用三层架构实现登录,注册。(下篇)

上篇写了构思与界面层,本篇写一下业务逻辑层、数据访问层

目录

【SpringMVC】使用三层架构实现登录,注册。(下篇)_第1张图片

业务逻辑层

包:pojo
用户类(JavaBean):User

public class User {
    private String userName;
    private String userPwd;

    public User(String userName, String userPwd) {
        this.userName = userName;
        this.userPwd = userPwd;
    }

    public User() {
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }
}

包:service
接口:IUserService

public interface IUserService {
    public boolean register(User user);
    public boolean login(User user);
}

类:UserServiceImpl

public class UserServiceImpl implements IUserService {
    @Override
    public boolean register(User user) {
        IUserDao dao=new UserDaoImpl();
        User userByName = dao.findUserByName(user);
        if(userByName==null){
            //如果数据库里没有找到同名的,就注册
            try {
                int i = dao.addUser(user);
                if(i>0){
                    System.out.println("注册成功");
                }else {
                    return false;
                }
                return true;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean login(User user) {
        IUserDao dao=new UserDaoImpl();
        User checkUser = dao.checkUser(user);
        if(checkUser==null){
            //如果数据库里没有找到同名、同密码的
            System.out.println("登录失败");
            return false;
        }else {
            System.out.println("欢迎登录");
            return true;
        }
    }
}

数据访问层

包:dao
接口:IUserDao

public interface IUserDao {
    public User findUserByName(User u);
    public User checkUser(User u);//检查用户名密码是否与数据库中一致
    public int addUser(User u);//向数据库中添加用户信息
}

实现接口的类:UserDaoImpl

public class UserDaoImpl implements IUserDao{
    private JdbcTemplate jt=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public User findUserByName(User user) {
        User u=null;
        try{
            u=jt.queryForObject("select userName from userinfo where userName=?",
                    new BeanPropertyRowMapper<User>(User.class),user.getUserName());
        }catch (Exception e){
        }
        return u;
    }

    @Override
    public User checkUser(User user) {
        User u=null;
        try{
            u=jt.queryForObject("select userName,userPwd from userinfo where userName=? and userPwd=?",
                    new BeanPropertyRowMapper<User>(User.class),user.getUserName(),user.getUserPwd());
        }catch (Exception e){
        }
        return u;
    }

    @Override
    public int addUser(User user)  {
        int c=0;
        String status="正常";
        try {//c为添加数据库影响的行数,添加成功则c=1,失败则c=0
            c=jt.update("insert into userinfo(userName,userPwd,typeId,userStatus) values (?,?,?,?)",
                    user.getUserName(),user.getUserPwd(),3,status);
        }catch (Exception e){
        }
        return c;
    }
}

工具类:JDBCUtils (创建连接池)

public class JDBCUtils {
    private static DataSource ds;

    static {
        Properties p=new Properties();
        try {
            p.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            ds= DruidDataSourceFactory.createDataSource(p);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource(){
        return ds;
    }
}

到这里就结束了,可以正常运行了。

你可能感兴趣的:(Java,Spring)