瀏覽代碼

新增默认字符串实体条件 like 查询,修改 selectOne 支持 limit 限制

hubin 7 年之前
父節點
當前提交
845d64b7bc

+ 1 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/enums/SqlMethod.java

@@ -64,6 +64,7 @@ public enum SqlMethod {
     SELECT_BY_ID("selectById", "根据ID 查询一条数据", "SELECT %s FROM %s WHERE %s=#{%s}"),
     SELECT_BY_MAP("selectByMap", "根据columnMap 查询一条数据", "<script>SELECT %s FROM %s %s</script>"),
     SELECT_BATCH_BY_IDS("selectBatchIds", "根据ID集合,批量查询数据", "<script>SELECT %s FROM %s WHERE %s IN (%s)</script>"),
+    SELECT_ONE("selectOne", "查询满足条件一条数据", "<script>SELECT %s FROM %s %s</script>"),
     SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>SELECT COUNT(1) FROM %s %s</script>"),
     SELECT_LIST("selectList", "查询满足条件所有数据", "<script>SELECT %s FROM %s %s</script>"),
     SELECT_PAGE("selectPage", "查询满足条件所有数据(并翻页)", "<script>SELECT %s FROM %s %s</script>"),

+ 43 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectOne.java

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <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>
+ * http://www.apache.org/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 com.baomidou.mybatisplus.core.injector.methods;
+
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.SqlSource;
+
+import com.baomidou.mybatisplus.core.enums.SqlMethod;
+import com.baomidou.mybatisplus.core.injector.AbstractMethod;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+
+/**
+ * <p>
+ * 根据 ID 删除
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-04-06
+ */
+public class SelectOne extends AbstractMethod {
+
+    @Override
+    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
+        SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
+        String sql = String.format(sqlMethod.getSql(), this.sqlSelectColumns(tableInfo, false),
+            tableInfo.getTableName(), this.sqlWhereEntityWrapper(tableInfo) + getGlobalConfig().getDbConfig().getDbType().getLimit(1));
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+        return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);
+    }
+}

+ 9 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -176,6 +176,15 @@ public interface BaseMapper<T> {
      */
     List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
 
+    /**
+     * <p>
+     * 根据 entity 条件,查询一条记录
+     * </p>
+     *
+     * @param queryWrapper 实体对象
+     */
+    T selectOne(@Param("ew") Wrapper<T> queryWrapper);
+
     /**
      * <p>
      * 根据 Wrapper 条件,查询总记录数

+ 43 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicSelectOne.java

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <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>
+ * http://www.apache.org/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 com.baomidou.mybatisplus.extension.injector.methods;
+
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.SqlSource;
+
+import com.baomidou.mybatisplus.core.enums.SqlMethod;
+import com.baomidou.mybatisplus.core.metadata.TableInfo;
+import com.baomidou.mybatisplus.extension.injector.LogicAbstractMethod;
+
+/**
+ * <p>
+ * 根据 ID 删除
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-13
+ */
+public class LogicSelectOne extends LogicAbstractMethod {
+
+    @Override
+    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
+        SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
+        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, false),
+            tableInfo.getTableName(), this.sqlWhereEntityWrapper(tableInfo) + getGlobalConfig().getDbConfig().getDbType().getLimit(1));
+        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
+        return addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);
+    }
+}