初识 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 ...
随机推荐
- scala spark 机器学习初探
Transformer: 是一个抽象类包含特征转换器, 和最终的学习模型, 需要实现transformer方法 通常transformer为一个RDD增加若干列, 最终转化成另一个RDD, 1. 特征 ...
- 盐城 - 开设IT公司的好地方
盐城:位于江苏省北部,这一好像只能算三线的城市,不久前当选为“国家中心城市”.在全国仅有的50城市中名列34.它可成为发展IT产业的好地方. (1)人才济济.从这里走出去的高级人才数不胜数,留在这里的 ...
- Spark程序数据结构优化
场景: 1.scala中的对象:对象头是16个字节(包含指向对象的指针等源数据信息),如果对象中只有一个int的属性,则会占用20个字节,也就是说对象的源数据占用了大部分的空间,所以在封装数据的时候尽 ...
- python第四十九天--paramiko模块安装大作战
准备开始学习:paramiko模块,发现这个模块十分难搞 安装不上 搞了半天,win10 64下 pytyon 3.6 的 paramiko模块 死活安不上,在网上不断的找资料,可是没有用,没有用啊 ...
- python第四十二天 socket ---ssh
用scoket 写一个简版的ssh 服务端: #!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan import socket,os s= ...
- python第五天 字典
今天,已经系统的学习了一下文件相关操作!对三级菜单代码进行的优化: 菜单文件:以字典格式 menu.txt {'第一层':{'第二层':{'第三层':['内容1','内容2','内容3']}},'第一 ...
- Spring boot 直接访问templates中html文件
application.properties 在浏览器中输入http://localhost:8080/index.html 会报一个 因为Spring boot 无法直接访问templates下的文 ...
- 17秋 软件工程 第六次作业 Beta冲刺 Scrum4
17秋 软件工程 第六次作业 Beta冲刺 Scrum4 各个成员冲刺期间完成的任务 世强:完成APP用户签到模块.群发短信模块前端界面: 陈翔:恢复Github项目,完成Scrum博客: 树民:和超 ...
- python之面向对象进阶
接口类 抽象类 钻石继承 多态 鸭子类型 接口类 接口类 继承有两种用途: 一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类Interface, ...
- 【洛谷】【计数原理+Floyed】P1037 产生数
[题目描述:] 给出一个整数 n \((n<10^{30})\) 和 k 个变换规则\((k≤15)\) . 规则: 一位数可变换成另一个一位数: 规则的右部不能为零. 例如: n=234 .有 ...