Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014-2016 abel533@gmail.com * * 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 com.sinotopia.mybatis.mapper.entity; import com.sinotopia.mybatis.mapper.MapperException; import com.sinotopia.mybatis.mapper.util.StringUtil; import org.apache.ibatis.mapping.ResultFlag; import org.apache.ibatis.mapping.ResultMap; import org.apache.ibatis.mapping.ResultMapping; import org.apache.ibatis.session.Configuration; import com.sinotopia.mybatis.mapper.MapperException; import com.sinotopia.mybatis.mapper.util.StringUtil; import javax.persistence.Table; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * ? * * @author liuzh */ public class EntityTable { public static final Pattern DELIMITER = Pattern.compile("^[`\\[\"]?(.*?)[`\\]\"]?$"); private String name; private String catalog; private String schema; private String orderByClause; private String baseSelect; // => private Set<EntityColumn> entityClassColumns; // => ? private Set<EntityColumn> entityClassPKColumns; //useGenerator?? private List<String> keyProperties; private List<String> keyColumns; //resultMap private ResultMap resultMap; // protected Map<String, EntityColumn> propertyMap; // private Class<?> entityClass; public EntityTable(Class<?> entityClass) { this.entityClass = entityClass; } public Class<?> getEntityClass() { return entityClass; } public void setTable(Table table) { this.name = table.name(); this.catalog = table.catalog(); this.schema = table.schema(); } public void setKeyColumns(List<String> keyColumns) { this.keyColumns = keyColumns; } public void setKeyProperties(List<String> keyProperties) { this.keyProperties = keyProperties; } public String getOrderByClause() { return orderByClause; } public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCatalog() { return catalog; } public void setCatalog(String catalog) { this.catalog = catalog; } public String getSchema() { return schema; } public void setSchema(String schema) { this.schema = schema; } public String getBaseSelect() { return baseSelect; } public void setBaseSelect(String baseSelect) { this.baseSelect = baseSelect; } public String getPrefix() { if (StringUtil.isNotEmpty(catalog)) { return catalog; } if (StringUtil.isNotEmpty(schema)) { return schema; } return ""; } public Set<EntityColumn> getEntityClassColumns() { return entityClassColumns; } public void setEntityClassColumns(Set<EntityColumn> entityClassColumns) { this.entityClassColumns = entityClassColumns; } public Set<EntityColumn> getEntityClassPKColumns() { return entityClassPKColumns; } public void setEntityClassPKColumns(Set<EntityColumn> entityClassPKColumns) { this.entityClassPKColumns = entityClassPKColumns; } public String[] getKeyProperties() { if (keyProperties != null && keyProperties.size() > 0) { return keyProperties.toArray(new String[] {}); } return new String[] {}; } public void setKeyProperties(String keyProperty) { if (this.keyProperties == null) { this.keyProperties = new ArrayList<String>(); this.keyProperties.add(keyProperty); } else { this.keyProperties.add(keyProperty); } } public String[] getKeyColumns() { if (keyColumns != null && keyColumns.size() > 0) { return keyColumns.toArray(new String[] {}); } return new String[] {}; } public void setKeyColumns(String keyColumn) { if (this.keyColumns == null) { this.keyColumns = new ArrayList<String>(); this.keyColumns.add(keyColumn); } else { this.keyColumns.add(keyColumn); } } /** * ??resultMap * * @param configuration * @return */ public ResultMap getResultMap(Configuration configuration) { if (this.resultMap != null) { return this.resultMap; } if (entityClassColumns == null || entityClassColumns.size() == 0) { return null; } List<ResultMapping> resultMappings = new ArrayList<ResultMapping>(); for (EntityColumn entityColumn : entityClassColumns) { String column = entityColumn.getColumn(); //? Matcher matcher = DELIMITER.matcher(column); if (matcher.find()) { column = matcher.group(1); } ResultMapping.Builder builder = new ResultMapping.Builder(configuration, entityColumn.getProperty(), column, entityColumn.getJavaType()); if (entityColumn.getJdbcType() != null) { builder.jdbcType(entityColumn.getJdbcType()); } if (entityColumn.getTypeHandler() != null) { try { builder.typeHandler(entityColumn.getTypeHandler().newInstance()); } catch (Exception e) { throw new MapperException(e); } } List<ResultFlag> flags = new ArrayList<ResultFlag>(); if (entityColumn.isId()) { flags.add(ResultFlag.ID); } builder.flags(flags); resultMappings.add(builder.build()); } ResultMap.Builder builder = new ResultMap.Builder(configuration, "BaseMapperResultMap", this.entityClass, resultMappings, true); this.resultMap = builder.build(); return this.resultMap; } /** * ? - Example */ public void initPropertyMap() { propertyMap = new HashMap<String, EntityColumn>(getEntityClassColumns().size()); for (EntityColumn column : getEntityClassColumns()) { propertyMap.put(column.getProperty(), column); } } public Map<String, EntityColumn> getPropertyMap() { return propertyMap; } }