Spring Boot 集成 Mybatis

2022-11-20  本文已影响0人  Tinyspot

1. Mybatis 配置

1.1 导入依赖包

<!-- 数据库连接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

1.2 application.yml 配置

Mybatis配置:
mapper-locations:用来指定 mapper.xml 文件的路径
type-aliases-package:指定MyBatis 需要设置别名的实体类的所在的包
map-underscore-to-camel-case: 开启驼峰映射配置

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.concrete.dao.entity
  configuration:
    map-underscore-to-camel-case: true

数据库配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://mysql.sqlpub.com:3306/spotlive
    username: xxx
    password: xxx

使用数据库连接池配置

spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://mysql.sqlpub.com:3306/spotlive?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: xxx
      password: xxx
      initial-size: 5
      min-idle: 5
      max-active: 20
      test-on-borrow: true

1.3 配置 mapper 映射文件

新建文件 resources/mapper/UserMapper.xml

<?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.concrete.boot.dao.mapper.UserDao">
    <select id="findAll" resultType="User">
        select * from user
    </select>
</mapper>

添加 Mapper 接口

public interface UserDao {
    List<User> findAll();
}

1.4 启动类添加@MapperScan

在启动类添加@MapperScan,扫描 Mapper 接口所在的包

@SpringBootApplication(scanBasePackages = "com.example.conboot.starter"/*, exclude = DataSourceAutoConfiguration.class*/)
@MapperScan(basePackages = "com.concrete.boot.dao.mapper")
public class ConbootStarterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConbootStarterApplication.class, args);
    }
}

2. 总配置方式

mybatis-config.png

yml 文件

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="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <!-- <typeAlias type="com.concrete.boot.dao.entity.User" alias="User"/> -->

        <!-- 自动配置类的别名-->
        <package name="com.concrete.boot.dao.entity"/>
    </typeAliases>

    <mappers>
        <mapper resource="mapper/UserMapper.xml" />
        <!-- <package name="mapper"/> 此配置需要 mapper.xml 与接口名完全一样 -->
    </mappers>

</configuration>

2.1 注意事项

mybatis:
# mapper-locations: classpath:mapper/*.xml
  config-location: classpath:mybatis-config.xml
#  type-aliases-package: com.concrete.boot.dao.entity
#  configuration:
#    map-underscore-to-camel-case: true

application.yml中 configuration 和 configLocation 两个属性不能同时存在,否则会报错:
Property 'configuration' and 'configLocation' can not specified with together

上一篇 下一篇

猜你喜欢

热点阅读