@Repository
public interface UserDao {
/**
-
@Description:
-
@Auther: ONESTAR
-
@Date: 10:24 2020/5/27
-
@Param: username:用户名;password:密码
-
@Return: 返回用户对象
*/
User findByUsernameAndPassword(@Param(“username”) String username, @Param(“password”) String password);
}
分析:
- @Mapper注解:让Mybatis找到对应的mapper,在编译的时候动态生成代理类,实现相应SQL功能
- @Repository注解:用来声明dao层的bean(这个注解可有可无,可以消去依赖注入的报错信息)【@Mapper和@Repository注解可以参考这篇文章:Mybatis 中的 @Repository 与 @Mapper】
- @Param注解:将参数传递给SQL
- 返回一个User对象给service调用并核对用户名和密码
4.mapper
Mybatis使用XMLMMapperBuilder类的实例来解析mapper配置文件并执行SQL语句,在resources目录下创建mapper文件夹,再创建UserDao.xml文件,如下:
<?xml version="1.0" encoding="utf-8" ?>select * from myblog.t_user
where username = #{username} and password = #{password};
5.用户业务层
在com.star目录下创建service包,创建用户业务层接口UserService,这里主要是检验用户名和密码,传递用户名和密码两个参数,代码如下:
package com.star.service;
import com.star.entity.User;
/**
-
@Description: 用户业务层接口
-
@Date: Created in 22:56 2020/5/26
-
@Author: ONESTAR
-
@QQ群: 530311074
-
@URL: https://onestar.newstar.net.cn/
*/
public interface UserService {
//核对用户名和密码
User checkUser(String username, String password);
}
用户层接口实现类:
在service包下创建Impl包,用来放接口实现类,UserServiceImpl代码如下:
package com.star.service.Impl;
import com.star.dao.UserDao;
import com.star.entity.User;
import com.star.service.UserService;
import com.star.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;</