spring boot 使用ssm框架之登录

Spring Boot

开发Spring Boot的主要动机是简化配置和部署spring应用程序的过程。

Spring Boot的主要特点:

创建独立的Spring应用程序

直接嵌入TomcatJettyUndertow(无需部署WAR文件)

提供初始POM文件内容,以简化Maven配置

尽可能时自动配置Spring

绝对无代码生成,也不需要XML配置

开发spring boot项目

可以在eclipse中安装spring插件,然后新建spring starter project

spring boot 使用ssm框架之登录

Spring boot项目的结构

spring boot 使用ssm框架之登录

这样一个spring boot项目就创建成功了

我们在spring boot的入后类中右键启动spring boot

spring boot 使用ssm框架之登录

会看到在控制台中会报错:

spring boot 使用ssm框架之登录

这是由于我们没有配置数据源的原因,spring boot虽说是简化配置但是改有的数据库配置还是要由我们手动配置。

在application.properties文件中配置数据库属性

spring boot 使用ssm框架之登录

其中配置thymeleaf模板是因为spring boot不推荐使用jsp,因此我们需要在属性文件中配置view层(此处view层就不采用jsp而是使用html),配置信息包括html文件所在路径等其他属性

由于使用到了thymeleaf模板因此我们需要在pom.xml中引入相关依赖

spring boot 使用ssm框架之登录

此时我们再启动项目会发现正常启动,到此一个简单的基于spring boot的ssm项目框架就完成了;接下来我们在这个基础上编写一个登录功能。

编写登录功能

spring boot 使用ssm框架之登录

我们都知道java代码是存放在上图的src/main/java目录下的,

但是需要注意的是在spring boot项目中我们编写的代码一定要在图中指出包中或其子包中,按照这个原则我们把所需要的包创建好如下图:

spring boot 使用ssm框架之登录

若在启动项目时报类似如下错误很有可能就是因为没有按照上述操作

spring boot 使用ssm框架之登录

功能代码

登录页面代码,我们在src/mian/resources/templates/下创建index.html如下:

spring boot 使用ssm框架之登录

图中箭头所指的标签默认若没有结束的”/”最好手动加上,否则后期运行可能会报错。

Controller代码:
 

package com.example.demo.controller;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

import com.example.demo.bean.User;

import com.example.demo.service.UserService;

 

@Controller

public class UserController {

 

   @Autowired

   private UserService userService;

   @RequestMapping("login")

   public String login(User user) {

      System.out.println(user.getUserName()+":"+user.getPassWord());

     if(userService.login(user)!=null) {

        return "success";

     }else{

        return "error";

     }

   }

}

 

Service代码

package com.example.demo.service.impl;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import com.example.demo.bean.User;

import com.example.demo.mapper.UserMapper;

import com.example.demo.service.UserService;

@Service("userService")

public class UserServiceImpl implements UserService{

 

      @Autowired

      private UserMapper userMapper;

      @Override

      public User login(User user) {

            // TODO Auto-generated method stub

            return userMapper.login(user);

      }

 

}

Mapper代码

package com.example.demo.mapper;

 

import org.apache.ibatis.annotations.Select;

 

import com.example.demo.bean.User;

 

public interface UserMapper {

 

      @Select("select * from users where username=#{userName} and password=#{passWord}")

      public User login(User user);

}

大功告成!!!

我们启动浏览器输入地址:

spring boot 使用ssm框架之登录

我们会发现找不到页面,这就尴尬了,为什么会出现这样的问题那?

Spring boot不允许我们直接访问页面,因此我们需要再创建一个controller如下:

package com.example.demo.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

 

import com.example.demo.bean.User;

 

@Controller

public class IndexController {

 

   @RequestMapping("index")

   public String login(User user) {

     return "index";

   }

}

 

我们访问这个controller然后它帮我们跳转到index页面

此时我们再刷新页面:

spring boot 使用ssm框架之登录

输入用户名密码,成功登录!

spring boot 使用ssm框架之登录

Ok,搞定收工!

补充:如在运行时出现以下问题可能是org.mybatis.spring.boot版本问题,将其更换为如下版本错误消失

<dependency>

            <groupId>org.mybatis.spring.boot</groupId>

            <artifactId>mybatis-spring-boot-starter</artifactId>

            <version>1.1.1</version>

        </dependency>

spring boot 使用ssm框架之登录