SpringBoot项目里,让TKmybatis支持可以手写sql的Mapper.xml文件

SpringBoot项目通常配合TKMybatis或MyBatis-Plus来做数据的持久化。

对于单表的增删改查,TKMybatis优雅简洁,无需像传统mybatis那样在mapper.xml文件里定义sql。

 

我们目前的项目呢,有一些数据分析的需求,涉及到多表关联、嵌套子查询等复杂的sql。

那么,TKMybatis是不是可以支持手写sql呢?

答案是yes!

 

 

我们知道,springboot集成tk-mybatis需添加2个依赖:


tk.mybatis
mapper-spring-boot-starter
2.1.5



tk.mybatis
mapper
4.1.5

执行dependency:tree时查看依赖关系,可以看到mapper-spring-boot-starter.jar同时依赖了org.mybatis。

[INFO] +- tk.mybatis:mapper-spring-boot-starter:jar:2.1.5:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.1.7.RELEASE:compile
[INFO] |  |  +- com.zaxxer:HikariCP:jar:3.2.0:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:5.1.9.RELEASE:compile
[INFO] |  +- org.mybatis:mybatis:jar:3.4.6:compile
[INFO] |  +- org.mybatis:mybatis-spring:jar:1.3.2:compile
[INFO] |  +- tk.mybatis:mapper-core:jar:1.1.5:compile
[INFO] |  +- tk.mybatis:mapper-base:jar:1.1.5:compile
[INFO] |  +- tk.mybatis:mapper-weekend:jar:1.1.5:compile
[INFO] |  +- tk.mybatis:mapper-spring:jar:1.1.5:compile
[INFO] |  +- tk.mybatis:mapper-extra:jar:1.1.5:compile
[INFO] |  \- tk.mybatis:mapper-spring-boot-autoconfigure:jar:2.1.5:compile

 

那么,在springboot项目里,如果要实现mybatis那样的在mapper文件里手写sql,如下几步轻松搞定。

 

1)application.yml里:

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

 

2)在resources/mapper目录下创建 SpiderDraftsMapper.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.draft.dao.SpiderDraftsMapper">
    <resultMap id="columnMap" type="com.draft.entity.SpiderDrafts">
        <result column="order_id" property="orderId" jdbcType="VARCHAR"/>
        <result column="interest_days" property="interestDays"/>
        <result column="draft_owner" property="draftOwner"/>
    resultMap>
    <select id="getfoo" resultMap="columnMap">
        select * from spider_drafts
        where order_id in(select order_id from spider_draft)
    select>
    <select id="getfoo1" resultType="com.draft.entity.SpiderDrafts">
        select interest_days, order_id as orderId,draft_owner from spider_drafts
        where order_id in(select order_id from spider_draft)
    select>
mapper>

 

 

3)在com.draft.entity包下,创建pojo实体类 SpiderDrafts.java(可以用代码生成器生成):

package com.draft.entity;

import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;

@Data
//@Table(name = "spider_drafts")
@Entity
public class SpiderDrafts implements Serializable {
//    @Column(name = "interest_days")
    private String interestDays;
    private String orderId;
    private String draftOwner;
}

 

4)数据访问接口类统一定义在com.draft.dao包下,项目启动程序直接通过@MapperScan("com.draft.dao")来进行扫描。 如下是SpiderDraftsMapper.java: 

package com.draft.dao;

import com.draft.entity.SpiderDrafts;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface SpiderDraftsMapper extends Mapper {
    List getfoo();

    List getfoo1();
}

 

 

以上几步,完美搞定。单标操作直接用tkmybatis,复杂sql则写在mapper.xml文件里。

注意:getfoo与getfoo1两个方法都是可用的,只是为了测试下划线命名方式的表字段与驼峰式命名的pojo属性的映射(mybatis.configuration.map-underscore-to-camel-case属性),见后文说明。

 

【几点说明】

1)map-underscore-to-camel-case:
数据库里表的字段是下划线命名,pojo的属性是驼峰命名。打开这个属性可实现这种转换。不用再依靠mybatis那样的resultMap了。
    <resultMap id="columnMap" type="com.draft.entity.SpiderDrafts">
        <result column="order_id" property="orderId" jdbcType="VARCHAR"/>
        <result column="interest_days" property="interestDays"/>
        <result column="draft_owner" property="draftOwner"/>
    resultMap>

 

数据库里表的字段是下划线命名,pojo的属性是驼峰命名。另外,打开这个属性后,pojo涉及到下划线式字段与驼峰式属性这种对应关系的,就可以去掉@Table和@Column注解。

2)type-aliases-package:

mapper.xml文件中resultMap的type或者parameterType或者resultType会使用程序里定义的pojo,
此时可以用完全限定名来指定这些POJO的引用,例如