欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springBoot-Mybatis

程序员文章站 2022-03-06 12:17:44
文章目录1. 前期准备1. 前期准备创建SpringBoot项目引入mysql驱动依赖和mybais依赖mysql爆红: mysql mysql-connector-java解决问题:打开Settings -> Build,Execution,Dep...

文章目录

1. 前期准备

  1. 创建SpringBoot项目
  2. 引入mysql驱动依赖和mybais依赖

mysql报红:

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
</dependency>

解决问题:
打开Settings -> Build,Execution,Deployment -> Build Tools -> Maven -> Ignored Files
springBoot-Mybatis
打开:Settings -> Build,Execution,Deployment -> Build Tools -> Maven -> Importing
springBoot-Mybatis
打开:File -> Maven ->
springBoot-Mybatis
打开settings.xml加入镜像:

<mirrors>
        <mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>
                http://maven.aliyun.com/nexus/content/groups/public/
            </url>
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>

删掉已经写好的依赖:
springBoot-Mybatis
然后再写上即可

mybatis依赖

 <!--Mybatis整合SpringBoot框架的起步依赖-->
 <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.4</version>
  </dependency>

springBoot-Mybatis

  1. application.properties添加数据库和mybatis配置或者配置application.yml

application.properties

# mysql
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

# mybatis
# 对应实体类的包名
mybatis.typeAliasesPackage=com.example.ewspace.enty
# mapper.xml文件所在位置,这里我放在resources下面
mybatis.mapperLocations=classpath:mapper/*Mapper.xml

application.yml

server:
  port: 8081

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.example.ewspace.enty
  1. 可以写代码了

本文地址:https://blog.csdn.net/qq_45083168/article/details/112004079