IBatis和Mybatis、Mybatis-Plus

本文介绍了iBATIS、Mybatis和Mybatis-Plus三个持久层框架。iBATIS最初是Apache的开源项目,后来演变为MyBatis,是一个简化数据库操作的Java框架。Mybatis进一步封装了JDBC,提供了XML或注解配置SQL,简化了开发工作。Mybatis-Plus在MyBatis基础上进行了增强,提供了更多便捷的功能,如Lambda表达式支持、自动生成代码和强大的CRUD操作。

目录

iBATIS

Mybatis

Mybatis-Plus


iBATIS

iBATIS一词来源于"internet"和"abatis"的组合,是apache的一个开源项目(由Clinton Begin在2001年发起的开放源代码项目)。 于2010年6月16号这个项目由apache software foundation 迁移到了google code(被谷歌托管)并且改名为MyBatis ,2013年11月迁移到Github。是一个基于SQL映射支持Java和.NET的持久层框架,使用XML或注解来配置和映射原生信息,将接口和Java的POJO(Plain Ordinary Java Object,普通的Java对象)映射成数据库中的记录。查看ibatis官网:

iBATIS HomeiBATIS

<think> 我们被要求回答关于Spring Boot 3接入Mybatis-PlusMybatis-Enhance-ACTable的方法。首先,我们需要明确这两个库的作用: - Mybatis-Plus:是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,简化开发、提高效率。 - Mybatis-Enhance-ACTable:是一个通过注解自动生成数据库表的工具,它基于MyBatis,可以自动根据实体类创建或更新数据库表结构。 根据引用内容,我们得知: 1. Mybatis-Enhance-ACTable支持Spring Boot传统Spring MVC(引用[1])。 2. Mybatis-Plus在Spring Boot中的基本使用(引用[2])。 3. 在Spring Boot中整合Mybatis-Plus定时器(引用[3])以及Mybatis-Enhance-ACTable创建表的示例(引用[4])。 由于Spring Boot 3已经发布,它要求Java 17+,并且一些依赖可能有版本变化。因此,我们需要选择与Spring Boot 3兼容的Mybatis-PlusMybatis-Enhance-ACTable版本。 步骤: 1. 创建Spring Boot 3项目,选择Java 17或更高版本。 2. 添加Mybatis-PlusMybatis-Enhance-ACTable的依赖。 根据引用[2][3],Mybatis-Plus在Spring Boot中通常使用mybatis-plus-boot-starter。但需要注意,目前(截至2023年)Mybatis-Plus的官方版本中,3.5.x版本支持Spring Boot 2.x,而Spring Boot 3需要更高版本的Mybatis-Plus。目前Mybatis-Plus已经发布了支持Spring Boot 3的版本(例如,从3.5.7开始有实验性支持,但推荐使用更高版本如3.5.13+或4.x以上)。 另外,Mybatis-Enhance-ACTable的版本也需要支持Spring Boot 3。我们可以查看其官方文档或Maven仓库来确定兼容版本。 由于引用中没有直接给出Spring Boot 3的配置,我们根据经验进行如下步骤: 步骤1:创建Spring Boot 3项目 可以使用Spring Initializr(https://start.spring.io/)创建,选择Spring Boot 3.x,Java 17,并添加必要的依赖(如Web、JDBC等)。 步骤2:添加Mybatis-Plus依赖 在pom.xml中添加: ```xml <!-- Mybatis-Plus Spring Boot 3 Starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.6</version> <!-- 注意:这里需要确认最新版本支持Spring Boot 3 --> <!-- 实际上,截至2024年,Mybatis-Plus的3.5.6版本可能不支持Spring Boot 3,需要更高版本如4.0.0以上 --> <!-- 请根据实际情况调整版本号 --> </dependency> ``` 步骤3:添加Mybatis-Enhance-ACTable依赖 根据引用[1][4],我们可以添加: ```xml <!-- Mybatis Enhance ACTable --> <dependency> <groupId>com.gitee.sunchenbin.mybatis.actable</groupId> <artifactId>mybatis-enhance-actable</artifactId> <version>1.5.0.RELEASE</version> <!-- 需要确认该版本是否支持Spring Boot 3 --> </dependency> ``` 注意:由于Spring Boot 3较新,可能这两个库的兼容版本还在更新中,因此需要查看官方文档或Maven仓库中的最新版本信息。 步骤4:配置数据源Mybatis-Plus 在application.yml或application.properties中配置数据源Mybatis-Plus相关属性。 例如,application.yml: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # Mybatis-Plus配置 mybatis-plus: configuration: # 开启驼峰命名 map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 步骤5:配置Mybatis-Enhance-ACTable 根据引用[1][4],我们需要配置ACTable的自动建表功能。在配置类中添加: ```java import org.springframework.context.annotation.Configuration; import com.gitee.sunchenbin.mybatis.actable.manager.handler.StartUpHandler; @Configuration public class ActableConfig { // 注入StartUpHandler,系统启动时会自动扫描带有@Table注解的实体类并创建或更新表 @Bean public StartUpHandler startUpHandler() { return new StartUpHandler(); } } ``` 同时,在实体类上使用@Table注解,例如: ```java import com.gitee.sunchenbin.mybatis.actable.annotation.Table; @Table(name = "users") public class User { // 字段定义,使用@Column等注解 } ``` 步骤6:创建Mapper接口 使用Mybatis-Plus的BaseMapper: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.example.learn.bean.User; // 实体类 @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 步骤7:在启动类上添加@MapperScan注解扫描Mapper接口 ```java import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mapper") // 替换为你的Mapper接口所在包 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 注意事项: 1. 版本兼容性:由于Spring Boot 3较新,需要确保使用的Mybatis-PlusMybatis-Enhance-ACTable版本支持Spring Boot 3。如果不支持,可能需要等待更新或考虑降级Spring Boot版本。 2. 冲突:Mybatis-PlusMybatis-Enhance-ACTable可能都依赖于Mybatis,注意版本冲突,排除不需要的依赖。 如果遇到版本不兼容问题,可以尝试以下方案: - 使用Mybatis-Plus的4.x版本,该版本支持Spring Boot 3(如4.3.5+)。 - 查看Mybatis-Enhance-ACTable的GitHub或Gitee仓库,看是否有支持Spring Boot 3的分支或版本。 由于引用中没有提供确切的版本,这里只能给出一般步骤。实际开发中,请根据依赖库的最新文档进行配置。 最后,生成相关问题: 1. Spring Boot 3中如何解决Mybatis-PlusMybatis-Enhance-ACTable的版本冲突? 2. 除了Mybatis-Enhance-ACTable,还有哪些自动建表的工具可以在Spring Boot 3中使用? 3. 在Spring Boot 3中使用Mybatis-Plus有哪些新特性? 注意:由于我们无法确定用户使用的确切版本,因此在实际操作中需要用户自行调整版本号以匹配Spring Boot 3。 </think> ### Spring Boot 3 集成 Mybatis-Plus Mybatis-Enhance-ACTable 方法 #### 一、环境准备 1. **基础依赖** 在 `pom.xml` 中添加 Spring Boot 3 Mybatis 核心依赖: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> </parent> <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Mybatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.6</version> </dependency> <!-- Mybatis-Enhance-ACTable --> <dependency> <groupId>com.gitee.sunchenbin.mybatis.actable</groupId> <artifactId>mybatis-enhance-actable</artifactId> <version>1.5.0.RELEASE</version> </dependency> </dependencies> ``` #### 二、配置自动建表 1. **配置文件 (`application.yml`)** ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver # Mybatis-Enhance-ACTable 配置 actable: table: auto: update # 自动更新表结构 model-pack: com.example.entity # 实体类包路径 ``` 2. **启动类配置** 添加注解启用自动建表功能: ```java import com.gitee.sunchenbin.mybatis.actable.manager.handler.StartUpHandler; import org.springframework.context.annotation.Bean; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public StartUpHandler startUpHandler() { return new StartUpHandler(); // 激活自动建表[^4] } } ``` #### 三、实体类与 Mapper 配置 1. **实体类示例** 使用注解定义表结构: ```java import com.gitee.sunchenbin.mybatis.actable.annotation.*; @Table(name = "user") // 指定表名 public class User { @IsKey // 主键标识 @Column private Long id; @Column(comment = "用户名") // 字段注释 private String username; } ``` 2. **Mapper 接口** 继承 Mybatis-Plus 的 `BaseMapper`: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.example.entity.User; @Mapper public interface UserMapper extends BaseMapper<User> { // 自动获得CRUD方法[^2] } ``` #### 四、验证与日志输出 启动应用后,控制台将输出建表日志: ``` 2024-06-20 10:00:00.001 INFO [main] c.g.s.m.manager.SysMysqlCreateTableManager 开始创建表:user 2024-06-20 10:00:00.500 INFO [main] c.g.s.m.manager.SysMysqlCreateTableManager 完成创建表:user[^4] ``` #### 五、关键注意事项 1. **版本兼容性** - Mybatis-Plus ≥3.5.6 支持 Spring Boot 3 - Mybatis-Enhance-ACTable ≥1.5.0 兼容 JDK 17 2. **冲突解决** 若存在依赖冲突,在 `pom.xml` 中排除旧版 Mybatis: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> </exclusions> </dependency> ``` --- ### 相关问题 1. 如何解决 Mybatis-Plus Mybatis-Enhance-ACTable 的注解冲突? 2. Spring Boot 3 中如何配置 Mybatis-Enhance-ACTable 的多数据源? 3. Mybatis-Enhance-ACTable 支持哪些数据库类型? [^1]: [Mybatis-Enhance-ACTable 核心功能](https://gitee.com/sunchenbin/mybatis-enhance) [^2]: [Mybatis-Plus 基础文档](https://baomidou.com) [^3]: [Spring Boot 官方文档](https://spring.io/projects/spring-boot) [^4]: [ACTable 自动建表日志输出示例](https://gitee.com/sunchenbin/mybatis-enhance
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

骆驼整理说

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

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

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

打赏作者

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

抵扣说明:

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

余额充值