在博客園發表Mybatis Dynamic Query後,一位園友問我知不知道通用mapper,仔細去找了一下,還真的有啊,比較好的就是abel533寫的tk.mybatis.mapper。
本次例子地址:https://github.com/wz2cool/tk-mybatis-demojava
引用基本的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>
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);
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... }
這裏的ProductDao 是傳統的mybatis的用法。github
@Mapper public interface ProductDao { List<Product> getProducts(); }
傳統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); } }
注意這裏的配置只是針對咱們傳統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
須要多添加兩個引用 (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>
嗯 是的沒有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
在理解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 的工具,這個我之後再試試看。
看了 tk.mybatis.mapper 之後,以爲通用mapper 也應該加入到 Mybatis Dynamic Query 中去,固然實現可能有點不同。
這裏真的想問問園友了
最後你們能夠關注我和 Mybatis-Dynamic-query項目 ^_^
Follow @wz2cool Star Fork