初识 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 ...
随机推荐
- python:异常处理、自定义异常、断言
什么是异常: 当程序遭遇某些非正常问题的时候就会抛出异常:比如int()只能处理能转化成int的对象,如果传入一个不能转化的对象就会报错并抛出异常 常用的异常有: ValueError :传入无效的错 ...
- dubbo-admin管理控制台的安装部署(最简单)
Dubbo-admin最简单的安装部署,十分钟就能搞定! 网上找的安装教程虽说详细,但是就是因为详细操作起来而显得繁琐.今天,我帮大家跳过这些繁琐的步骤,简单快捷的安装部署dubbo-admin. 1 ...
- [20171225]没有备份数据文件的恢复.txt
[20171225]没有备份数据文件的恢复.txt --//别人问的问题,增加了数据文件没有备份,如何恢复,实际上很简单,因为当前控制文件有记录建立时间只要从建立数据文件开始的--//归档日志都存在恢 ...
- python中remove的一些坑
前几天,使用python时遇到这么一个需求,删除一个列表中值为1的元素.我寻思着使用remove方法,但是remove方法只会删除第一个,于是我使用for循环去删除.代码和运行结果如下: 当时这个结果 ...
- 字符串通过在配置文件配置三个key来进行加密解密
在这里和大家分享一个加密util,相对于md5加密相信大家都已经很熟悉了吧,md5是不可逆的一种加密方式,虽说不可逆但是网上已经有了破解的方法,我这边分享一个免费的破解 网址给大家:https://w ...
- 3.7Python数据处理篇之Numpy系列(七)---Numpy的统计函数
目录 目录 前言 (一)函数一览表 (二)统计函数1 (三)统计函数2 目录 前言 具体我们来学Numpy的统计函数 (一)函数一览表 调用方式:np.* .sum(a) 对数组a求和 .mean(a ...
- Appium 实战练习一
# -*- coding:utf-8 -*- ''' Created on Sep 30, 2018 @author: SaShuangYiBing Comment: ''' import time ...
- 【Ansible 文档】【译文】Playbooks 变量
Variables 变量 自动化的存在使得重复的做事情变得很容易,但是我们的系统不可能完全一样. 在某些系统中,你可能想要设置一些与其他系统不一样的行为和配置. 同样地,远程系统的行为和状态也可以影响 ...
- php5.4之分布式缓存memcache(windows7下安装配置)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq1355541448/article/details/36663203 使用理由:就是为了频繁查询 ...
- 分布式消息列队RocketMQ部署
模式: 多Master多Slave模式,异步复制: 每个 Master 配置一个 Slave,有多对Master-Slave,HA 采用异步复制方式,主备有短暂消息延迟,毫秒级. 优点:即使磁盘损坏, ...