Skip to content

Commit

Permalink
Use Guava and Apache lang3 instead of camel-name-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
alex yang committed Mar 19, 2018
1 parent f352d7a commit be76de4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.settings
.classpath
.project
target
target
jdbctemplatetool.iml
14 changes: 11 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,19 @@ Based on those questions I create JdbcTemplateTool which can provide these featu
<version>5.1.19</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.crazycake</groupId>
<artifactId>camel-name-utils</artifactId>
<version>1.0.0-RELEASE</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>

</dependencies>

<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public <T> List<T> list(String sql, Object[] params, Class<T> clazz) {
* get count
* @param sql
* @param params
* @param clazz
* @return
*/
public int count(String sql, Object[] params) {
Expand Down Expand Up @@ -103,8 +102,6 @@ public int count(String sql, Object[] params) {
/**
* 获取一个对象
* get object by id
* @param sql
* @param params
* @param clazz
* @return
* @throws NoIdAnnotationFoundException
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/org/crazycake/jdbcTemplateTool/utils/IdUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.crazycake.utils.CamelNameUtils;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang3.StringUtils;


public class IdUtils {
Expand All @@ -31,7 +32,7 @@ public static String getAutoGeneratedId(Object po) throws SecurityException, NoS
}

//获取getter方法
String getterName = "get" + CamelNameUtils.capitalize(f.getName());
String getterName = "get" + StringUtils.capitalize(f.getName());
Method getter = po.getClass().getDeclaredMethod(getterName);

Id idAnno = getter.getAnnotation(Id.class);
Expand Down Expand Up @@ -60,9 +61,27 @@ public static String getAutoGeneratedId(Object po) throws SecurityException, NoS
* @throws NoSuchMethodException
*/
public static void setAutoIncreamentIdValue(Object po,String autoGeneratedId,Object idValue) throws Exception, NoSuchMethodException{
String setterName = "set" + CamelNameUtils.capitalize(autoGeneratedId);
String setterName = "set" + StringUtils.capitalize(autoGeneratedId);
Method setter = po.getClass().getDeclaredMethod(setterName, idValue.getClass());
setter.invoke(po, idValue);
}


/**
* Convert underscore style to camel style
* @param name
* @return
*/
public static String toCamel(String name) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
}

/**
* Convert camel style to underscore style
* @param name
* @return
*/
public static String toUnderscore(String name) {
return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import javax.persistence.Table;
import javax.persistence.Transient;

import org.crazycake.jdbcTemplateTool.exception.NoColumnAnnotationFoundException;
import org.apache.commons.lang3.StringUtils;
import org.crazycake.jdbcTemplateTool.exception.NoColumnAnnotationFoundException;
import org.crazycake.jdbcTemplateTool.exception.NoDefinedGetterException;
import org.crazycake.jdbcTemplateTool.exception.NoIdAnnotationFoundException;
import org.crazycake.jdbcTemplateTool.model.SqlParamsPairs;
import org.crazycake.utils.CamelNameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -114,7 +114,7 @@ public static <T> SqlParamsPairs getInsertFromObject(T po) throws Exception{
* @return
*/
private static <T> Method getGetter(Class<T> clazz, Field f){
String getterName = "get" + CamelNameUtils.capitalize(f.getName());
String getterName = "get" + StringUtils.capitalize(f.getName());
Method getter = null;
try {
getter = clazz.getMethod(getterName);
Expand All @@ -126,7 +126,6 @@ private static <T> Method getGetter(Class<T> clazz, Field f){

/**
* 从po类获取表名
* @param po
* @return
*/
private static <T> String getTableName(Class<T> clazz) {
Expand All @@ -140,7 +139,7 @@ private static <T> String getTableName(Class<T> clazz) {
}
//if Table annotation is null
String className = clazz.getName();
return CamelNameUtils.camel2underscore(className.substring(className.lastIndexOf(".")+1));
return IdUtils.toUnderscore(className.substring(className.lastIndexOf(".")+1));
}


Expand Down Expand Up @@ -296,7 +295,6 @@ public static SqlParamsPairs getDeleteFromObject(Object po) throws Exception{

/**
* 获取根据主键查对象的sql和参数
* @param po
* @param id
* @return
* @throws NoIdAnnotationFoundException
Expand Down Expand Up @@ -357,7 +355,6 @@ public static <T> SqlParamsPairs getGetFromObject(Class<T> clazz,Object id) thro
/**
* use getter to guess column name, if there is annotation then use annotation value, if not then guess from field name
* @param getter
* @param clazz
* @param f
* @return
* @throws NoColumnAnnotationFoundException
Expand All @@ -372,7 +369,7 @@ private static String getColumnNameFromGetter(Method getter,Field f){

if(columnName == null || "".equals(columnName)){
//如果没有列注解就用命名方式去猜
columnName = CamelNameUtils.camel2underscore(f.getName());
columnName = IdUtils.toUnderscore(f.getName());
}
return columnName;
}
Expand Down

0 comments on commit be76de4

Please sign in to comment.