1. 程式人生 > >spring boot 使用ssm框架之登入

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專案的結構

這樣一個spring boot專案就建立成功了

我們在spring boot的入後類中右鍵啟動spring boot

會看到在控制檯中會報錯:

這是由於我們沒有配置資料來源的原因,spring boot雖說是簡化配置但是改有的資料庫配置還是要由我們手動配置。

在application.properties檔案中配置資料庫屬性

其中配置thymeleaf模板是因為spring boot不推薦使用jsp,因此我們需要在屬性檔案中配置view層(此處view層就不採用jsp而是使用html),配置資訊包括html檔案所在路徑等其他屬性

由於使用到了thymeleaf模板因此我們需要在pom.xml中引入相關依賴

此時我們再啟動專案會發現正常啟動,到此一個簡單的基於spring boot的ssm專案框架就完成了;接下來我們在這個基礎上編寫一個登入功能。

編寫登入功能

我們都知道java程式碼是存放在上圖的src/main/java目錄下的,

但是需要注意的是在spring boot專案中我們編寫的程式碼一定要在圖中指出包中或其子包中,按照這個原則我們把所需要的包建立好如下圖:

若在啟動專案時報類似如下錯誤很有可能就是因為沒有按照上述操作

功能程式碼

登入頁面程式碼,我們在src/mian/resources/templates/下建立index.html如下:

圖中箭頭所指的標籤預設若沒有結束的”/”最好手動加上,否則後期執行可能會報錯。

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不允許我們直接訪問頁面,因此我們需要再建立一個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頁面

此時我們再重新整理頁面:

輸入使用者名稱密碼,成功登入!

Ok,搞定收工!

補充:如在執行時出現以下問題可能是org.mybatis.spring.boot版本問題,將其更換為如下版本錯誤消失

<dependency>

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

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

            <version>1.1.1</version>

        </dependency>