其他分享
首页 > 其他分享> > SpringBoot集成tkMybatis插件

SpringBoot集成tkMybatis插件

作者:互联网

  通用 Mapper4 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及Example相关的单表操作。通用 Mapper 是为了解决 MyBatis 使用中 90% 的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。

仓库:https://github.com/abel533/Mapper


0x01:引入TkMybatis的Maven依赖

    <!--添加tk-mapper依赖-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>


0x02:实体类的相关配置@Id、@Table

package com.crazy.gateway.dto;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * 
 * @author 2230
 *
 */
@Table(name="t_gateway_auth_channel")
public class GatewayAuthChannelDto implements Serializable{

    private static final long serialVersionUID = 6977508213718572302L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "JDBC")
    private Long id;

    private String channelCode;

    private String appkey;

    private String appsecret;

    /**
     * 启用状态 0禁用 1启用
     */
    private int enableStatus;

    /**
     * 公钥
     */
    private String pubKey;

    /**
     * 私钥
     */
    private String priKey;

    /**
     * 加密类型 rsa, aes
     */
    private String encryptType;

    /**
     * 删除状态 0删除 1正常
     */
    private String status;

    /**
     * 说明
     */
    private String remark;

    private Date createDate;

    private String createUser;

    private Date updateDate;

    private String updateUser;

    //省略 get  set

}


0x03:Mapper继承tkMabatis的Mapper接口

package com.crazy.gateway.mapper;


import org.apache.ibatis.annotations.Select;

import com.crazy.gateway.dto.GatewayAuthChannelDto;
import com.github.pagehelper.Page;

import tk.mybatis.mapper.common.Mapper;

/**
 * 网关 appkey  appsecret 认证
 * 
 * @author 2230
 *
 */
public interface GatewayAuthChannelMapper extends Mapper<GatewayAuthChannelDto> {

     @Select("SELECT * FROM t_gateway_auth_channel")
    Page<GatewayAuthChannelDto> getGatewayAuthChannelPageList();
}


0x04:启动类Application或自定义Mybatis配置类上使用@MapperScan注解扫描Mapper接口

package com.crazy.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import tk.mybatis.spring.annotation.MapperScan;

@EnableEurekaClient
@MapperScan("com.crazy.gateway.mapper")
@SpringBootApplication
public class ServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }

}


0x05:在application.yml配置文件中,配置mapper.xml文件指定的位置

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.crazy.gateway.dto


0x06: 使用tkMybatis提供的通用方法执行sql

package com.crazy.gateway.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.crazy.gateway.dto.GatewayAuthChannelDto;
import com.crazy.gateway.form.GatewayAuthPageForm;
import com.crazy.gateway.mapper.GatewayAuthChannelMapper;
import com.crazy.gateway.service.GatewayAuthChannelService;

@Service
public class GatewayAuthChannelServiceImpl implements GatewayAuthChannelService {

    private Logger log = LoggerFactory.getLogger(GatewayAuthChannelServiceImpl.class);

    @Autowired
    private GatewayAuthChannelMapper gatewayAuthChannelMapper;

    @Override
    public boolean addGatewayAuthChannel(GatewayAuthChannelDto gatewayAuthChannelDto) {
        int result = gatewayAuthChannelMapper.insert(gatewayAuthChannelDto);
        return result > 0;
    }

    @Override
    public boolean editGatewayAuthChannel(GatewayAuthChannelDto gatewayAuthChannelDto) {
        int result = gatewayAuthChannelMapper.updateByPrimaryKey(gatewayAuthChannelDto);
        return result > 0;
    }

    @Override
    public boolean delGatewayAuthChannel(Long id) {
        int result = gatewayAuthChannelMapper.deleteByPrimaryKey(id);
        return result > 0;
    }

}


tkMybatis插件对应单表操作相对强大,对减少工作有非常大的作用。更详细的说明可以参考:

https://www.oschina.net/news/94347/mapper-4-0-released


标签:插件,SpringBoot,tkMybatis,crazy,private,import,com,gateway,String
来源: https://blog.51cto.com/15127574/2667696