springboot整合MyBatis

本文详细介绍了如何在SpringBoot项目中整合MyBatis,包括配置数据源、添加依赖、扫描Mapper,以及使用MyBatisGenerator自动生成代码。同时,解决了常见问题如SQL打印和Mapper找不到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. springboot整合MyBatis

1.1. application.properties中配置整合mybatis的配置文件、mybatis扫描别名的基本包与数据源

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/vue-admin?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000

#mybatis.config-location=mybatis-config.xml配置文件的路径
#mybatis.type-handlers-package=扫描typeHandlers的包
#mybatis.check-config-location=检查配置文件是否存在
#mybatis.executor-type=设置执行模式(SIMPLE, REUSE, BATCH),默认为SIMPLE
#mybatis.config-location=classpath*:mybatis-config.xml
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.vue.admin.entity

1.2. pom.xml加入springboot整合mybatis的jar包与数据库驱动包

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.29</version>
</dependency>

1.3. 注意Mapper的扫描

步骤一:

在springboot的配置文件application.properties中添加mybatis的配置,如下所示:

    mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
    mybatis.type-aliases-package=com.vue.admin.entity

步骤二:

①将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法。(没测试)
②或者在启动类上加上@MapperScan或者@ComponentScan注解,手动指定application类要扫描哪些包下的注解,如下所示:

     @SpringBootApplication
     @ComponentScan(basePackages = {"com.xxx.xxx.dao"})

③或者在接口上添加@Mapper注解。

     @Mapper
     public interface UserMapper {
     }

2. MyBatis Generator

MyBatis Generator是一个Mybatis的代码生成器。支持Mybatis的所有版本。通过MyBatis Generator可以根据数据库表生成相应的实体、sql映射文件、Dao等,能应付简单的CRUD

2.1. 引入MyBatis Generator的maven插件

pom.xml

<build>
    <plugins>
        <!--mybatis自动生成代码插件-->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.6</version>
            <configuration>
                <!-- 是否覆盖,true表示会替换生成的JAVA文件,false则不覆盖 -->
                <overwrite>true</overwrite>
            </configuration>
            <dependencies>
                <!--mysql驱动包-->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.45</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

2.2. MyBatis Generator配置文件

简单配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 导入配置文件 -->
    <properties resource="application.properties"/>

    <!-- defaultModelType="flat" 设置复合主键时不单独为主键创建实体 -->
    <context id="MySql" defaultModelType="flat">
        <!-- 生成的POJO实现java.io.Serializable接口 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <!--注释-->
        <commentGenerator>
            <!-- 将数据库中表的字段描述信息添加到注释 -->
            <property name="addRemarkComments" value="true"/>
            <!-- 注释里不添加日期 -->
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!-- 数据库连接,直接通过${}读取application.properties里的配置 -->
        <jdbcConnection
                driverClass="${spring.datasource.driver-class-name}"
                connectionURL="${spring.datasource.url}"
                userId="${spring.datasource.username}"
                password="${spring.datasource.password}"/>

        <!-- 生成POJO对象,并将类放到com.songguoliang.springboot.entity包下 -->
        <javaModelGenerator targetPackage="com.vue.admin.entity" targetProject="src/main/java"></javaModelGenerator>
        <!-- 生成mapper xml文件,并放到resources下的mapper文件夹下 -->
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources"></sqlMapGenerator>


        <!-- 生成mapper xml对应dao接口,放到targetPackage包下-->
        <javaClientGenerator targetPackage="com.vue.admin.mapper" targetProject="src/main/java" type="XMLMAPPER"></javaClientGenerator>

        <!-- table标签可以有多个,至少一个,tableName指定表名,可以使用_和%通配符 -->
        <table tableName="sys_user">
            <!-- 是否只生成POJO对象 -->
            <property name="modelOnly" value="false"/>
            <!-- 数据库中表名有时我们都会带个前缀,而实体又不想带前缀,这个配置可以把实体的前缀去掉 -->
            <!--<domainObjectRenamingRule searchString="^Tbl" replaceString=""/>-->

        </table>
    </context>
</generatorConfiguration>

application.properties中配置

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/vue-admin?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000

#mybatis.config-location=mybatis-config.xml配置文件的路径
#mybatis.type-handlers-package=扫描typeHandlers的包
#mybatis.check-config-location=检查配置文件是否存在
#mybatis.executor-type=设置执行模式(SIMPLE, REUSE, BATCH),默认为SIMPLE
#mybatis.config-location=classpath*:mybatis-config.xml
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.vue.admin.entity

2.3. Maven执行插件

在这里插入图片描述

3. 问题记录

3.1. SpringBoot中Mybatis打印sql

如果使用的是application.properties文件,加入如下配置:

logging.level.com.example.demo.dao=debug

logging.level.com指的是mybatis对应的方法接口所在的包。并不是mapper.xml所在的包。

如果使用的是application.yml文件,加入如下配置:

# 打印sql
logging:
  level:
     com.example.demo.dao : debug

3.2. Mapper could not be found

required a bean of type 'com.xxx.xxx.dao.UserEntityMapper' that could not be found.

原因:Mapper没有成功注入进去、@SpringBootApplication没有扫描到

解决方式:

步骤一:

在springboot的配置文件application.properties中添加mybatis的配置,如下所示:

    mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
    mybatis.type-aliases-package=com.vue.admin.entity

步骤二:

①将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法。(没测试)
②或者在启动类上加上@MapperScan或者@ComponentScan注解,手动指定application类要扫描哪些包下的注解,如下所示:

     @SpringBootApplication
     @ComponentScan(basePackages = {"com.xxx.xxx.dao"})

③或者在接口上添加@Mapper注解。

     @Mapper
     public interface UserMapper {
     }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值