Java Code Examples for tk.mybatis.mapper.util.StringUtil

The following examples show how to use tk.mybatis.mapper.util.StringUtil. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jvue-admin   Source File: HySwaggerMapperPlugin.java    License: MIT License 6 votes vote down vote up
private void columnTypeAnnotation(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
	if (this.columnTypeEnabled) {
		String columnType = "tk.mybatis.mapper.annotation.ColumnType";
		String jdbcType = "org.apache.ibatis.type.JdbcType";
		topLevelClass.addImportedType(columnType);
		topLevelClass.addImportedType(jdbcType);
		List<Field> fields = topLevelClass.getFields();
		for (Field field : fields) {
			List<IntrospectedColumn> allColumns = introspectedTable.getAllColumns();
			for (IntrospectedColumn introspectedColumn : allColumns)
				if (field.getName().equals(introspectedColumn.getJavaProperty())) {
					String jdbcTypeName = introspectedColumn.getJdbcTypeName();
					if(StringUtil.isEmpty(jdbcTypeName)){
						continue;
					}else{
						jdbcTypeName="JdbcType."+jdbcTypeName;
					}
					field.addAnnotation("@ColumnType(jdbcType=" + jdbcTypeName + ")");
				}
		}
	}
}
 
Example 2
Source Project: ml-blog   Source File: GuestbookServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public void reply(Guestbook guestbook) throws GlobalException {
    guestbook.setNickname(commonMap.get("author").toString());
    guestbook.setEmail(commonMap.get("email").toString());
    guestbook.setStatus(0);
    guestbook.setDelStatus(0);
    guestbook.setType(2);
    this.guestbookMapper.insert(guestbook);

    // 发送邮件
    Guestbook gb = this.guestbookMapper.selectByPrimaryKey(guestbook.getGuestbookId());
    if (gb != null && !StringUtil.isEmpty(gb.getEmail())) {
        try {
            this.mailService.sendEmail(gb.getEmail(),guestbook.getNickname() + "的留言回复【"+commonMap.get("blogName").toString()+"】",guestbook.getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example 3
Source Project: tk-mybatis   Source File: MapperHelper.java    License: MIT License 6 votes vote down vote up
/**
 * 配置属性
 *
 * @param properties
 */
public void setProperties(Properties properties) {
    config.setProperties(properties);
    //注册通用接口
    String mapper = null;
    if (properties != null) {
        mapper = properties.getProperty("mappers");
    }
    if (StringUtil.isNotEmpty(mapper)) {
        String[] mappers = mapper.split(",");
        for (String mapperClass : mappers) {
            if (mapperClass.length() > 0) {
                registerMapper(mapperClass);
            }
        }
    }
}
 
Example 4
Source Project: tk-mybatis   Source File: SqlHelper.java    License: MIT License 6 votes vote down vote up
/**
 * 获取表名 - 支持动态表名,该方法用于多个入参时,通过parameterName指定入参中实体类的@Param的注解值
 *
 * @param entityClass
 * @param tableName
 * @param parameterName
 * @return
 */
public static String getDynamicTableName(Class<?> entityClass, String tableName, String parameterName) {
    if (IDynamicTableName.class.isAssignableFrom(entityClass)) {
        if (StringUtil.isNotEmpty(parameterName)) {
            return "<if test=\"@[email protected](" + parameterName + ") and " + parameterName + ".dynamicTableName != null and " + parameterName + ".dynamicTableName != ''\">\n" +
                    "${" + parameterName + ".dynamicTableName}\n" +
                    "</if>\n" +
                    "<if test=\"@[email protected](" + parameterName + ") or " + parameterName + ".dynamicTableName == null or " + parameterName + ".dynamicTableName == ''\">\n" +
                    tableName + "\n" +
                    "</if>";
        } else {
            return getDynamicTableName(entityClass, tableName);
        }

    } else {
        return tableName;
    }
}
 
Example 5
Source Project: tk-mybatis   Source File: SqlHelper.java    License: MIT License 6 votes vote down vote up
/**
 * 判断自动!=null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public static String getIfNotNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<if test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" != null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" and ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" != '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</if>");
    return sql.toString();
}
 
Example 6
Source Project: tk-mybatis   Source File: SqlHelper.java    License: MIT License 6 votes vote down vote up
/**
 * 判断自动==null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public static String getIfIsNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<if test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" == null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" or ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" == '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</if>");
    return sql.toString();
}
 
Example 7
Source Project: Mapper   Source File: AggregationProvider.java    License: MIT License 6 votes vote down vote up
public static String aggregationSelectClause(Class<?> entityClass, String wrapKeyword, AggregateCondition condition) {
    Assert.notEmpty(condition.getAggregateProperty(), "aggregateProperty must have length; it must not be null or empty");
    Assert.notNull(condition.getAggregateType(), "aggregateType is required; it must not be null");
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    Map<String, EntityColumn> propertyMap = entityTable.getPropertyMap();
    StringBuilder selectBuilder = new StringBuilder();
    selectBuilder.append(condition.getAggregateType().name());
    String columnName = propertyMap.get(condition.getAggregateProperty()).getColumn();
    selectBuilder.append("(").append(columnName).append(")");
    selectBuilder.append(" AS ");
    if (StringUtil.isNotEmpty(condition.getAggregateAliasName())) {
        selectBuilder.append(condition.getAggregateAliasName());
    } else {
        selectBuilder.append(wrapKeyword(wrapKeyword, columnName));
    }
    if (condition.getGroupByProperties() != null && condition.getGroupByProperties().size() > 0) {
        for (String property : condition.getGroupByProperties()) {
            selectBuilder.append(", ");
            columnName = propertyMap.get(property).getColumn();
            selectBuilder.append(columnName).append(" AS ").append(wrapKeyword(wrapKeyword, columnName));
        }
    }
    return selectBuilder.toString();
}
 
Example 8
Source Project: Mapper   Source File: Example.java    License: MIT License 6 votes vote down vote up
private Example(Builder builder) {
    this.exists = builder.exists;
    this.notNull = builder.notNull;
    this.distinct = builder.distinct;
    this.entityClass = builder.entityClass;
    this.propertyMap = builder.propertyMap;
    this.selectColumns = builder.selectColumns;
    this.excludeColumns = builder.excludeColumns;
    this.oredCriteria = builder.exampleCriterias;
    this.forUpdate = builder.forUpdate;
    this.tableName = builder.tableName;

    if (!StringUtil.isEmpty(builder.orderByClause.toString())) {
        this.orderByClause = builder.orderByClause.toString();
    }
}
 
Example 9
Source Project: Mapper   Source File: DefaultEntityResolve.java    License: MIT License 6 votes vote down vote up
/**
 * 处理排序
 *
 * @param entityTable
 * @param field
 * @param entityColumn
 */
protected void processOrderBy(EntityTable entityTable, EntityField field, EntityColumn entityColumn) {
    String orderBy = "";
    if(field.isAnnotationPresent(OrderBy.class)){
        orderBy = field.getAnnotation(OrderBy.class).value();
        if ("".equals(orderBy)) {
            orderBy = "ASC";
        }
        log.warn(OrderBy.class + " is outdated, use " + Order.class + " instead!");
    }
    if (field.isAnnotationPresent(Order.class)) {
        Order order = field.getAnnotation(Order.class);
        if ("".equals(order.value()) && "".equals(orderBy)) {
            orderBy = "ASC";
        } else {
            orderBy = order.value();
        }
        entityColumn.setOrderPriority(order.priority());
    }
    if (StringUtil.isNotEmpty(orderBy)) {
        entityColumn.setOrderBy(orderBy);
    }
}
 
Example 10
Source Project: Mapper   Source File: SqlHelper.java    License: MIT License 6 votes vote down vote up
/**
 * 获取表名 - 支持动态表名,该方法用于多个入参时,通过parameterName指定入参中实体类的@Param的注解值
 *
 * @param entityClass
 * @param tableName
 * @param parameterName
 * @return
 */
public static String getDynamicTableName(Class<?> entityClass, String tableName, String parameterName) {
    if (IDynamicTableName.class.isAssignableFrom(entityClass)) {
        if (StringUtil.isNotEmpty(parameterName)) {
            StringBuilder sql = new StringBuilder();
            sql.append("<choose>");
            sql.append("<when test=\"@[email protected](" + parameterName + ") and " + parameterName + ".dynamicTableName != null and " + parameterName + ".dynamicTableName != ''\">");
            sql.append("${" + parameterName + ".dynamicTableName}");
            sql.append("</when>");
            //不支持指定列的时候查询全部列
            sql.append("<otherwise>");
            sql.append(tableName);
            sql.append("</otherwise>");
            sql.append("</choose>");
            return sql.toString();
        } else {
            return getDynamicTableName(entityClass, tableName);
        }

    } else {
        return tableName;
    }
}
 
Example 11
Source Project: Mapper   Source File: SqlHelper.java    License: MIT License 6 votes vote down vote up
/**
 * 判断自动!=null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public static String getIfNotNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<if test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" != null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" and ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" != '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</if>");
    return sql.toString();
}
 
Example 12
Source Project: Mapper   Source File: SqlHelper.java    License: MIT License 6 votes vote down vote up
/**
 * 判断自动==null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public static String getIfIsNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<if test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" == null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" or ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" == '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</if>");
    return sql.toString();
}
 
Example 13
Source Project: erp-framework   Source File: DtreeServiceImpl.java    License: MIT License 5 votes vote down vote up
private List<Dtree> selectTreeMenuList(String roleId) {
    List<Dtree> origin = erpMenuMapper.getSysRoleTreeMenus(null);
    List<ErpMenu> menuList = erpMenuMapper.selectAll();

    setBasicData(origin, menuList);

    if (!StringUtil.isEmpty(roleId)) {
        setCheckArrayStatus(roleId, origin);
    }

    if (origin != null && origin.size() > 0) {
        return getTargetList(origin);
    }
    return null;
}
 
Example 14
Source Project: tk-mybatis   Source File: Example.java    License: MIT License 5 votes vote down vote up
public OrderBy orderBy(String property) {
    String column = property(property);
    if (column == null) {
        isProperty = false;
        return this;
    }
    if (StringUtil.isNotEmpty(example.getOrderByClause())) {
        example.setOrderByClause(example.getOrderByClause() + "," + column);
    } else {
        example.setOrderByClause(column);
    }
    isProperty = true;
    return this;
}
 
Example 15
Source Project: tk-mybatis   Source File: EntityColumn.java    License: MIT License 5 votes vote down vote up
/**
 * 返回格式如:#{entityName.age+suffix,jdbcType=NUMERIC,typeHandler=MyTypeHandler}+separator
 *
 * @param entityName
 * @param suffix
 * @param separator
 * @return
 */
public String getColumnHolder(String entityName, String suffix, String separator) {
    StringBuffer sb = new StringBuffer("#{");
    if (StringUtil.isNotEmpty(entityName)) {
        sb.append(entityName);
        sb.append(".");
    }
    sb.append(this.property);
    if (StringUtil.isNotEmpty(suffix)) {
        sb.append(suffix);
    }
    if (this.jdbcType != null) {
        sb.append(",jdbcType=");
        sb.append(this.jdbcType.toString());
    } else if (this.typeHandler != null) {
        sb.append(",typeHandler=");
        sb.append(this.typeHandler.getCanonicalName());
    } else if (!this.javaType.isArray()) {//当类型为数组时,不设置javaType#103
        sb.append(",javaType=");
        sb.append(javaType.getCanonicalName());
    }
    sb.append("}");
    if (StringUtil.isNotEmpty(separator)) {
        sb.append(separator);
    }
    return sb.toString();
}
 
Example 16
Source Project: tk-mybatis   Source File: Config.java    License: MIT License 5 votes vote down vote up
/**
 * 获取主键自增回写SQL
 *
 * @return
 */
public String getIDENTITY() {
    if (StringUtil.isNotEmpty(this.IDENTITY)) {
        return this.IDENTITY;
    }
    //针对mysql的默认值
    return IdentityDialect.MYSQL.getIdentityRetrievalStatement();
}
 
Example 17
Source Project: tk-mybatis   Source File: Config.java    License: MIT License 5 votes vote down vote up
/**
 * 获取序列格式化模板
 *
 * @return
 */
public String getSeqFormat() {
    if (StringUtil.isNotEmpty(this.seqFormat)) {
        return this.seqFormat;
    }
    return "{0}.nextval";
}
 
Example 18
Source Project: tk-mybatis   Source File: Config.java    License: MIT License 5 votes vote down vote up
/**
 * 获取UUID生成规则
 *
 * @return
 */
public String getUUID() {
    if (StringUtil.isNotEmpty(this.UUID)) {
        return this.UUID;
    }
    return "@[email protected]().toString().replace(\"-\", \"\")";
}
 
Example 19
Source Project: tk-mybatis   Source File: Config.java    License: MIT License 5 votes vote down vote up
/**
 * 获取表前缀,带catalog或schema
 *
 * @return
 */
public String getPrefix() {
    if (StringUtil.isNotEmpty(this.catalog)) {
        return this.catalog;
    }
    if (StringUtil.isNotEmpty(this.schema)) {
        return this.schema;
    }
    return "";
}
 
Example 20
Source Project: tk-mybatis   Source File: EntityTable.java    License: MIT License 5 votes vote down vote up
public String getPrefix() {
    if (StringUtil.isNotEmpty(catalog)) {
        return catalog;
    }
    if (StringUtil.isNotEmpty(schema)) {
        return schema;
    }
    return "";
}
 
Example 21
Source Project: tk-mybatis   Source File: MapperTemplate.java    License: MIT License 5 votes vote down vote up
/**
 * 获取实体类的表名
 *
 * @param entityClass
 * @return
 */
protected String tableName(Class<?> entityClass) {
    EntityTable entityTable = EntityHelper.getEntityTable(entityClass);
    String prefix = entityTable.getPrefix();
    if (StringUtil.isEmpty(prefix)) {
        //使用全局配置
        prefix = mapperHelper.getConfig().getPrefix();
    }
    if (StringUtil.isNotEmpty(prefix)) {
        return prefix + "." + entityTable.getName();
    }
    return entityTable.getName();
}
 
Example 22
Source Project: tk-mybatis   Source File: CamelCaseTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testCamelhumpToUnderline() {
    Assert.assertEquals("user_id", StringUtil.camelhumpToUnderline("userId"));
    Assert.assertEquals("sys_user", StringUtil.camelhumpToUnderline("sysUser"));
    Assert.assertEquals("sys_user_role", StringUtil.camelhumpToUnderline("sysUserRole"));
    Assert.assertEquals("s_function", StringUtil.camelhumpToUnderline("sFunction"));
}
 
Example 23
Source Project: Mapper   Source File: CamelCaseTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testCamelhumpToUnderline() {
    Assert.assertEquals("user_id", StringUtil.camelhumpToUnderline("userId"));
    Assert.assertEquals("sys_user", StringUtil.camelhumpToUnderline("sysUser"));
    Assert.assertEquals("sys_user_role", StringUtil.camelhumpToUnderline("sysUserRole"));
    Assert.assertEquals("s_function", StringUtil.camelhumpToUnderline("sFunction"));
}
 
Example 24
Source Project: Mapper   Source File: AggregateCondition.java    License: MIT License 5 votes vote down vote up
/**
 * @param aggregateProperty 不能为空,为保证返回结果key与传入值相同 方法不会去除前后空格
 * @param aggregateType
 * @param groupByProperties 为保证返回结果key与传入值相同 方法不会去除每一项前后空格
 */
public AggregateCondition(String aggregateProperty, AggregateType aggregateType,
                          String[] groupByProperties) {
    this.groupByProperties = new ArrayList<String>();
    // 需要放在propertyMap初始化完成后执行
    aggregateType(aggregateType);
    if (StringUtil.isNotEmpty(aggregateProperty)) {
        aggregateBy(aggregateProperty);
    }
    groupBy(groupByProperties);
}
 
Example 25
Source Project: Mapper   Source File: SelectPropertyProvider.java    License: MIT License 5 votes vote down vote up
/**
 * 判断是否需要拼接 where 条件
 * @param value
 * @param notEmpty
 * @return
 */
public static boolean existsWhereCondition(Object value, boolean notEmpty) {
    boolean appendWhereCondition = true;
    if (Objects.isNull(value)) {
        log.warn("value is null! this will case no conditions after where keyword");
    } else {
        if (String.class.equals(value.getClass()) && notEmpty && StringUtil.isEmpty(value.toString())) {
            // 如果 value 是 String 类型,则根据是否允许为空串做进一步校验来决定是否拼接 where 条件
            appendWhereCondition = false;
        }
    }
    return appendWhereCondition;
}
 
Example 26
Source Project: Mapper   Source File: UpdateByPrimaryKeySelectiveForceProvider.java    License: MIT License 5 votes vote down vote up
/**
 * 判断自动!=null的条件结构
 *
 * @param entityName
 * @param column
 * @param contents
 * @param empty
 * @return
 */
public String getIfNotNull(String entityName, EntityColumn column, String contents, boolean empty) {
    StringBuilder sql = new StringBuilder();
    sql.append("<choose>");
    sql.append("<when test=\"");
    if (StringUtil.isNotEmpty(entityName)) {
        sql.append(entityName).append(".");
    }
    sql.append(column.getProperty()).append(" != null");
    if (empty && column.getJavaType().equals(String.class)) {
        sql.append(" and ");
        if (StringUtil.isNotEmpty(entityName)) {
            sql.append(entityName).append(".");
        }
        sql.append(column.getProperty()).append(" != '' ");
    }
    sql.append("\">");
    sql.append(contents);
    sql.append("</when>");

    //指定的字段会被强制更新
    sql.append("<when test=\"");
    sql.append(FORCE_UPDATE_PROPERTIES).append(" != null and ").append(FORCE_UPDATE_PROPERTIES).append(".contains('");
    sql.append(column.getProperty());
    sql.append("')\">");
    sql.append(contents);
    sql.append("</when>");

    sql.append("<otherwise></otherwise>");
    sql.append("</choose>");
    return sql.toString();
}
 
Example 27
Source Project: Mapper   Source File: Example.java    License: MIT License 5 votes vote down vote up
public OrderBy orderBy(String property) {
    String column = property(property);
    if (column == null) {
        isProperty = false;
        return this;
    }
    if (StringUtil.isNotEmpty(example.getOrderByClause())) {
        example.setOrderByClause(example.getOrderByClause() + "," + column);
    } else {
        example.setOrderByClause(column);
    }
    isProperty = true;
    return this;
}
 
Example 28
Source Project: Mapper   Source File: Example.java    License: MIT License 5 votes vote down vote up
private String propertyforOderBy(String property) {
    if (StringUtil.isEmpty(property) || StringUtil.isEmpty(property.trim())) {
        throw new MapperException("接收的property为空!");
    }
    property = property.trim();
    if (!propertyMap.containsKey(property)) {
        throw new MapperException("当前实体类不包含名为" + property + "的属性!");
    }
    return propertyMap.get(property).getColumn();
}
 
Example 29
Source Project: Mapper   Source File: EntityColumn.java    License: MIT License 5 votes vote down vote up
/**
 * 返回格式如:#{entityName.age+suffix,jdbcType=NUMERIC,typeHandler=MyTypeHandler}+separator
 *
 * @param entityName
 * @param suffix
 * @param separator
 * @return
 */
public String getColumnHolder(String entityName, String suffix, String separator) {
    StringBuffer sb = new StringBuffer("#{");
    if (StringUtil.isNotEmpty(entityName)) {
        sb.append(entityName);
        sb.append(".");
    }
    sb.append(this.property);
    if (StringUtil.isNotEmpty(suffix)) {
        sb.append(suffix);
    }
    //如果 null 被当作值来传递,对于所有可能为空的列,JDBC Type 是需要的
    if (this.jdbcType != null) {
        sb.append(", jdbcType=");
        sb.append(this.jdbcType.toString());
    }
    //为了以后定制类型处理方式,你也可以指定一个特殊的类型处理器类,例如枚举
    if (this.typeHandler != null) {
        sb.append(", typeHandler=");
        sb.append(this.typeHandler.getCanonicalName());
    }
    //3.4.0 以前的 mybatis 无法获取父类中泛型的 javaType,所以如果使用低版本,就需要设置 useJavaType = true
    //useJavaType 默认 false,没有 javaType 限制时,对 ByPrimaryKey 方法的参数校验就放宽了,会自动转型
    if (useJavaType && !this.javaType.isArray()) {//当类型为数组时,不设置javaType#103
        sb.append(", javaType=");
        sb.append(javaType.getCanonicalName());
    }
    sb.append("}");
    if (StringUtil.isNotEmpty(separator)) {
        sb.append(separator);
    }
    return sb.toString();
}
 
Example 30
Source Project: Mapper   Source File: Config.java    License: MIT License 5 votes vote down vote up
/**
 * 获取主键自增回写SQL
 *
 * @return
 */
public String getIDENTITY() {
    if (StringUtil.isNotEmpty(this.IDENTITY)) {
        return this.IDENTITY;
    }
    //针对mysql的默认值
    return IdentityDialect.MYSQL.getIdentityRetrievalStatement();
}