tk.mybatis.mapper.provider.ExampleProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapper Show documentation
Show all versions of mapper Show documentation
Mybatis 通用 Mapper Jar 集成
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package tk.mybatis.mapper.provider;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import tk.mybatis.mapper.util.MetaObjectUtil;
/**
* ExampleProvider实现类,基础方法实现类
*
* @author liuzh
*/
public class ExampleProvider extends MapperTemplate {
public ExampleProvider(Class> mapperClass, MapperHelper mapperHelper) {
super(mapperClass, mapperHelper);
}
/**
* 根据Example查询总数
*
* @param ms
* @return
*/
public String selectCountByExample(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder("SELECT ");
if (isCheckExampleEntityClass()) {
sql.append(SqlHelper.exampleCheck(entityClass));
}
sql.append(SqlHelper.exampleCountColumn(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.exampleWhereClause());
sql.append(SqlHelper.exampleForUpdate());
return sql.toString();
}
/**
* 根据Example删除
*
* @param ms
* @return
*/
public String deleteByExample(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
if (isCheckExampleEntityClass()) {
sql.append(SqlHelper.exampleCheck(entityClass));
}
//如果设置了安全删除,就不允许执行不带查询条件的 delete 方法
if (getConfig().isSafeDelete()) {
sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("_parameter"));
}
if (SqlHelper.hasLogicDeleteColumn(entityClass)) {
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass)));
sql.append("");
sql.append(SqlHelper.logicDeleteColumnEqualsValue(entityClass, true));
sql.append(" ");
MetaObjectUtil.forObject(ms).setValue("sqlCommandType", SqlCommandType.UPDATE);
} else {
sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
}
sql.append(SqlHelper.exampleWhereClause());
return sql.toString();
}
/**
* 根据Example查询
*
* @param ms
* @return
*/
public String selectByExample(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
//将返回值修改为实体类型
setResultType(ms, entityClass);
StringBuilder sql = new StringBuilder("SELECT ");
if (isCheckExampleEntityClass()) {
sql.append(SqlHelper.exampleCheck(entityClass));
}
sql.append("distinct ");
//支持查询指定列
sql.append(SqlHelper.exampleSelectColumns(entityClass));
sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
sql.append(SqlHelper.exampleWhereClause());
sql.append(SqlHelper.exampleOrderBy(entityClass));
sql.append(SqlHelper.exampleForUpdate());
return sql.toString();
}
/**
* 根据Example查询
*
* @param ms
* @return
*/
public String selectByExampleAndRowBounds(MappedStatement ms) {
return selectByExample(ms);
}
/**
* 根据Example更新非null字段
*
* @param ms
* @return
*/
public String updateByExampleSelective(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
if (isCheckExampleEntityClass()) {
sql.append(SqlHelper.exampleCheck(entityClass));
}
//安全更新,Example 必须包含条件
if (getConfig().isSafeUpdate()) {
sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("example"));
}
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
sql.append(SqlHelper.updateSetColumns(entityClass, "record", true, isNotEmpty()));
sql.append(SqlHelper.updateByExampleWhereClause());
return sql.toString();
}
/**
* 根据Example更新
*
* @param ms
* @return
*/
public String updateByExample(MappedStatement ms) {
Class> entityClass = getEntityClass(ms);
StringBuilder sql = new StringBuilder();
if (isCheckExampleEntityClass()) {
sql.append(SqlHelper.exampleCheck(entityClass));
}
//安全更新,Example 必须包含条件
if (getConfig().isSafeUpdate()) {
sql.append(SqlHelper.exampleHasAtLeastOneCriteriaCheck("example"));
}
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass), "example"));
sql.append(SqlHelper.updateSetColumns(entityClass, "record", false, false));
sql.append(SqlHelper.updateByExampleWhereClause());
return sql.toString();
}
/**
* 根据Example查询一个结果
*
* @param ms
* @return
*/
public String selectOneByExample(MappedStatement ms) {
return selectByExample(ms);
}
}