MyBatis-Plus⼯具使⽤之EntityWrapper解析
⽬录
EntityWrapper使⽤解析
EntityWrapper源码解读
EntityWrapper使⽤解析
1、项⽬中引⼊jar包,我这⾥使⽤Maven构建
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>仓库最⾼版本号</version>
</dependency>
mybatis和springmvc<!--快照版本使⽤,正式版本⽆需添加此仓库-->
<repository>
<id>snapshots</id>
<url>/content/repositories/snapshots/</url>
</repository>
特别说明: Mybatis及Mybatis-Spring依赖请勿加⼊项⽬配置,以免引起版本冲突Mybatis-Plus会⾃动帮你维护!
2、springboot项⽬中l⽂件中加上
mybatisplus:
enabled: true
generic:
enabled: true
dialectType: mysql
传统SSM项⽬,修改配置⽂件,将mybatis的sqlSessionFactory替换成mybatis-plus的即可,mybatis-plus只做了⼀些功能的扩展:
<bean id="sqlSessionFactory" class="batisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- ⾃动扫描l⽂件 -->
<property name="mapperLocations" value="classpath:mybatis/*/*.xml"/>
<property name="configLocation" value="classpath:l"/>
<property name="typeAliasesPackage" value="com.del.*"/>
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor" class="batisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
</bean>
</array>
</property>
<!-- 全局配置注⼊ -->
<property name="globalConfig" ref="globalConfig" />
</bean>
3、创建Mapper、xml,创建Mapper时继承BaseMapper,xml正常(省略xml信息)
public interface UserMapper extends BaseMapper<User> {
}
4、实现类继承ServiceImpl
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
public void queryUserList(UserDto dto){
EntityWrapper<User> ew = new EntityWrapper<User>();
ew.where("deleted={0}", 1);
ew.in("user_type", "1");
ew.eq("role", "1");
ew.eq("status", "1");
log.info("selectList condition:{}", ew.getSqlSegment());
List<User> userList = this.selectList(ew);
}
}
更多资料,请查看:
EntityWrapper源码解读
mybatis plus内置了好多CRUD,其中 EntityWrapper这个类就是。
这个类是mybatis plus帮我们写好的好多接⼝,就如同我们在dao层写好⽅法在xml中实现⼀样。那么这个友好的类给我们实现了哪些⽅法呐,今天我们来通过看看源码,来具体说说
/**
*Copyright(c)2011-2014,hubin(************).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* /licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package batisplus.mapper;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import urrent.atomic.AtomicInteger;
import ums.SqlLike;
import ptions.MybatisPlusException;
import lkit.ArrayUtils;
import lkit.CollectionUtils;
import lkit.MapUtils;
import lkit.SqlUtils;
import lkit.StringUtils;
/**
* <p>
* 条件构造抽象类,定义T-SQL语法
* </p>
*
* @author hubin , yanghu , Dyang , Caratacus
* @Date 2016-11-7
*/
@SuppressWarnings("serial")
public abstract class Wrapper<T> implements Serializable {
/**
* 占位符
*/
private static final String PLACE_HOLDER = "{%s}";
private static final String MYBATIS_PLUS_TOKEN = "#{%s.paramNameValuePairs.%s}";
private static final String MP_GENERAL_PARAMNAME = "MPGENVAL";
private static final String DEFAULT_PARAM_ALIAS = "ew";
protected String paramAlias = null;
/**
* SQL 查询字段内容,例如:id,name,age
*/
protected String sqlSelect = null;
/**
* 实现了TSQL语法的SQL实体
protected SqlPlus sql = new SqlPlus();
/**
* ⾃定义是否输出sql为 WHERE OR AND OR OR
*/
protected Boolean isWhere;
/**
* 拼接WHERE后应该是AND还是OR
*/
protected String AND_OR = "AND";
private Map<String, Object> paramNameValuePairs = new HashMap<>(4);
private AtomicInteger paramNameSeq = new AtomicInteger(0);
/**
* 兼容EntityWrapper
*
* @return
*/
public T getEntity() {
return null;
}
public String getSqlSelect() {
if (StringUtils.isEmpty(sqlSelect)) {
return null;
}
return stripSqlInjection(sqlSelect);
}
public Wrapper<T> setSqlSelect(String sqlSelect) {
if (StringUtils.isNotEmpty(sqlSelect)) {
this.sqlSelect = sqlSelect;
}
return this;
}
/**
* SQL ⽚段 (⼦类实现)
*/
public abstract String getSqlSegment();
public String toString() {
String sqlSegment = getSqlSegment();
if (StringUtils.isNotEmpty(sqlSegment)) {
sqlSegment = placeAll("#\\{" + getParamAlias() + ".paramNameValuePairs.MPGENVAL[0-9]+}", "\\?"); }
return sqlSegment;
}
/**
* <p>
* SQL中WHERE关键字跟的条件语句
* </p>
* <p>
* eg: ew.where("name='zhangsan'").where("id={0}","123");
* <p>
* 输出: WHERE (NAME='zhangsan' AND id=123)
* </p>
*
* @param sqlWhere where语句
* @param params 参数集
* @return this
*/
public Wrapper<T> where(String sqlWhere, params) {
sql.WHERE(formatSql(sqlWhere, params));
return this;
}
/**
* <p>
* 等同于SQL的"field=value"表达式
* </p>
*
* @param column
* @param params
* @return
public Wrapper<T> eq(String column, Object params) {
sql.WHERE(formatSql(String.format("%s = {0}", column), params));
return this;
}
/**
* <p>
* 等同于SQL的"field <> value"表达式
* </p>
*
* @param column
* @param params
* @return
*/
public Wrapper<T> ne(String column, Object params) {
sql.WHERE(formatSql(String.format("%s <> {0}", column), params));
return this;
}
/**
* <p>
* 等同于SQL的"field=value"表达式
* </p>
*
* @param params
* @return
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public Wrapper<T> allEq(Map<String, Object> params) {
if (MapUtils.isNotEmpty(params)) {
Iterator iterator = Set().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry<String, Object>) ();
Object value = Value();
if (StringUtils.checkValNotNull(value)) {
sql.WHERE(formatSql(String.format("%s = {0}", Key()), Value())); }
}
}
return this;
}
/**
* <p>
* 等同于SQL的"field>value"表达式
* </p>
*
* @param column
* @param params
* @return
*/
public Wrapper<T> gt(String column, Object params) {
sql.WHERE(formatSql(String.format("%s > {0}", column), params));
return this;
}
/**
* <p>
* 等同于SQL的"field>=value"表达式
* </p>
*
* @param column
* @param params
* @return
*/
public Wrapper<T> ge(String column, Object params) {
sql.WHERE(formatSql(String.format("%s >= {0}", column), params));
return this;
}
/**
* <p>
* 等同于SQL的"field<value"表达式
* </p>
*
* @param column
* @param params
* @return
*/
public Wrapper<T> lt(String column, Object params) {
sql.WHERE(formatSql(String.format("%s < {0}", column), params));
return this;
}
/**
* <p>
* 等同于SQL的"field<=value"表达式
* </p>
*
* @param column
* @param params
* @return
*/
public Wrapper<T> le(String column, Object params) {
sql.WHERE(formatSql(String.format("%s <= {0}", column), params));
return this;
}
/**
* <p>
* AND 连接后续条件
* </p>
*
* @param sqlAnd and条件语句
* @param params 参数集
* @return this
*/
public Wrapper<T> and(String sqlAnd, params) {
sql.AND().WHERE(formatSql(sqlAnd, params));
return this;
}
/**
* <p>
* 使⽤AND连接并换⾏
* </p>
* <p>
* eg: ew.where("name='zhangsan'").and("id=11").andNew("statu=1"); 输出: WHERE * (name='zhangsan' AND id=11) AND (statu=1)
* </p>
*
* @param sqlAnd AND 条件语句
* @param params 参数值
* @return this
*/
public Wrapper<T> andNew(String sqlAnd, params) {
sql.AND_NEW().WHERE(formatSql(sqlAnd, params));
return this;
}
/**
* <p>
* 使⽤AND连接并换⾏
* </p>
* <p>
*
* @return this
*/
public Wrapper<T> and() {
sql.AND_NEW();
return this;
}
/**
* <p>
* 使⽤OR连接并换⾏
* </p>
*
* @return this
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论