通用 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


©著作权归作者所有:来自51CTO博客作者mob604756f1e4c7的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 【每天一题】PHP中常用的数组操作方法笔记整理
  2. jQuery初学习之dom操作
  3. jQuery常用dom操作之验证用户名
  4. 统信UOS中普通用户忘记密码处理方法
  5. 对误操作说“NO”,DevOps 三十六计之日常运维
  6. 永久免费!国产操作系统 Deepin V20 Beta版发布(附安装教程)
  7. JSON 与 JSONB
  8. 互联网保险的疯狂试探-互助宝的骚操作
  9. ProBuilder快速原型开发技术 ---进阶功能操作

随机推荐

  1. Python能不能方便的画三角形?
  2. win8.1 cygwin - pip正在安装到windows p
  3. 使用Python启动浏览器(Chromium)并更改URL
  4. python 读写文本文件
  5. 用 Python 分析胡歌的《猎场》到底值不值
  6. Python: sorted和sort的区别
  7. Python闭包需要注意的问题
  8. 使用python将图片转换为字符图片
  9. 如何按期执行python脚本?
  10. python 3.3 爬虫之爬取图片