初识 tk.mybatis.mapper 通用mapper
在博客园发表Mybatis Dynamic Query后,一位园友问我知不知道通用mapper,仔细去找了一下,还真的有啊,比较好的就是abel533写的tk.mybatis.mapper。
本次例子地址:https://github.com/wz2cool/tk-mybatis-demo

传统Mybatis用法
Spring boot
引用基本的jar到pom
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
sql 数据准备
DROP TABLE IF EXISTS category;
CREATE TABLE category (
category_id INT PRIMARY KEY,
category_name VARCHAR (50) NOT NULL,
description VARCHAR (100)
);
DROP TABLE IF EXISTS product;
CREATE TABLE product (
product_id INT PRIMARY KEY auto_increment,
category_id INT NOT NULL,
product_name VARCHAR (50) NOT NULL,
price DECIMAL
);
DELETE FROM category;
INSERT INTO category (category_id, category_name, description) VALUES
(1, 'Beverages', 'test'),
(2, 'Condiments', 'test'),
(3, 'Oil', 'test');
DELETE FROM product;
INSERT INTO product (product_id, category_id, product_name, price) VALUES
(1, 1, 'Northwind Traders Chai', 18.0000),
(2, 2, 'Northwind Traders Syrup', 7.5000),
(3, 2, 'Northwind Traders Cajun Seasoning', 16.5000),
(4, 3, 'Northwind Traders Olive Oil', 16.5000),
(5, 3, 'Northwind Traders Olive Oil2', 16.5000);
entity
public class Product {
private Integer productID;
private String productName;
private BigDecimal price;
private Integer categoryID;
// get/set...
}
@Table(name = "category")
public class Category {
@Id
@Column(name = "category_id")
private Integer categoryID;
private String categoryName;
private String description;
// get /set...
}
Dao
这里的ProductDao 是传统的mybatis的用法。
@Mapper
public interface ProductDao {
List<Product> getProducts();
}
mapper
传统mybatis 我们必须有个xml 文件和Dao 对应起来, tk.maybatis.mapper无需此文件。
<mapper namespace="com.github.wz2cool.demo.tk.mybatis.mapper.ProductDao">
<select id="getProducts" resultType="com.github.wz2cool.demo.tk.mybatis.model.entity.table.Product">
SELECT * FROM product
</select>
</mapper>
设置扫描包
@SpringBootApplication
@MapperScan(basePackages = "com.github.wz2cool.demo.tk.mybatis.mapper")
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
application.properies
注意这里的配置只是针对我们传统mybatis配置,对于tk.maybatis.mapper可以省略这里的配置。
mybatis.type-aliases-package=com.github.wz2cool.demo.tk.mybatis.mapper
mybatis.mapper-locations=classpath:com.github.wz2cool.demo.tk.mybatis.mapper/*.xml
测试
我们可以简单调用一下
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestApplication.class)
public class SimpleTest {
@Autowired
private ProductDao productDao;
@Test
public void testSelect() throws Exception {
List<Product> productList = productDao.getProducts();
assertEquals(true, productList.size() > 0);
}
}
我们可以看到输出结果,基本上就通了
==> Preparing: SELECT * FROM product
==> Parameters:
<== Columns: PRODUCT_ID, CATEGORY_ID, PRODUCT_NAME, PRICE
<== Row: 1, 1, Northwind Traders Chai, 18.0000
<== Row: 2, 2, Northwind Traders Syrup, 7.5000
<== Row: 3, 2, Northwind Traders Cajun Seasoning, 16.5000
<== Row: 4, 3, Northwind Traders Olive Oil, 16.5000
<== Row: 5, 3, Northwind Traders Olive Oil2, 16.5000
<== Total: 5
tk.mybatis.mapper 用法
添加引用
需要多添加两个引用 (ps:曾经少了一个mapper-spring-boot-starter,报错死活找不到为什么--!!!)
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.2</version>
</dependency>
<!--mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.1.3</version>
</dependency>
继承通用mapper
嗯 是的没有xml 文件。
public interface CategoryDao extends Mapper<Category> {
}
测试调用
dao 里面自带很多方法,比如 selectAll(), insert().
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestApplication.class)
public class SimpleTkMapperTest {
@Autowired
private CategoryDao categoryDao;
@Test
public void selectAllTest() {
List<Category> categories = categoryDao.selectAll();
assertEquals(true, categories.size() > 0);
}
@Test
public void insertTest() {
Category newCategory = new Category();
newCategory.setCategoryID(1000);
newCategory.setCategoryName("test");
newCategory.setDescription("for test");
int result = categoryDao.insert(newCategory);
assertEquals(1, result);
}
}
输出结果
==> Preparing: SELECT category_id,category_name,description FROM category
==> Parameters:
<== Columns: CATEGORY_ID, CATEGORY_NAME, DESCRIPTION
<== Row: 1, Beverages, test
<== Row: 2, Condiments, test
<== Row: 3, Oil, test
<== Total: 3
==> Preparing: INSERT INTO category ( category_id,category_name,description ) VALUES( ?,?,? )
==> Parameters: 1000(Integer), test(String), for test(String)
<== Updates: 1
tk.mybatis.mapper 初步使用感受
优势
- 无需xml文件和Dao 对应。
- 无需指定xml 文件位置(因为就没有xml文件)。
- 自带多种常用方法。
困惑(仅代表个人观点)
- 自带的方法多但是有些方法理解很晦涩,比如 deleteByExample(Object var1),这里给一个object 我都不知道要填什么,是填写主键呢还是这个实体呢?(后来了解其实example 应该是一个筛选条件)
tk.mybatis.mapper(Criteria) 筛选
在理解Example 以后,就开始了解了 Criteria 筛选,当然Example 还是可以做排序的。
来看看 tk.mybatis.mapper 筛选是如何做的
@Test
public void selectByExampleTest() {
Example example = new Example(Category.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("categoryID", 1);
criteria.orEqualTo("categoryID", 2);
categoryDao.selectByExample(example);
}
输出结果
==> Preparing: SELECT category_id,category_name,description FROM category WHERE ( category_id = ? or category_id = ? )
==> Parameters: 1(Integer), 2(Integer)
<== Columns: CATEGORY_ID, CATEGORY_NAME, DESCRIPTION
<== Row: 1, Beverages, test
<== Row: 2, Condiments, test
<== Total: 2
结束
这个只是我初次使用tk.mybatis.mapper 确实不错,可以减少工作量,好像还有可以生成Dao和mapper 的工具,这个我以后再试试看。
要有自己的Mapper
看了 tk.mybatis.mapper 以后,觉得通用mapper 也应该加入到 Mybatis Dynamic Query 中去,当然实现可能有点不一样。
关于 tk.mybatis.mapper 问题
这里真的想问问园友了
- tk.mybatis.mapper 支持多表筛选么(join),我自己好像没有找到。
- tk.mybatis.mapper 支持组么,就是类似于 (id > 1 and id < 5) and price = 10.
关注我 ##
最后大家可以关注我和 Mybatis-Dynamic-query项目 _
Follow @wz2cool Star Fork
初识 tk.mybatis.mapper 通用mapper的更多相关文章
- 初识 tk.mybatis.mapper
在博客园发表Mybatis Dynamic Query后,一位园友问我知不知道通用mapper,仔细去找了一下,还真的有啊,比较好的就是abel533写的tk.mybatis.mapper. 本次例子 ...
- Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x
Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...
- 扩展mybatis和通用mapper,支持mysql的geometry类型字段
因项目中需要用到地理位置信息的存储.查询.计算等,经过研究决定使用mysql(5.7版本)数据库的geometry类型字段来保存地理位置坐标,使用虚拟列(Virtual Generated Colum ...
- MyBatis插件 - 通用mapper
1.简单认识通用mapper 1.1.了解mapper 作用:就是为了帮助我们自动的生成sql语句 [ ps:MyBatis需要编写xxxMapper.xml,而逆向工程是根据entity实体类来进行 ...
- 使用mybatis-generator插件结合tk.mybatis自动生成mapper二三事
本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑. ...
- 使用mybatis-generator插件结合tk.mybatis自动生成mapper
本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑. ...
- (二、下) springBoot 、maven 、mysql、 mybatis、 通用Mapper、lombok 简单搭建例子 《附项目源码》
接着上篇文章中 继续前进. 一.在maven 的pom.xm中添加组件依赖, mybatis通用Mapper,及分页插件 1.mybatis通用Mapper <!-- mybatis通用Mapp ...
- Spring Boot集成Mybatis及通用Mapper
集成Mybatis可以通过 mybatis-spring-boot-starter 实现. <!-- https://mvnrepository.com/artifact/org.mybatis ...
- mybatis的通用mapper小结
import tk.mybatis.mapper.entity.Example; //此包是tk下的1.定义一个dao层接口不需要任何方法 需要继承Mapper<类型> 2.在servic ...
随机推荐
- [20180403]访问dba_autotask_task无输出问题.txt
[20180403]访问dba_autotask_task无输出问题.txt --//链接http://www.itpub.net/thread-1911421-1-1.html的讨论,还没注意原先的 ...
- 洗礼灵魂,修炼python(52)--爬虫篇—【转载】爬虫工具列表
与爬虫相关的常用模块列表. 原文出处:传送门链接 网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pycurl). pycurl – 网络 ...
- python socket 套接字编程 单进程服务器 实现多客户端访问
服务器: import socket #单进程服务器 实现多客户端访问 IO复用 #吧所有的客户端套接字 放在一个列表里面,一次又一次的便利过滤 server = socket.socket(sock ...
- 用Python实现数据结构之二叉搜索树
二叉搜索树 二叉搜索树是一种特殊的二叉树,它的特点是: 对于任意一个节点p,存储在p的左子树的中的所有节点中的值都小于p中的值 对于任意一个节点p,存储在p的右子树的中的所有节点中的值都大于p中的值 ...
- js,ajax,layer笔记(弹出层,在弹出一个弹框)
整体认识: 因为作用域的问题,js 在页面初次加载时已近加载好了,所以要有第二次弹窗的效果,必须得在第一次成功之后再次让他加载js 代码: /*shaun*/showdetailsPag: funct ...
- 【PAT】B1043 输出PATest(20 分)
/* */ #include<stdio.h> #include<algorithm> #include<string.h> #include<ctype.h ...
- JS学习小结(上)
学而时习之,不亦说乎,开启JS学习新乐章~ JS是干啥的?网页特效,它主要是实现控制结构和样式,是一种行为,有多重要,不言而喻吧,页面炫酷的资本. 1. JS输出: alert("hello ...
- flex布局下el-table横向滚动条失效
如下图,是一种常见的页面结构,我们可以有很多方法实现,inline-block,float,flex等等 但是,最近项目中遇到一个怪事,左边是侧边栏导航,右边是一个数据展示table,el-table ...
- Xpath语法-爬虫(一)
前言 这一章节主要讲解Xpath的基础语法,学习如何通过Xpath获取网页中我们想要的内容;为我们的后面学习Java网络爬虫基础准备工作. 备注:此章节为基础核心章节,未来会在网络爬虫的数据解析环节经 ...
- Excel函数(不定期持续更新)
1.COUNTIF函数 COUNTIF函数用来计算单元格区域内符合条件的单元格个数. COUNTIF函数只有两个参数 COUNTIF(单元格区域,计算的条件) 例如:计算上海市的数量