使用IDEA搭建一个简单的SpringBoot项目——————超级详细(包含一些报错)本人亲测无误

直接上步骤

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
先上目录表:(每个目录用处就不多解释了)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

如果报:Connection to @localhost failed. [08001] Could not create connection to database server. Attempt
在数据库路径后面添加:?serverTimezone=GMT

package com.example.test_1020.bean;
 
public class user {
    
    
    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;
    }
}
package com.example.test_1020.controller;

import com.example.test_1020.bean.user;
import com.example.test_1020.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;
 
@Controller
public class LoginController {
    
    
 
    //将Service注入Web层
    @Autowired
    UserService userService;
 
    @RequestMapping("/login")
    public String show(){
    
    
        return "login";
    }
 
    @RequestMapping(value = "/loginIn",method = RequestMethod.POST)
    public String login(String name,String password){
    
    
        user userBean = userService.loginIn(name,password);
        if(userBean!=null){
    
    
            return "success";
        }else {
    
    
            return "error";
        }
    }
 
}
package com.example.test_1020.mapper;

import com.example.test_1020.bean.user;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

@Component
public interface UserMapper {
    
    
    // 如果有多个参数,需要加@param
    user getInfo(@Param("name") String name,@Param("password") String password);
 
}
package com.example.test_1020.service;

import com.example.test_1020.bean.user;
import org.springframework.stereotype.Component;

@Component
public interface UserService {
    
    
 
    user loginIn(String name, String password);
 
}
package com.example.test_1020.serviceImpl;
 
import com.example.test_1020.bean.user;
import com.example.test_1020.mapper.UserMapper;
import com.example.test_1020.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service
public class UserServiceImpl implements UserService {
    
    
 
    //将DAO注入Service层
    @Autowired
    private UserMapper userMapper;
 
    @Override
    public user loginIn(String name, String password) {
    
    
        return userMapper.getInfo(name,password);
    }

}

Application文件一定要加@MapperScan注解,并标记mapper目录,不然会找不到

@SpringBootApplication
@MapperScan("com.example.test_1020.mapper")
public class Test1020Application {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(Test1020Application.class, args);
    }

}

测试类:

package com.example.test_1020;

import com.example.test_1020.bean.user;
import com.example.test_1020.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
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 TestApplicationTests {
    
    
 
    @Autowired
    UserService userService;
 
    @Test
    public void contextLoads() {
    
    
        user userBean = userService.loginIn("qwe","111");
        System.out.println("该用户ID为:");
        System.out.println(userBean.getId());
    }
 
}


在resources下创建mapper文件夹,用来存放xml文件

UserMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.test_1020.mapper.UserMapper">

    <select id="getInfo" parameterType="String" resultType="com.example.test_1020.bean.user">
        SELECT *
        FROM user
         WHERE name = #{
    
    name} AND password = #{
    
    password}
    </select>

</mapper>

templates:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>error</title>
</head>
<body>
<h1>登录失败!</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body>
你好!初学者,我是SpringBoot的简单启动页面!
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
</head>
<body>
<form role="form" action = "/loginIn" method="post">
    账号:<input type="text" id="name" name = "name"> <br>
    密码:<input type="password" id = "password" name = "password"> <br>
    <input type="submit" id = "login" value = "login">
</form>

</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>success</title>
</head>
<body>
<h1>登录成功!</h1>
</body>
</html>

application.yml:

## 应用名称
#spring.application.name=test_1020
## 应用服务 WEB 访问端口
#server.port=8080
##下面这些内容是为了让MyBatis映射
##指定Mybatis的Mapper文件
#mybatis.mapper-locations=classpath:mappers/*xml
##指定Mybatis的实体目录
#mybatis.type-aliases-package=com.example.test_1020.mybatis.entity
## 数据库驱动:
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
## 数据源名称
#spring.datasource.name=defaultDataSource
## 数据库连接地址
#spring.datasource.url=jdbc:mysql://localhost:3306/blue?serverTimezone=UTC
## 数据库用户名&密码:
#spring.datasource.username=***
#spring.datasource.password=***

server:
  port:8080:


spring:
  datasource:
    name: test  #数据库名
    url: jdbc:mysql://localhost:3306/test #url
    username: root  #用户名
    password: root  #密码
    driver-class-name: com.mysql.cj.jdbc.Driver  #数据库链接驱动

mybatis:
  mapper-locations: classpath:mapper/*.xml  #配置映射文件
  type-aliases-package: com.example.test_1020.bean #配置实体类

**
如果报时区异常,可以在url: jdbc:mysql://localhost:3306/test后加?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

pom:

**
一些包必须匹配

<?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>
    <groupId>com.example</groupId>
    <artifactId>test_1020</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test_1020</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
<!--            <version>5.1.41</version>   -->
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
<!--            <version>5.3.2</version>
            <scope>compile</scope>-->
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${
    
    spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.test_1020.Test1020Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

猜你喜欢

转载自blog.csdn.net/weixin_44704605/article/details/120875420