How to customize SQL for MybatisPlus EntityWrapper
This article mainly introduces MybatisPlus EntityWrapper how to customize SQL, the article is very detailed, has a certain reference value, interested friends must read it!
EntityWrapper Custom SQL
In mybatis plus, it is convenient to build SQL queries based on conditional constructors, which can be used to write SQL instead of us. Let's look at a few examples.
First kind
Use T selectOne (Wrapper wrapper); we just need to pass in that we create an EntityWrapper () and concatenate the conditions.
For example, new EntityWrapper (). Eq ("id", "1") is to query this piece of data where id equals 1. It supports not only eq (), but also like,not like, group by, etc., almost everything you need in SQL.
The second kind
When we need to customize the columns for a single table query, we use the setSqlSelect method, as shown below:
/ * * this format means that the where condition is created after the requireParam parameter is passed. Please write the code according to this format * Object for your single table entity class * requireParam for the parameter entity class * EntityWrapper ew = new EntityWrapper (requireParam); to construct the where condition * I set the value to 1 for the state attribute of requireParam earlier, so the where condition is automatically concatenated here, and do not pass requireParam****/ EntityWrapper ew = new EntityWrapper (requireParam) if you don't need it. / * * set the column name to be queried * * / ew.setSqlSelect ("id,resource_id,user_id,username,content,createtime,parent_id," + "path,type,like_count") / * * sort method * * / .orderBy (false, "path", false)
As in the first case, various conditions can continue to be spliced later.
The third kind
EntityWrapper () splices the where condition, which is actually mentioned in the second case, which is based on whether we have input parameters when we created the EntityWrapper.
This is all we have used so far, and I will continue to update it when we meet.
MybatisPlus custom sql query public void reconciliationForConfirmOne (Integer Id, Integer payoffType) {List frList = financeReconciliationService.selectList (new EntityWrapper () .eq ("payoff_id", Id) .eq ("payoff_type", payoffType) .where ("(reconciliation_status 1 OR reconciliation_status IS NULL)")) For (FinanceReconciliationEntity fr: frList) {fr.setReconciliationStatus (1);} if (frList! = null & & frList.size () > 0) {financeReconciliationService.updateBatchById (frList);}} these are all the contents of the article "how to customize SQL by MybatisPlus EntityWrapper". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!