Spring框架三层架构(dao、service和controller)注解注入 实现简易登录功能

三层架构

1、Dao层

全称为Data Access Object,负责于数据库进行联络,完成增删查改的功能。提供接口给Service层。

2、Service层

调用Dao层提供的接口,业务处理,为Controller层提供接口。

3、Controller层

负责请求转发,接收用户传来的参数,通过调用Service层提供的接口,实现将数据转发给Service层处理。接收Service的返回值,再转发给用户。

简易登录功能

需求:
通过Spring框架,实现与数据库连接,在控制台输入用户名和密码,对用户进行校验、登陆、验证,成功返回“success”,失败返回“error”。

项目整体目录结构

其中libs目录包含了spring框架所需要的jar依赖包和jdbc包
Spring框架三层架构(dao、service和controller)注解注入 实现简易登录功能_第1张图片

数据库信息

Spring框架三层架构(dao、service和controller)注解注入 实现简易登录功能_第2张图片

entity实体类User:

package com.yancy.entity;

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

    public String getUserName() {
        return userName;
    }

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

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
}

Controller层代码

package com.yancy.controller;

import com.yancy.entity.User;
import com.yancy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    public String userLogin(User user) {
        Boolean result = userService.userLogin(user);
        if(result)
            return "success";
        else
            return "erro";
    }
}

Srevice层代码

UserService接口
package com.yancy.service;

import com.yancy.entity.User;

public interface UserService {
    public boolean userLogin(User user);
}
UserService接口实现类 UserServiceImpl
package com.yancy.service.impl;

import com.yancy.dao.UserDao;
import com.yancy.entity.User;
import com.yancy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Override
    public boolean userLogin(User user) {
        User sqlUser = userDao.userLogin(user);
        if((sqlUser.getUserName().equals(user.getUserName())) && (sqlUser.getPassWord().equals( user.getPassWord()))) return true;
        return false;
    }
}

Dao层代码

UserDao接口:UserDao
package com.yancy.dao;

import com.yancy.entity.User;

public interface UserDao {
    public User userLogin(User user);
}
UserDao接口的实现类:UserDaoImpl
package com.yancy.dao.impl;

import com.yancy.dao.UserDao;
import com.yancy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public User userLogin(User user) {
        String sql="select * from user where userName=?";
        RowMapper<User> rowMapper=new BeanPropertyRowMapper(User.class);
        User resUser= jdbcTemplate.queryForObject(sql,rowMapper,user.getUserName());
        return resUser;
    }
}

配置文件bean.xml(在src的目录下,不在com下)


<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">

    
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver "/>
        <property name="url" value="jdbc:mysql://localhost:3306/students?serverTimezone=Asia/Shanghai"/>
        <property name="username" value="root"/>
        <property name="password" value="zy20011109"/>
    bean>
    
    
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    bean>
    
    
    <context:component-scan base-package="com.*"/>
beans>

测试类Main

import com.yancy.controller.UserController;
import com.yancy.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
/*        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
        System.out.println(jdbcTemplate);*/
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        UserController userController = (UserController) applicationContext.getBean("userController");
        User user = new User();
        Scanner in = new Scanner(System.in);
        System.out.printf("请输入用户名:");
        user.setUserName(in.next());
        System.out.printf("请输入密码:");
        user.setPassWord(in.next());
        System.out.println(userController.userLogin(user));
    }
}

运行结果

运行Main类中主函数
输入数据库中存在的用户信息:
Spring框架三层架构(dao、service和controller)注解注入 实现简易登录功能_第3张图片
输入错误信息:
Spring框架三层架构(dao、service和controller)注解注入 实现简易登录功能_第4张图片

你可能感兴趣的:(JavaWeb,spring,架构,java)