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包
在这里插入图片描述

数据库信息

在这里插入图片描述

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下)
<?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">

    <!--编写数据库配置-->
    <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>
    
    <!--编写jdbc模板-->
    <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类中主函数
输入数据库中存在的用户信息:
在这里插入图片描述
输入错误信息:
在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小绵杨Yancy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值