Chain operation
In MyBatis-Flex, built-in 、
as well as
Used to perform chain query operations and chain operations (modify and delete) on data.
- QueryChain: Chain query
- UpdateChain: Chain update
- DbChain: Chained callDb + Row
QueryChain Example
For example, the query article list code is as follows:
@SpringBootTest class ArticleServiceTest { @Autowired ArticleService articleService; @Test void testChain() { List<Article> articles = () .select(ARTICLE.ALL_COLUMNS) .from(ARTICLE) .where((100)) .list(); } }
If it is not in the Service, we can also pass(mapper)
Method, create one by yourselfQueryChain
Example, the code is as follows:
List<Article> articles = (mapper) .select(ARTICLE.ALL_COLUMNS) .from(ARTICLE) .where((100)) .list();
UpdateChain Example
Suppose we want to updateAccount
ofuserName
for "Zhang San
", add 1 on the previous basis, and the update code is as follows:
@Test public void testUpdateChain1() { () .set(Account::getUserName, "Zhang San") .setRaw(Account::getAge, "age + 1") .where(Account::getId).eq(1) .update(); }
When the above method is called, the SQL executed internally by MyBatis-Flex is as follows:
UPDATE `tb_account` SET `user_name` = 'Zhang San' , `age` = age + 1 WHERE `id` = 1
Another example:
@Test public void testUpdateChain2() { //Update data () .set(Account::getAge, (1)) .where(Account::getId).ge(100) .and(Account::getAge).eq(18) .update(); //Query all data and print (accountMapper) .where(Account::getId).ge(100) .and(Account::getAge).eq(18) .list() .forEach(::println); }
passUpdateChain
conductupdate()
, the SQL it executes is as follows:
UPDATE `tb_account` SET `age` = `age` + 1 WHERE `id` >= 100 AND `age` = 18
QueryChain method
- one(): Get a data
- list(): Get multiple pieces of data
- page(): Pagination query
- obj(): This method can be used when SQL query only returns 1 column of data and only 1 piece of data
- objList(): This method can be used when SQL query only returns 1 column of data.
- count(): query the number of data strips
- exists(): Whether it exists, determine whether count is greater than 0
QueryChain extension method
one() series methods
- one(): Get a data
- oneAs(asType): Query the data and convert it directly into vo, dto, etc.
- oneWithRelations: Query a data and its associated data
- oneWithRelationsAs: Query a data and its associated data, and directly convert it into vo, dto, etc.
- oneOpt: Returns the Optional type and gets a data
- oneAsOpt(asType): Returns the Optional type, querys the data, and directly converts it to vo, dto, etc.
- oneWithRelationsOpt: Returns the Optional type, querying a data and its associated data
- oneWithRelationsAsOpt: Returns the Optional type, querys a data and its associated data, and directly converts it to vo, dto, etc.
list() series methods
- list(): query the data list
- listWithRelations(): Query the data list and its associated data
- listAs(): Query the data list and convert it directly into vo, dto, etc.
- listWithRelationsAs(): query the data list, and its associated data, and directly convert it into vo, dto, etc.
page() series methods
- page(page): paging query data list
- pageAs(page): paging query data list and directly convert it into vo, dto, etc.
obj() series methods
- obj(): Query the first column and the first piece of data
- objAs(asType): Query the first column, and convert the first piece of data to a specified type, such as Long, String, etc.
- objOpt(): Returns the Optional type, querys the first column, and the first piece of data
- objAsOpt(asType): Returns the Optional type, querys the first column, and converts the first piece of data to the specified type, such as Long, String, etc.
objList() series methods
- objList(): Query the first column
- objListAs(asType): Query the first column and convert it to the specified type, such as Long, String, etc.
Code practical examples
Example 1: Query the Entity List
List<Article> articles = () .select(ARTICLE.ALL_COLUMNS) .from(ARTICLE) .where((100)) .list();
Example 2: Query 1 Entity data
Article article = () .select(ARTICLE.ALL_COLUMNS) .from(ARTICLE) .where((100)) .limit(1) .one();
Example 3: Query VO data (ArticleVo)
public class ArticleVo { private Long id; private Long accountId; private String title; private String content; //The content with the most comments private Long maxComments; //getter setter }
Query code:
ArticleVo articleVo = () .select( ARTICLE.ALL_COLUMNS, max().as(ArticleVo::maxComments) ).from(ARTICLE) .where((100)) .limit(1) .oneAs();
Example 4: Many-to-many association query VO data (ArticleVo)
AndArticle CategoryDefinition:
public class ArticleVo { private Long id; private Long accountId; private String title; private String content; //Article and classification many-to-many relationship configuration @RelationManyToMany( joinTable = "tb_article_category_mapping", // Intermediate table selfField = "id", joinSelfColumn = "article_id", targetField = "id", joinTargetColumn = "category_id" ) private List<ArticleCategory> categories; //getter setter }
Query code:
ArticleVo articleVo = () .select() .from(ARTICLE) .where((100)) .limit(1) .oneWithRelationsAs();
Query ArticleVo and its associated data (many-to-many article classification) through the oneWithRelationsAs method. For more information about association queries, please refer to the chapter:"Related Query"。
DbChain Example
useDbChain
No need to addQueryWrapper
andRow
The construction separation can be performed directly.
// Added Row build("tb_account") .setId() .set("user_name", "zhang san") .set("age", 18) .set("birthday", new Date()) .save(); // Query QueryWrapper build("tb_account") .select("id", "user_name", "age", "birthday") .where("age > ?", 18) .list() .forEach(::println);
This is the article about the sample code of mybatis-flex implementing chain operations. For more related contents of mybatis-flex chain operations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!