在博客园发表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 初步使用感受

优势

  1. 无需xml文件和Dao 对应。
  2. 无需指定xml 文件位置(因为就没有xml文件)。
  3. 自带多种常用方法。

困惑(仅代表个人观点)

  1. 自带的方法多但是有些方法理解很晦涩,比如 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 问题

这里真的想问问园友了

  1. tk.mybatis.mapper 支持多表筛选么(join),我自己好像没有找到。
  2. tk.mybatis.mapper 支持组么,就是类似于 (id > 1 and id < 5) and price = 10.

关注我 ##

最后大家可以关注我和 Mybatis-Dynamic-query项目 _

Follow @wz2cool Star Fork

初识 tk.mybatis.mapper 通用mapper的更多相关文章

  1. 初识 tk.mybatis.mapper

    在博客园发表Mybatis Dynamic Query后,一位园友问我知不知道通用mapper,仔细去找了一下,还真的有啊,比较好的就是abel533写的tk.mybatis.mapper. 本次例子 ...

  2. Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x

    Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...

  3. 扩展mybatis和通用mapper,支持mysql的geometry类型字段

    因项目中需要用到地理位置信息的存储.查询.计算等,经过研究决定使用mysql(5.7版本)数据库的geometry类型字段来保存地理位置坐标,使用虚拟列(Virtual Generated Colum ...

  4. MyBatis插件 - 通用mapper

    1.简单认识通用mapper 1.1.了解mapper 作用:就是为了帮助我们自动的生成sql语句 [ ps:MyBatis需要编写xxxMapper.xml,而逆向工程是根据entity实体类来进行 ...

  5. 使用mybatis-generator插件结合tk.mybatis自动生成mapper二三事

    本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑.     ...

  6. 使用mybatis-generator插件结合tk.mybatis自动生成mapper

    本篇文章将介绍使用spring boot框架,引入mybatis-generator插件,结合tk.mybatis自动生成Mapper和Entity的一整套流程,其中包括最重要的踩坑与填坑.     ...

  7. (二、下) springBoot 、maven 、mysql、 mybatis、 通用Mapper、lombok 简单搭建例子 《附项目源码》

    接着上篇文章中 继续前进. 一.在maven 的pom.xm中添加组件依赖, mybatis通用Mapper,及分页插件 1.mybatis通用Mapper <!-- mybatis通用Mapp ...

  8. Spring Boot集成Mybatis及通用Mapper

    集成Mybatis可以通过 mybatis-spring-boot-starter 实现. <!-- https://mvnrepository.com/artifact/org.mybatis ...

  9. mybatis的通用mapper小结

    import tk.mybatis.mapper.entity.Example; //此包是tk下的1.定义一个dao层接口不需要任何方法 需要继承Mapper<类型> 2.在servic ...

随机推荐

  1. Python中列表

    names=["Linda","Lily","Lucy","Grace","Paul"] #切片 p ...

  2. 慕学在线网0.4_xadmin后台管理

    admin是基于Django开发的后台管理框架,方便,快捷,而且简单: 而xadmin就相当于admin的升级版,更加强大. 1.安装xadmin(源码安装方式) 教程 PS: - 卸载pip安装的x ...

  3. Spark性能优化(基于Spark 1.x)

    Task优化:    1.慢任务的性能优化:可以考虑减少每个Partition处理的数据量,同时建议开启spark.speculation(慢任务推导,当检测的慢任务时,会同步开启相同的新任务,谁先完 ...

  4. tkinter中combobox下拉选择控件(九)

    combobox控件,下拉菜单控件 combobox控件在tkinter中的ttk下 简单的实现下: import tkinter from tkinter import ttk # 导入ttk模块, ...

  5. 03-13_WLST导航和定位MBean

    本文重点:WLST导航和定位MBean     MBean切换图 如上:红色的字体表示切换的命令.ls和cd是在当前树下切换,其他命令是在不同树之间切换. 其中: DomainMBeanServer有 ...

  6. fedora输入法

    fedora自带输入法,另外如果自己鼓捣的话很可能身心俱疲. 打开设置(在桌面右击也能打开) 区域和语言 在输入源中添加 汉语(中国) 快捷键 输入源切换:win+space 中英文切换:shift

  7. tidb集群某个节点报错之:node_exporter-9100.service failed

    今天启动集群tidb时出现一个错误,是某个tikv节点报错:node_exporter-9100.service  failed 一个节点的问题会导致整个集群启动失败.去此节点下的日志文件中查找,发现 ...

  8. 4.91Python数据类型之(6)元组

    前言 有时候,我们为了数值的安全性,不许用户修改数据,今天我们就来讲讲关于python不可变的数据类型--- 元组数据 目录 1.元组的基本定义 2.元组的基本操作 (一)元组的基本定义 1.元组的概 ...

  9. win10 文件扩展名的更改

    win10 文件扩展名的改 随便打开一个文件夹,最好是"此电脑",  第二行是 "     文件  -   计算机  -  查看   " 在查看里面就可以更改了 ...

  10. web自动化--如何在不同页面间游刃有余

    大家都知道,selenium中对页面元素的操作都是基于当前页面进行操作的,有时会有这种情况,在这个页面操作完一个步骤后,要去另一个页面查看,这就涉及到页面间的操作 # -*- coding:utf-8 ...