使用idea搭建一个简单的SpringBoot项目

参考:https://blog.csdn.net/baidu_39298625/article/details/98102453

1、创建项目

 

下一步:

Web下勾选Spring Web、Spring Web Services

 

 

 

Template Englines勾选Thymeleaf

 

 

 

SQL勾选:MS SQL Server Driver、JDBC API   、 MyBatis Framework三项;点击next;

 

 

点击Finish

完成后的目录结构:

 

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.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--jdbc访问数据库依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--数据库驱动依赖-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--thymeleaf模板引擎配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--Web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!--MyBatis配置-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <!--<version>2.1.0</version>-->
        </dependency>
        <!--单元测试配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.8</version>
        </dependency>
    </dependencies>

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

</project>

 

2、在templates文件下新建index.html页面,作为启动的初始页面

 

 

3、在com.example.demo下新建controller文件夹建立文件夹controller

 

 

4、在controller文件夹下建一个简单的helloController类;(Controller类要添加@Controller注解,项目启动时,SpringBoot会自动扫描加载Controller)

 

 

 

 

package com.example.demo.controller;

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

@Controller
public class HelloController {
    @RequestMapping("/index")
    public String sayHello() {
        return "index";
    }
}

 

 

5、在application.properties中配置信息和datasource数据源

#应用名称
spring.application.name=springboot-demo
server.port=8888
server.servlet.context-path=/website
#编码格式
server.tomcat.uri-encoding=utf-8
#数据库相关配置
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://127.0.0.1:1433/pckjdps
spring.datasource.username=sa
spring.datasource.password=123456
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
#session生命周期
server.servlet.session.timeout=30m

 

 运行项目启动类:DemoApplication

 

有条告警信息,原始是还没有配置编写MyBatis的相关文件:No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.

 

6、在浏览器中输入localhost:8888/website/index  ,回车显示初始的index界面;到这项目的初步搭建已经完成

 

7、用idea连接数据库

     ==>    

 

 

连接成功:

 

 

8、SpringBoot项目大概分为四层:

(1)DAO层:包括XxxMapper.java(数据库访问接口类),XxxMapper.xml(数据库链接实现);(这个命名,有人喜欢用Dao命名,有人喜欢用Mapper,看个人习惯了吧)

(2)Bean层:也叫model层,模型层,entity层,实体层,就是数据库表的映射实体类,存放POJO对象;

(3)Service层:也叫服务层,业务层,包括XxxService.java(业务接口类),XxxServiceImpl.java(业务实现类);(可以在service文件夹下新建impl文件放业务实现类,也可以把业务实现类单独放一个文件夹下,更清晰)

(4)Web层:就是Controller层,实现与web前端的交互。


创建目录结构如下:

 

 

 

 9、在application配置文件中添加MyBatis配置

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

 

posted @ 2021-06-27 15:16  清语堂  阅读(427)  评论(0编辑  收藏  举报