springBoot整合mybatisPlus从零搭建项目--基础整合版

springBoot整合Mybatis和spring data jdbc 初级版。简单搭个项目。

环境是Jdk8

第一步创建项目:打开idea>file>project>SpringInitializr

 

 

 第二步 选择需要加载的开发工具类:这里选择默认的springboot版本即可,选择完成后点击 create即可

 

 

 第三步:项目创建完成之后,进入file>setting里面搜索maven并更改默认的maven配置,使用我们自己的maven配置。

 

 

 在设置完maven后如果侧边没有出现maven工具栏重启一下就好,如果maven加载不成功则根据提示信息再次重复上一步即可。

 

 

 当maven加载完成后运行即可(此时没有设置数据库连接,运行后控制台会提醒没有数据源)

 

 

 在这时候一个简单的springBoot项目已经搭建完毕,但是没有配置数据源,下面进入application.yml的简单配置:首先将application.propertites改为application.yml 并复制一份改名为application-dev.yml(application-dev开发环境  application-test 测试环境  application-pro 生产环境)

 

 

 在application里面配置active为dev,代表我们目前使用的是dev的配置文件

 

 

 在application-dev.yml里面配置:

variable:
  profiles: dev

#数据源
spring:
  datasource:
    username: 数据库用户名
    password: 数据库用户密码
    url: jdbc:mysql://数据库地址:3306/app_version?useSSL=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT%2B8&allowMultiQueries=true
    driver-class-name: com.mysql.cj.jdbc.Driver
server:
  tomcat:
    uri-encoding: utf-8
  #设置端口
  port: 8080
  servlet:
    #设置访问路径前缀
    context-path: /car318
##Mybatis Plus 配置
mybatis-plus:
  #配置mapper.xml,实体类,枚举类 的扫描路径
  mapper-locations: classpath*:/mapper/**/*Mapper.xml
  type-aliases-package: com.car318.appversion.**.entity
  type-enums-package: com.car318.appversion.**.enums
  global-config:
    db-config:
      id-type: auto
      table-underline: false
  configuration:
  #下划线自动转驼峰 #map
-underscore-to-camel-case: false cache-enabled: false # 默认不展示展示日志 # log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl # 展示日志 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #解决mybatis查询时数据返回值为null时不会反射的问题---目前设置了也没用 call-setters-on-nulls: true

做完application-dev配置后复制改名为application-test和application-pro将里面的profiles值改为对应的值,数据库修改地址,即可实现在application里面进行配置文件的切换。

最后,因为部分驱动尚未导入,需要在pom文件中导入jar包即可:

<?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.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.car318</groupId>
    <artifactId>appVersion</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>appVersion</name>
    <description>appVersion</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</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>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- mybatis-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <!-- 引入log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- 热启动 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <!--        aspectjrt引入-->
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.5</version>
        </dependency>
        <!-- json引入-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.8</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>book</doctype>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.restdocs</groupId>
                        <artifactId>spring-restdocs-asciidoctor</artifactId>
                        <version>${spring-restdocs.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.0</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

posted @ 2022-05-12 17:54  牧雨  阅读(330)  评论(0编辑  收藏  举报