初識 tk.mybatis.mapper 通用mapper

在博客園發表Mybatis Dynamic Query後,一位園友問我知不知道通用mapper,仔細去找了一下,還真的有啊,比較好的就是abel533寫的tk.mybatis.mapper
本次例子地址:https://github.com/wz2cool/tk-mybatis-demojava

傳統Mybatis用法

Spring boot

引用基本的jar到pomgit

<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的用法。github

@Mapper
public interface ProductDao {
    List<Product> getProducts();
}

mapper

傳統mybatis 咱們必須有個xml 文件和Dao 對應起來, tk.maybatis.mapper無需此文件。spring

<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能夠省略這裏的配置。sql

mybatis.type-aliases-package=com.github.wz2cool.demo.tk.mybatis.mapper
mybatis.mapper-locations=classpath:com.github.wz2cool.demo.tk.mybatis.mapper/*.xml

測試

咱們能夠簡單調用一下bash

@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);
    }
}

咱們能夠看到輸出結果,基本上就通了mybatis

==>  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,報錯死活找不到爲何--!!!)app

<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 文件。spring-boot

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

相關文章
相關標籤/搜索