Mybatis-Plus通过 properties文件与 yml 文件配置数据源

Mybatis-Plus通过 properties文件与 yml 文件配置数据源

1.pom.xml中添加依赖

添加jdbc依赖
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
MySQL的jdbc驱动
<dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>5.1.34</version>
</dependency>

2.application.properties配置文件

#数据库连接配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/数据库名
spring.datasource.password=密码
spring.datasource.username=用户名

spring.datasource.initialSize=5                   //初始化连接数
spring.datasource.minIdle=5                       //最大空闲连接
spring.datasource.maxActive=20                    //最大连接数量
spring.datasource.maxWait=60000                   //最小空闲连接

3.application.yml文件配置文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/s数据库名
    username: 用户名
    password: 密码
#mybatis-plus配置
mybatis-plus:
    #配置Mapper映射文件
    mapper-locations: classpath:/mybatis/mappers/*.xml
    # 配置Mybatis数据返回类型别名(默认别名为类名)
    type-aliases-package: com.yunnuo.server.pojo
    configuration:
        # 自动驼峰命名
        map-underscore-to-camel-case: false

#配置控制台打印日志Debug
logging:
    level:
        com.jd.mapper: debug


4.乱码问题

如果数据库连接或者插入或修改数据中文时出现乱码可以在数据库连接url后面加上useUnicode=true&&characterEncoding=utf-8

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Mybatis-Plus 是一个 Mybatis 的增强工具,在使用 Mybatis-Plus 之前需要先配置 Mybatis。以下是在 Mybatis-Plus 中添加配置的步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency> ``` 2. 配置数据源 在 application.yml 或 application.properties 文件配置数据源信息,例如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver ``` 3. 配置 Mybatis 在 application.yml 或 application.properties 文件中添加以下配置: ```yaml mybatis: mapper-locations: classpath*:mapper/*.xml configuration: map-underscore-to-camel-case: true ``` 其中,mapper-locations 属性指定了 Mapper 文件的位置,configuration 属性指定了 Mybatis 的全局配置。 4. 配置 Mybatis-Plus 在 application.yml 或 application.properties 文件中添加以下配置: ```yaml mybatis-plus: mapper-locations: classpath*:mapper/*.xml configuration: map-underscore-to-camel-case: true ``` 其中,mapper-locations 属性和 Mybatis配置相同,configuration 属性可以配置 Mybatis-Plus 的全局配置。 5. 配置代码生成器(可选) Mybatis-Plus 提供了代码生成器,可以根据数据表生成实体类、Mapper 接口和 XML 文件。在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.2</version> </dependency> ``` 在 application.yml 或 application.properties 文件中添加以下配置: ```yaml mybatis-plus: generator: # 全局配置 global-config: author: yourname output-dir: src/main/java file-override: true open: false # 数据源配置 datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # 包配置 package-info: parent: com.example.demo moduleName: model entity: model.entity mapper: model.mapper service: model.service serviceImpl: model.service.impl controller: model.controller # 策略配置 strategy: naming: underline_to_camel include: sys_user entityLombokModel: true ``` 其中,generator 属性配置了代码生成器的全局配置数据源配置、包配置和策略配置。需要根据实际情况进行修改。 以上是在 Mybatis-Plus 中添加配置的步骤,根据实际情况可以选择需要的配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧寂173

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值