SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询

📅 发布时间:2026/7/17 14:53:40 👁️ 浏览次数:
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
01 引言上一节介绍了Apache Calcite使用JDBC客户端统一查询但是DAO几乎已经被Myabits占了半壁江山可不可以通过Mybaits的形式调用呢我们一下来看下吧还会遇到哪些坑点呢follow me02 实战配置2.1 Maven依赖!-- Apache Calcite --dependencygroupIdorg.apache.calcite/groupIdartifactIdcalcite-core/artifactIdversion1.38.0/version/dependencydependencygroupIdorg.apache.calcite/groupIdartifactIdcalcite-csv/artifactIdversion1.38.0/version/dependencydependencygroupIdcom.mysql/groupIdartifactIdmysql-connector-j/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion3.0.4/version/dependency这里列举了主要配置其他的辅助配置按需引入。2.2 配置文件配置mybaits相关的内容。因为我们不写mapper.xml所以就不配置mybatis.mapper-locations。# 日志 mybatis.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl # 开启驼峰匹配 mybatis.configuration.map-underscore-to-camel-casetrue2.3 模型文件模型文件和之前的一致。{version:1.0,defaultSchema:MEM,schemas:[{name:MEM,type:custom,factory:org.apache.calcite.adapter.java.ReflectiveSchema$Factory,operand:{class:com.simonking.boot.calcite.schema.UserSchema}},{name:CSV,type:custom,factory:org.apache.calcite.adapter.csv.CsvSchemaFactory,operand:{directory:csv,flavor:scannable}},{name:MYSQL_DB,type:custom,factory:org.apache.calcite.adapter.jdbc.JdbcSchema$Factory,operand:{jdbcUrl:jdbc:mysql://127.0.0.1:3306/test,jdbcUser:root,jdbcPassword:root}}]}2.4 数据源配置BeanpublicDataSourcecalciteDataSource()throwsException{PropertiespropnewProperties();prop.put(lex,MYSQL);prop.put(model,src/main/resources/calcite-model.json);ConnectionconnectionDriverManager.getConnection(jdbc:calcite:,prop);SingleConnectionDataSourcecalciteDataSourcenewSingleConnectionDataSource(connection,true);// 绑定资源TransactionSynchronizationManager.bindResource(calciteDataSource,newConnectionHolder(connection));returncalciteDataSource;}这里的数据源配置需要注意的代码TransactionSynchronizationManager.bindResource(calciteDataSource, new ConnectionHolder(connection));这里是为了绑定资源否则就会默认提交事务导致报错。因为本身只能查询所以没必要提交事务。报错如下报错源码位置只需要配置上面的代码即可跳过事务的提交。2.5 启动类配置增加Mapper扫描MapperScan(com.simonking.boot.calcite.mapper)03 实战3.1 内存Mybatis应用实体DatapublicclassUserVOimplementsSerializable{SerialprivatestaticfinallongserialVersionUID-8236381006056066976L;/** 用户id */privateIntegerid;/** 用户名 */privateStringname;/** 年龄 */privateIntegerage;}Mapper查询publicinterfaceUserMapper{Select(select * from MEM.users where id #{id})UserVOselectById(Param(id)Integerid);}示例与结果3.2 CSV的Mybatis应用实体DatapublicclassOrderVOimplementsSerializable{SerialprivatestaticfinallongserialVersionUID1063926652351474739L;/** 订单ID */privateIntegerorderId;/** 订单ID */privateIntegeruserId;/** 金额 */privateBigDecimalamount;}Mapper查询publicinterfaceOrderMapper{Select(select * from CSV.orders where order_id #{orderId})OrderVOselectById(Param(orderId)IntegerorderId);}示例与结果3.3 Mysql的Mybaits应用实体DatapublicclassUserRoleVOimplementsSerializable{SerialprivatestaticfinallongserialVersionUID-7359882062781599308L;/** id */privateIntegerid;/** 用户ID */privateIntegeruserId;/** 用户名 */privateStringusername;/** 角色名称 */privateStringrole_name;}Mapper查询publicinterfaceUserRoleMapper{Select(SELECT * FROM MYSQL_DB.user_roles WHERE user_id #{userId})UserRoleVOgetUserRoleByUserId(Param(userId)IntegeruserId);}示例及结果3.4 联查联查我们可以随意使用一个Mapper我们直接使用UserRoleMapper为了方便接受消息我们直接使用JSONObject接收。Mapper查询publicinterfaceUserRoleMapper{Select( SELECT * FROM CSV.orders o INNER JOIN MEM.users u ON o.user_id u.id INNER JOIN MYSQL_DB.user_roles r ON u.id r.user_id WHERE u.id #{userId} )JSONObjectselectByUserId(Param(userId)IntegeruserId);}示例及结果04 小结使用Mybatis的方式更加适合我们日常的编码风格使用的时候基本和单表的查询没有区别到这里坑基本上踩的差不多了老铁们还遇到什么问题评论区唠唠