在博客園發表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