Mybatis Tk插件環境搭建
一、概述
最近在一家公司實習,他們用的Mybatis框架,以前都是在XML文件寫SQL,同事們都使用TK插件,絕大部分sql都由TK幫我們集成好了,所以來學習哈。下面基於SpringBoot搭建環境的。
二、Maven依賴
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--類型處理,沒有此依賴,有可能報no found property異常-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.1</version>
</dependency>
<!--單元測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
三、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<Product> {
}
- 單元測試
查詢所有記錄
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<Product> 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其他查詢方法。感謝欣賞。