项目目录
登录功能实现思路
目录
1.pom.xml添加依赖
2.配置application.yml文件
3.sql映射文件配置---UserMapper.xml
4.导入页面资源
5.Springboot启动类的配置
6.编写全局配置类 config->AppConfig
7.创建实体类--数据表对应
8.修改login.html页面
9.编写UserController的login方法
10.编写IUserService接口的login方法
11.编写UserServiceImpl实现类的login方法
12.编写IUserMapper接口的login方法
13.编写UserMapper.xml映射文件
14.idea中配置数据源
15.运行结果
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
test
org.webjars
jquery
3.5.1
com.alibaba
druid
1.1.8
org.springframework.boot
spring-boot-configuration-processor
org.springframework.boot
spring-boot-starter-validation
org.projectlombok
lombok
commons-lang
commons-lang
2.5
#数据源配置
spring:
datasource:
username: root
password: rootroot
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/travel?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
type: com.alibaba.druid.pool.DruidDataSource
#禁用thymeleaf缓存
thymeleaf:
cache: false
#加载mybatis配置文件
mybatis:
#指定mapper映射文件路径
mapper-locations: classpath:mapper/*.xml
#设置类型别名
type-aliases-package: com.etc.travelsys.model
#mybatis sql打印(方法接口所在的包,不是Mapper.xml所在的包)
logging:
level:
com.etc.travelsys.mapper: debug
css js img 复制到static目录 , html页面复制到templates目录下
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动类
@SpringBootApplication
//在指定包下扫描mapper接口
@MapperScan(basePackages = {"com.shenu.travelsys.mapper"})
public class TravelSysApplication {
public static void main(String[] args) {
SpringApplication.run(TravelSysApplication.class, args);
}
}
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class AppConfig implements WebMvcConfigurer {
//统一视图跳转
@Override
public void addViewControllers(ViewControllerRegistry registry){
//访问路径为http://localhost:8080/时,那么就是访问login文件夹下的login.html页面
registry.addViewController("/").setViewName("login/login");
}
}
entity->UserEntity
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
import java.util.Date;
/**
* @description: 用户实体类
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserEntity {
private int userid;//用户id
private String username;//用户名
private String password;//密码
private String email;//邮箱
private String sex;//性别 1-男 2-女
private int status;//状态 1-账户正常 2-账户异常
private Date birthday;//出生日期
private String location;//地址
private String QQ;//qq号
private String mobile;//手机号
private String hobby;//爱好
private String introduction;//个人介绍
private String headimg;//头像地址
private Timestamp registertime;//注册时间
private int role;
}
templates/login/login.html
import com.etc.travelsys.model.entity.UserEntity;
import com.etc.travelsys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
/**
* @description: 用户相关的控制层
**/
@Controller //标记控制层,把当前类的对象交给容器管理
//映射路径(访问路径,可以声明在类和方法上),只要访问此类中的资源,一级路径必须是user
@RequestMapping("/user")
public class UserController {
@Autowired //根据类型自动注入对象
@Qualifier("userService") //如果指定注入的对象,使用Qualifier注解
private IUserService userService;
//登录
@RequestMapping("/login")
public String login(String username, String password, Model model, HttpSession session){
//1.接收请求中的参数(view-controller):方法的入参直接获取(页面输入的username和password)
//2.处理数据
//(1)创建UserServiceImpl的对象 --- 成员变量自动注入
//(2)调用service中的方法 参数:username password 返回值:UserEntity
UserEntity user = userService.login(username,password);
//3.保存数据(controller-view)并跳转页面
String url = "";
//(1)判断登录是否成功
if (user != null){
//(2)成功 1.把数据保存到session中 2.跳转页面到index.html
session.setAttribute("USER",user);
if (user.getRole() == 1){ //role 1-普通用户 2-管理员用户
url = "index";
}else{
url = "/admin/index";
}
}else{
//(3)失败 1.把异常信息保存到model中 2.跳转到login.html
model.addAttribute("msg","登录名或者密码错误");
url = "/login/login";
}
//经过视图解析器,最终路径解析为 前缀(/templates/)+url+后缀(.html)
return url;
}
}
import com.shenu.travelsys.model.entity.UserEntity;
/**
* 用户相关的业务逻辑层接口
*/
public interface IUserService {
UserEntity login(String username, String password);
}
import com.etc.travelsys.mapper.UserMapper;
import com.etc.travelsys.model.entity.UserEntity;
import com.etc.travelsys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @description: 用户相关的业务逻辑层接口实现类
**/
@Service("userService") //用于标注业务层组件
public class UserServiceImpl implements IUserService {
//自动注入UserMapper对象
@Autowired
private UserMapper userMapper;
//重写接口的登录方法
@Override
public UserEntity login(String username, String password) {
return userMapper.login(username,password);
}
}
import com.etc.travelsys.model.entity.UserEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
* @description:用户相关的数据持久层接口
**/
@Repository
public interface UserMapper {
//登录抽象方法,如果是两个以上的参数,可以使用@Param注解;如果是一个参数--不需要
UserEntity login(@Param("username") String username,
@Param("password") String password);
}