项目中表名称的统一命名使用的了NamingStrategy的命名策略。
在Java对象里面, 偶们知道一个良好的命名规范会采用大写单词的首字母, 比如订单项这个对象, 偶们会起名为OrderItem, 这样很容易就看出来这个对象是由Order和Item 2个单词组成的, 断词就很容易了, 而属性也是如此, 比如maxPrice, totalPrice等等.
但是如果按照同样的命名规范运到数据库的时候, 由于很多数据库对于表名, 字段名是大小写不敏感的, 所以最常见的策略是加下划线作为断词的依据:
OrderItem -> order_item
maxPrice -> max_price
这样运用Hibernate的时候, 偶们就得手工在mapping文件里面指明:
<class name="OrderItem" table="order_item">
<property name="maxPrice" column="max_price"/>
NamingStrategy的源代码如下:
public interface NamingStrategy {
/**
* 返回实体的表名
* Return a table name for an entity class
* @param className the fully-qualified class name 参数为类的完整名称的
* @return a table name
*/
public String classToTableName(String className);
/**将一个属性映射为一个表的列明地方法
* Return a column name for a property path expression
* @param propertyName a property path
* @return a column name
*/
public String propertyToColumnName(String propertyName);
/**根据表名获取的映射文件名称
* Alter the table name given in the mapping document
* @param tableName a table name
* @return a table name
*/
public String tableName(String tableName);
/**
* Alter the column name given in the mapping document
* @param columnName a column name
* @return a column name
*/
public String columnName(String columnName);
/**返回相关联的表名称
* Return a collection table name ie an association having a join table
*
* @param ownerEntity
* @param ownerEntityTable owner side table name
* @param associatedEntity
* @param associatedEntityTable reverse side table name if any
* @param propertyName collection role
*/
public String collectionTableName(
String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable,
String propertyName
);
/**
* Return the join key column name ie a FK column used in a JOINED strategy or for a secondary table
*
* @param joinedColumn joined column name (logical one) used to join with
* @param joinedTable joined table name (ie the referenced table) used to join with
*/
public String joinKeyColumnName(String joinedColumn, String joinedTable);
/**
* Return the foreign key column name for the given parameters
* @param propertyName the property name involved
* @param propertyEntityName
* @param propertyTableName the property table name involved (logical one)
* @param referencedColumnName the referenced column name involved (logical one)
*/
public String foreignKeyColumnName(
String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName
);
/**
* Return the logical column name used to refer to a column in the metadata
* (like index, unique constraints etc)
* A full bijection is required between logicalNames and physical ones
* logicalName have to be case insersitively unique for a given table
*
* @param columnName given column name if any
* @param propertyName property name of this column
*/
public String logicalColumnName(String columnName, String propertyName);
/**
* Returns the logical collection table name used to refer to a table in the mapping metadata
*
* @param tableName the metadata explicit name
* @param ownerEntityTable owner table entity table name (logical one)
* @param associatedEntityTable reverse side table name if any (logical one)
* @param propertyName collection role
*/
public String logicalCollectionTableName(String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName);
/**
* Returns the logical foreign key column name used to refer to this column in the mapping metadata
*
* @param columnName given column name in the metadata if any
* @param propertyName property name
* @param referencedColumn referenced column name (logical one) in the join
*/
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn);
}
源码如上必须注意的方法:
classToTableName:将一个类转换为表名
propertyToColumnName:属性转换为列名
tableName:根据表名返回映射文件
columnName:根据列名返回映射列名;
在初始化配置的时候, 把这个NamingStrategy加上: