道阻且长,行则将至,走慢一点没关系|

Ac_c0mpany丶

园龄:3年7个月粉丝:6关注:3

2022-11-18 15:16阅读: 26评论: 0推荐: 0

Spring Boot集成Mybatis

Spring Boot集成Mybatis

一、添加mybatis依赖、MySQL驱动

  • pom.xml
<!--MySQL驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <!--不需要写版本号,SpringBoot在Parent中锁定了版本-->
</dependency>

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

<!-- Druid数据源 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.1</version>
</dependency>

二、数据源配置

  • application.yml
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:13306/wiki?serverTimezone=UTC
    druid:
      initial-size: 5
      max-active: 10

数据源的相关配置可以参考:spring-boot-autoconfigure.jar 中的 DataSourceProperties 类

三、Mybatis配置

  • application.yml
# 配置mybatis所有Mapper.xml所在的路径
mybatis:
  mapper-locations: classpath:/mapper/*.xml
  # 开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

Mybatis还有很多核心配置,官网现用现查。

Mybatis相关的配置,可以参考:mybatis-spring-boot-autoconfigure.jar 中的 MybatisProperties 类

四、生成实体entity

package com.kyk.wiki.domain;

import lombok.Data;

/**
 * @FileName wiki-server
 * @Author keyongkang
 * @Create 2022-11-18-14:25
 */

@Data
public class Test {
    // id
    private Long id;

    // 名称
    private String name;

    // 密码
    private String password;
}

五、生成Mapper接口

package com.kyk.wiki.mapper;

import com.kyk.wiki.domain.Test;

import java.util.List;

/**
 * @FileName wiki-server
 * @Author keyongkang
 * @Create 2022-11-18-14:27
 */
public interface TestMapper {

    List<Test> list();
}

扫描Mapper的注解有两种——@Mapper@MapperScan

  • @Mapper需要在每一个Mapper接口类上添加,作用扫描Mapper接口
  • @MapperScan是在SpringBoot启动入口类上添加的,它是扫描所有Mapper接口所在的包

六、生成Mapper接口对应的映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kyk.wiki.mapper.TestMapper">
    <!-- List<Test> list();-->
    <select id="list" resultType="com.kyk.wiki.domain.Test">
        SELECT *
        FROM test
    </select>

</mapper>

额外:Mybatis的配置——XML、注解、application.yml

Mybatis主配置文件主要有以下三种形式:

  • 使用额外的 XML文件,然后在 SpringBoot 中引入该 XML(通用)
  • 利用注解实现纯代码进行配置(SpringBoot 专用)
  • application 中进行配置(SpringBoot 专用)

XML配置

在application.yml中指定mybatis配置文件的位置:

mybatis:
  config-location: classpath:mybatis-config.xml

然后在mybatis-config.xml中配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 使用新生成记录的主键 -->
        <setting name="useGeneratedKeys" value="true"/>
        <!-- 数据库: my_first_name -> Java: myFirstName -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

注解方式配置

@Configuration
public class MyBatisConfig {
    @Bean
    public ConfigurationCustomizer customizer() {
        return (configuration) -> {
            configuration.setMapUnderscoreToCamelCase(true);
            configuration.setUseGeneratedKeys(true);
        };
    }
}

application.yml中配置

mybatis:
  configuration:
    map-underscore-to-camel-case: true
    use-generated-keys: true

本文作者:Ac_c0mpany丶

本文链接:https://www.cnblogs.com/keyongkang/p/16903362.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ac_c0mpany丶  阅读(26)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 You Are My Sunshine REOL
You Are My Sunshine - REOL
00:00 / 00:00
An audio error has occurred.

作曲 : Traditional

You are my sunshine

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away

The other night, dear,

When I lay sleeping

I dreamed I held you in my arms.

When I awoke, dear,

I was mistaken

So I hung my head and cried.

You are my sunshine,

My only sunshine.

You make me happy

When skies are gray.

You'll never know, dear,

How much I love you.

Please don't take my sunshine away.

You are my sunshine,

My only sunshine

You make me happy

When skies are gray.

You'll never know, dear

How much I love you

Please don't take my sunshine away

Please don't take my sunshine away.

Please don't take my sunshine away.