01.初识TK.Mybatis插件

Mybatis Tk插件环境搭建

一、概述

最近在一家公司实习,他们用的Mybatis框架,以前都是在XML文件写SQL,同事们都使用TK插件,绝大部分sql都由TK帮我们集成好了,所以来学习哈。下面基于SpringBoot搭建环境的。

二、Maven依赖

		
            tk.mybatis
            mapper-spring-boot-starter
            1.1.3
        
        
            mysql
            mysql-connector-java
        
        
        
            org.mybatis
            mybatis-typehandlers-jsr310
            1.0.1
        
        
         
            org.springframework.boot
            spring-boot-starter-test
            test
        

三、appliaction.yml配置

在这里,我们需要配置datasource,还配置sql日志,以便于我们观察执行sql语句。

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: your username
    password: your password
    url: jdbc:mysql://localhost/tk_test?characterEncoding=utf-8&useSSL=false
    jpa:
      show-sql: true
      
# 打印sql语句
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

四、创建数据表

创建一张Product表

CREATE TABLE `product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_name` varchar(255) NOT NULL COMMENT '商品名称',
  `price` decimal(10,0) NOT NULL COMMENT '商品价格',
  `in_time` datetime NOT NULL COMMENT '入库时间',
  `out_time` datetime NOT NULL COMMENT '出库时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO product(product_name, price, in_time, out_time) VALUES('Redis深度历险记', 79.00, '2019-12-21 18:22:20', '2019-12-23 18:22:20');
INSERT INTO product(product_name, price, in_time, out_time) VALUES('Java 8 函数编程', 56.00, '2019-12-24 18:22:20', '2019-12-26 18:22:20');
INSERT INTO product(product_name, price, in_time, out_time) VALUES('剑指offer', 67.00, '2019-12-27 18:22:20', '2019-12-29 18:22:20');

五、查询

  • 创建Java Bean
package com.example.test.tk.bean;

import lombok.Data;
import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
@Table(name = "product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String productName;
    
    private BigDecimal price;

    private LocalDateTime inTime;
    
    private LocalDateTime outTime;
}

  • 创建ProductDAO
package com.example.test.tk.dao;

import com.example.test.tk.bean.Product;
import tk.mybatis.mapper.common.Mapper;

@org.apache.ibatis.annotations.Mapper
public interface ProductDao extends Mapper {
}

  • 单元测试
    查询所有记录
package com.example.test.tk.dao;

import com.example.test.tk.bean.Product;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
class ProductDaoTest {
    @Autowired
    private ProductDao productDao;

    @Test
    public void selectAll(){
        List productList = productDao.selectAll();
        Assert.assertEquals(3, productList.size());
    }
 }

控制台打印日志,如下

==>  Preparing: SELECT id,product_name,price,in_time,out_time FROM product 
==> Parameters: 
<==    Columns: id, product_name, price, in_time, out_time
<==        Row: 1, Redis深度历险记, 79, 2019-12-21 18:22:20, 2019-12-23 18:22:20
<==        Row: 2, Java 8 函数编程, 56, 2019-12-24 18:22:20, 2019-12-26 18:22:20
<==        Row: 3, 剑指offer, 67, 2019-12-27 18:22:20, 2019-12-29 18:22:20
<==      Total: 3

六、最后

在下一章,将介绍TK其他查询方法。感谢欣赏。

你可能感兴趣的:(Mybatis插件,TK)