从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

一、创建SpringBoot项目

1. 打开idea File->New->Project

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

2. 选择Spring initializr(初始化) ,然后点Next

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

3. 更改Group和Artifact后,点击Next

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

4. 在新页面中Core中勾选DevTools;Web中勾选Web;Template Engines(模板引擎)勾选 Thymeleaf;SQL中勾选JPA和              MYSQL,然后点Next

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

5. 最后界面直接点Finish

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)


二、配置SpringBoot项目

1. 在src/main/java 建立以下目录

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

2. 编辑pom.xml文件 

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 http://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.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springbootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo</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-data-jpa</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.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- druid数据库连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>

        <!--springboot操作redis的组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!--添加糊涂工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.6</version>
        </dependency>

        <!--javamail依赖用来发送电子邮件-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>

    </dependencies>

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

</project>

3. 编辑src/main/resources/ 下的 application.properties文件

application.properties:

#数据源配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stu?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root    
spring.datasource.initialSize=20
spring.datasource.minIdle=50
spring.datasource.maxActive=500


#上下文配置
server.port=8888
server.servlet.context-path=/kude


#配置jpa
#帮我们自动生成表结构
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

4. 此时点击运行测试SpringBoot运行成功

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

控制台:

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)

 


三、设计表结构(未关联)

 

从零开始完成一个SpringBoot+JPA的Web案例((一)新建项目和配置)


项目搭建和配置完成,接下来编写后台代码,功能实现后再进行测试,请看下一篇:《从零开始完成一个SpringBoot+JPA的Web案例((二)后端实现)》