目录

1.概述

2.创建项目

3.配置pom.xml

4.配置application.properties

5.创建package

6.创建相关类

6.1在model包下创建UserBean

6.2在service包下创建UserService

6.3在serviceImpl包下创建UserServiceImpl

6.4在dao包下创建UserMapper

6.5在controller包下创建LoginController

6.6 在创建主程序

7.创建mapper文件

7.1创建存放mapper文件的目录

7.2创建mapper文件

7.3配置mapper路径

8.创建HTML页面

8.1创建login.html

8.2创建success.html

8.2创建error.html

9.创建数据库用户表

10.测试

10.1 直接在主类测试

10.2 编写测试类测试

11.问题处理

11.1 提示没有org.postgresql.Driver

11.2 页面提示Whitelabel Error Page


1.概述

本实例通过spring boot构建web项目,从用户登录的例子创建CMV层次的流程。

环境:

JDK:1.8

数据库:postgresql

spring boot:2.4.2

2.创建项目

参考《[spring]用IEDA创建spring-boot项目

3.配置pom.xml

文件位置:项目文件夹下

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.4.2</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.zzj.spring.boot</groupId>
   <artifactId>spring-boot-test02</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springboottest02</name>
   <description>Demo project for Spring Boot</description>
   <properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <!--解析html包-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>
      <dependency>
         <groupId>org.postgresql</groupId>
         <artifactId>postgresql</artifactId>
         <version>42.2.18</version>

      </dependency>
        <!--MyBatis配置-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

 

4.配置application.properties

文件位置:src/main/resources

 

spring.datasource.driver-class-name=org.postgresql.Driver
#配置数据库连接URL
spring.datasource.url=jdbc:postgresql://192.168.17.23:25308/zzjdb
#配置数据库额度用户名
spring.datasource.username=zzj
#配置数据库的密码
spring.datasource.password=Huawei@123
# 配置服务器的相关信息
# 配置服务器的端口
#server.port=8080

#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#pring.thymeleaf.mode =LEGACYHTML5
#spring.thymeleaf.encoding=UTF-8
#pring.thymeleaf.content-type=text/html

#配置映射文件
mybatis.mapper-locations=classpath:mapper/*.xml
#配置实体类
#mybatis.type-aliases-package=com.zzj.spring.boot.model

 

5.创建package

创建5个包: controller、service、serviceImpl、dao、model

 

6.创建相关类

6.1在model包下创建UserBean

package com.zzj.spring.boot.model;
public class UserBean {

    private int id;

    private String name;

    private String password;

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }
    public String getName() {

        return name;

    }

    public void setName(String name) {
        this.name = name;

    }

    public String getPassword() {
        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

}

 

6.2在service包下创建UserService

package com.zzj.spring.boot.service;
import com.zzj.spring.boot.model.UserBean;
public interface UserService {
    UserBean loginIn(String name, String password);

}

6.3在serviceImpl包下创建UserServiceImpl

package com.zzj.spring.boot.serviceImpl;
import com.zzj.spring.boot.dao.UserMapper;
import com.zzj.spring.boot.service.UserService;
import com.zzj.spring.boot.model.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserBean loginIn(String name, String password) {
        return userMapper.getInfo(name, password);
    }
}

6.4在dao包下创建UserMapper

package com.zzj.spring.boot.dao;
import com.zzj.spring.boot.model.UserBean;
public interface UserMapper {
    UserBean getInfo(String name, String password);
}

6.5在controller包下创建LoginController

package com.zzj.spring.boot.controller;
import com.zzj.spring.boot.model.UserBean;
import com.zzj.spring.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@Controller
public class LoginController {

    @Autowired
    UserService userService;
    @RequestMapping("/login")
    public String index(){
        return "login";
    }

    @RequestMapping(value = "/loginIn",method = RequestMethod.POST)
    public String login(String name,String password) {
        UserBean userBean = userService.loginIn(name, password);
        if (userBean != null) {
            return "success";
        }
        else {
            return "error";
        }

    }
    }

6.6 在创建主程序

注意:主程序一定要在和controller包在同一个层级上,否则报错。

package com.zzj.spring.boot.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.mybatis.spring.annotation.MapperScan;

@SpringBootApplication

@MapperScan("com.zzj.spring.boot.dao")

public class Springboottest02Application {

   public static void main(String[] args) {

      SpringApplication.run(Springboottest02Application.class, args);

   }

}

 

7.创建mapper文件

Mapper文件作为dao包下的接口接口实现类,可以通过配置XML文件的方式实现底层数据库的解耦。一般上dao下的每个类对应一个mapper文件,每个类型的每个方法对应一个配置。

7.1创建存放mapper文件的目录

在src/main/resources下创建mapper文件夹

 

8.1创建login.html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>hello</title>

</head>

<body>

<form role="form" action = "/loginIn" method="post">

    username:<input type="text" id="name" name = "name"> <br>

    passwd:<input type="password" id = "password" name = "password"> <br>

    <input type="submit" id = "login" value = "login">

</form>

</body>

</html>

 

8.2创建success.html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>hello</title>

</head>

<body>

恭喜你,登录成功!!

</body>

</html>

8.2创建error.html

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>hello</title>

</head>

<body>

用户名或密码错误,请确实是否正确!!

</body>

</html>

 

9.创建数据库用户表

在postgrepsql数据库中执行:

create table zzjtest.testuser(

id int,

name varchar(50),

password varchar(50)

);

insert into zzjtest.testuser(id,name,password)values(1,'a','a');

insert into zzjtest.testuser(id,name,password)values(1,'b','b');

10.测试

10.1 直接在主类测试

启动Springboottest02Application

 
 
启动完成后,在浏览器输入localhost:8080 进行测试

10.2 编写测试类测试

 修改src/test/java/com.zzj.spring.boot.demo的Springboottest02ApplicationTests类

 

package com.zzj.spring.boot.demo;
import com.zzj.spring.boot.model.UserBean;
import com.zzj.spring.boot.service.UserService;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public  class Springboottest02ApplicationTests {
   @Autowired
   UserService userService;
   @Test
   public void contextLoads() {
      UserBean userBean = userService.loginIn("b", "b");
      System.out.println("username:"+userBean.getName()+";userpasswd:"+userBean.getPassword());
   }
   }

 

11.问题处理

11.1 提示没有org.postgresql.Driver

在pom.xml中添加

<repositories>

   <repository>

      <id>java.net</id>

      <url>http://download.java.net/maven/2</url>

   </repository>

</repositories>

11.2 页面提示Whitelabel Error Page

 

在pom.xml中添加

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在application.properties 中添加

mybatis.mapper-locations=classpath:mapper/*.xml

 

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐