conditionParam) {
82 | this.conditionParam = conditionParam;
83 | }
84 |
85 | public Integer getPageSize() {
86 | return pageSize;
87 | }
88 |
89 | public void setPageSize(Integer pageSize) {
90 | this.pageSize = pageSize;
91 | }
92 |
93 | public Integer getPageNo() {
94 | return pageNo;
95 | }
96 |
97 | public void setPageNo(Integer pageNo) {
98 | this.pageNo = pageNo;
99 | }
100 |
101 | public String getOrderExp() {
102 | return orderExp;
103 | }
104 |
105 | public void setOrderExp(String orderExp) {
106 | this.orderExp = orderExp;
107 | }
108 |
109 | }
110 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/SQLColumn.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud;
2 |
3 | /**
4 | * SQL字段对象
5 | * java对象与jdbc转换
6 | * Mybatis-insert update中Oracle.data类型对应的Java对象需要声明jdbcType=TIMESTAMP
7 | *
8 | * 使用SQLColumn工厂(@see SQLColumnFactory)创建对象
9 | *
10 | * @author svili
11 | * @data 2017年3月27日
12 | *
13 | */
14 | public class SQLColumn {
15 |
16 | private String columnName;
17 |
18 | private Object columnValue;
19 |
20 | private String jdbcType;
21 |
22 | public String getColumnName() {
23 | return columnName;
24 | }
25 |
26 | public void setColumnName(String columnName) {
27 | this.columnName = columnName;
28 | }
29 |
30 | public Object getColumnValue() {
31 | return columnValue;
32 | }
33 |
34 | public void setColumnValue(Object columnValue) {
35 | this.columnValue = columnValue;
36 | }
37 |
38 | public String getJdbcType() {
39 | return jdbcType;
40 | }
41 |
42 | public void setJdbcType(String jdbcType) {
43 | this.jdbcType = jdbcType;
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/common/DateUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.common;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Calendar;
5 | import java.util.Date;
6 |
7 | /**
8 | * 时间格式化工具类
9 | *
10 | * @author svili
11 | * @date 2016年11月17日
12 | *
13 | */
14 | public class DateUtil {
15 |
16 | /**
17 | * 普通格式化:yyyy-MM-dd
18 | *
19 | */
20 | public static String formatSimple() {
21 | return formatSimple(System.currentTimeMillis());
22 | }
23 |
24 | /**
25 | * 普通格式化:yyyy-MM-dd
26 | *
27 | */
28 | public static String formatSimple(Date date) {
29 | return formatSimple(date.getTime());
30 | }
31 |
32 | /**
33 | * 普通格式化:yyyy-MM-dd
34 | *
35 | */
36 | public static String formatSimple(long time) {
37 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
38 | return simpleDateFormat.format(time);
39 | }
40 |
41 | /**
42 | * 带时分秒:yyyy-MM-dd HH:mm:ss
43 | *
44 | */
45 | public static String format() {
46 | return format(System.currentTimeMillis());
47 | }
48 |
49 | /**
50 | * 带时分秒:yyyy-MM-dd HH:mm:ss
51 | *
52 | */
53 | public static String format(Date date) {
54 | return format(date.getTime());
55 | }
56 |
57 | /**
58 | * 带时分秒:yyyy-MM-dd HH:mm:ss
59 | *
60 | */
61 | public static String format(long time) {
62 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
63 | return simpleDateFormat.format(time);
64 | }
65 |
66 | /**
67 | * 自定义
68 | *
69 | * @param exp
70 | * 表达式
71 | */
72 | public static String format(String exp) {
73 | return format(System.currentTimeMillis(), exp);
74 | }
75 |
76 | /**
77 | * 自定义
78 | *
79 | * @param exp
80 | * 表达式
81 | */
82 | public static String format(Date date, String exp) {
83 | return format(date.getTime(), exp);
84 | }
85 |
86 | /**
87 | * 自定义
88 | *
89 | * @param exp
90 | * 表达式
91 | */
92 | public static String format(long time, String exp) {
93 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(exp);
94 | return simpleDateFormat.format(time);
95 | }
96 |
97 | /**
98 | * 日期
99 | */
100 | public static int getDay() {
101 | return getDay(System.currentTimeMillis());
102 | }
103 |
104 | public static int getDay(Date date) {
105 | return getDay(date.getTime());
106 | }
107 |
108 | public static int getDay(long time) {
109 | Calendar cal = Calendar.getInstance();
110 | cal.setTimeInMillis(time);
111 | return cal.get(Calendar.DATE);
112 | }
113 |
114 | /**
115 | * 月份
116 | */
117 | public static int getMonth() {
118 | return getMonth(System.currentTimeMillis());
119 | }
120 |
121 | public static int getMonth(Date date) {
122 | return getMonth(date.getTime());
123 | }
124 |
125 | public static int getMonth(long time) {
126 | Calendar cal = Calendar.getInstance();
127 | cal.setTimeInMillis(time);
128 | return cal.get(Calendar.MONTH) + 1;
129 | }
130 |
131 | /**
132 | * 年份
133 | */
134 | public static int getYear() {
135 | return getYear(System.currentTimeMillis());
136 | }
137 |
138 | public static int getYear(Date date) {
139 | return getYear(date.getTime());
140 | }
141 |
142 | public static int getYear(long time) {
143 | Calendar cal = Calendar.getInstance();
144 | cal.setTimeInMillis(time);
145 | return cal.get(Calendar.YEAR);
146 | }
147 |
148 | /**
149 | * sql.Date
150 | */
151 | public static java.sql.Date toSQLDate(Date date) {
152 | return date instanceof java.sql.Date ? (java.sql.Date) date : toSQLDate(date.getTime());
153 | }
154 |
155 | public static java.sql.Date toSQLDate(long time) {
156 | return new java.sql.Date(time);
157 | }
158 |
159 | public static java.sql.Date getSQLDate() {
160 | return toSQLDate(System.currentTimeMillis());
161 | }
162 |
163 | /**
164 | * sql.Timestamp
165 | */
166 | public static java.sql.Timestamp toTimestamp(Date date) {
167 | return date instanceof java.sql.Timestamp ? (java.sql.Timestamp) date : toTimestamp(date.getTime());
168 | }
169 |
170 | public static java.sql.Timestamp toTimestamp(long time) {
171 | return new java.sql.Timestamp(time);
172 | }
173 |
174 | public static java.sql.Timestamp getTimestamp() {
175 | return toTimestamp(System.currentTimeMillis());
176 | }
177 |
178 | }
179 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/common/NumberUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.common;
2 |
3 | import java.math.BigDecimal;
4 |
5 | /**
6 | * Number格式化工具类
7 | *
8 | * @author svili
9 | * @date 2017年2月18日
10 | *
11 | */
12 | public class NumberUtil {
13 |
14 | public static byte toByte(Number number) {
15 | return number.byteValue();
16 | }
17 |
18 | public static double toDouble(Number number) {
19 | return number.doubleValue();
20 | }
21 |
22 | public static float toFloat(Number number) {
23 | return number.floatValue();
24 | }
25 |
26 | public static int toInt(Number number) {
27 | return number.intValue();
28 | }
29 |
30 | public static long toLong(Number number) {
31 | return number.longValue();
32 | }
33 |
34 | public static short toShort(Number number) {
35 | return number.shortValue();
36 | }
37 |
38 | public static BigDecimal toBigDecimal(Number number) {
39 | if (number instanceof BigDecimal) {
40 | return (BigDecimal) number;
41 | } else {
42 | return new BigDecimal(number.doubleValue());
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/common/PersistentUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.common;
2 |
3 | import java.lang.reflect.Field;
4 | import java.util.ArrayList;
5 | import java.util.HashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import javax.persistence.Column;
10 | import javax.persistence.Id;
11 | import javax.persistence.Table;
12 | import javax.persistence.Transient;
13 |
14 | import org.apache.ibatis.reflection.ReflectionException;
15 |
16 | import com.svili.portal.crud.utils.FieldReflectUtil;
17 |
18 | /**
19 | * 持久化工具类
20 | *
21 | * @author svili
22 | *
23 | */
24 | public class PersistentUtil {
25 |
26 | /**
27 | * 驼峰_下划线转换,默认开启
28 | * 对@Table注解无效,支持ClassName_TableName
29 | * 对@Column注解无效,支持fieldName_columnName
30 | */
31 | private static final boolean CAMEL_TO_UNDERLINE = true;
32 |
33 | /**
34 | * 获取Java对象对应的表名
35 | * 默认下划线风格
36 | *
37 | * @param clazz
38 | * pojo类class对象
39 | * @return tableName
40 | */
41 | public static String getTableName(Class> clazz) {
42 | // 判断是否有Table注解
43 | if (clazz.isAnnotationPresent(Table.class)) {
44 | // 获取注解对象
45 | Table table = clazz.getAnnotation(Table.class);
46 | // 设置了name属性
47 | if (!table.name().trim().equals("")) {
48 | return table.name();
49 | }
50 | }
51 | // 类名
52 | String className = clazz.getSimpleName();
53 |
54 | if (!CAMEL_TO_UNDERLINE) {
55 | return className;
56 | } else {
57 | // 驼峰转下划线
58 | return StringUtil.camelToUnderline(className);
59 | }
60 | }
61 |
62 | /**
63 | * 获取列名
64 | * 注解优先,javax.persistence.Column name属性值。
65 | * 无注解,将字段名转为字符串,默认下划线风格.
66 | *
67 | * @param field
68 | * pojo字段对象
69 | * @return
70 | */
71 | public static String getColumnName(Field field) {
72 | if (field.isAnnotationPresent(javax.persistence.Column.class)) {
73 | // 获取注解对象
74 | Column column = field.getAnnotation(Column.class);
75 | // 设置了name属性
76 | if (!column.name().trim().equals("")) {
77 | return column.name();
78 | }
79 | }
80 | if (!CAMEL_TO_UNDERLINE) {
81 | return field.getName();
82 | } else {
83 | return StringUtil.camelToUnderline(field.getName());
84 | }
85 | }
86 |
87 | /**
88 | * 获取pojo主键字段
89 | * javax.persistence.Id注解的字段不存在返回null
90 | *
91 | * @param clazz
92 | * pojo类-class对象
93 | * @return Field
94 | */
95 | public static Field getPrimaryFieldNotCareNull(Class> clazz) {
96 | Field field = FieldReflectUtil.findField(clazz, Id.class);
97 | if (field != null) {
98 | return field;
99 | } else {
100 | return null;
101 | }
102 | }
103 |
104 | /**
105 | * 获取pojo主键字段
106 | *
107 | * 主键必须存在,不存在抛异常
108 | *
109 | * @param clazz
110 | * pojo类-class对象
111 | * @return Field
112 | * @throws ReflectionExceptio
113 | * @see org.apache.ibatis.reflection.ReflectionException
114 | */
115 | public static Field getPrimaryField(Class> clazz) {
116 | Field field = getPrimaryFieldNotCareNull(clazz);
117 | if (field != null) {
118 | return field;
119 | } else {
120 | throw new ReflectionException(
121 | "no search result for javax.persistence.Id annotation from " + clazz.getName());
122 | }
123 | }
124 |
125 | /**
126 | * 获取pojo主键列名
127 | *
128 | * @param clazz
129 | * pojo类-class对象
130 | * @return underline-columnName
131 | * @throws ReflectionExceptio
132 | * @see org.apache.ibatis.reflection.ReflectionException
133 | */
134 | public static String getPrimaryKey(Class> clazz) {
135 | Field primaryField = getPrimaryField(clazz);
136 | return getColumnName(primaryField);
137 | }
138 |
139 | /**
140 | * 根据列名获取字段
141 | * 无匹配的字段抛异常
142 | *
143 | * @param clazz
144 | * pojo类class对象
145 | * @param columnName
146 | * 列名
147 | * @return
148 | */
149 | public static Field getFieldByColumnName(Class> clazz, String columnName) {
150 | Map mapping = getColumnFieldMapping(clazz);
151 | Field field = mapping.get(clazz.getName() + "." + columnName);
152 | if (field == null) {
153 | throw new ReflectionException(
154 | "no search matched field to columnName :" + columnName + " from " + clazz.getName());
155 | }
156 | return field;
157 | }
158 |
159 | /**
160 | * 列名-字段Mapping
161 | * key = clazz.getName() + "." + columnName
162 | * value = Field
163 | * 可以将此方法的返回结果存储到容器中
164 | *
165 | * @param clazz
166 | * pojo类class对象
167 | * @return
168 | */
169 | public static Map getColumnFieldMapping(Class> clazz) {
170 | List fieldList = getPersistentFields(clazz);
171 | if (fieldList == null || fieldList.size() == 0) {
172 | return null;
173 | }
174 | Map mapping = new HashMap();
175 | String className = clazz.getName();
176 | for (Field field : fieldList) {
177 | mapping.put(className + "." + getColumnName(field), field);
178 | }
179 | return mapping;
180 | }
181 |
182 | /**
183 | * 获取持久化字段
184 | * 可以将此方法的返回结果存储到容器中
185 | *
186 | * @param clazz
187 | * class对象
188 | * @return
189 | */
190 | public static List getPersistentFields(Class> clazz) {
191 | List list = new ArrayList();
192 | Class> searchType = clazz;
193 | while (!Object.class.equals(searchType) && searchType != null) {
194 | Field[] fields = searchType.getDeclaredFields();
195 | for (Field field : fields) {
196 | if (isPersistentField(field)) {
197 | list.add(field);
198 | }
199 | }
200 | searchType = searchType.getSuperclass();
201 | }
202 | return list;
203 | }
204 |
205 | /**
206 | * 是否为持久化字段
207 | * javax.persistence.Transient注解为非持久化字段
208 | *
209 | * @param field
210 | * Field对象
211 | * @return
212 | */
213 | public static boolean isPersistentField(Field field) {
214 | return !field.isAnnotationPresent(Transient.class);
215 | }
216 | }
217 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/common/StringUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.common;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * 字符串工具类
7 | *
8 | * @author svili
9 | * @date 2016年11月8日
10 | *
11 | */
12 | public class StringUtil {
13 |
14 | /**
15 | * 将驼峰标识转换为下划线
16 | *
17 | * @param text
18 | * @return camel
19 | */
20 | public static String camelToUnderline(String text) {
21 | if (text == null || "".equals(text.trim())) {
22 | return "";
23 | }
24 | StringBuffer result = new StringBuffer(text.length() + 1);
25 | result.append(text.substring(0, 1));
26 | for (int i = 1; i < text.length(); i++) {
27 | if (!Character.isLowerCase(text.charAt(i))) {
28 | result.append('_');
29 | }
30 | result.append(text.substring(i, i + 1));
31 | }
32 | return result.toString().toLowerCase();
33 | }
34 |
35 | /**
36 | * 将下划线标识转换为驼峰
37 | *
38 | * @param text
39 | * @return underline
40 | */
41 | public static String underlineToCamel(String text) {
42 | if (text == null || "".equals(text.trim())) {
43 | return "";
44 | }
45 | int length = text.length();
46 | StringBuffer result = new StringBuffer();
47 | for (int i = 0; i < length; i++) {
48 | char c = text.charAt(i);
49 | if (c == '_') {
50 | if (++i < length) {
51 | result.append(Character.toUpperCase(text.charAt(i)));
52 | }
53 | } else {
54 | result.append(c);
55 | }
56 | }
57 | return result.toString();
58 | }
59 |
60 | /**
61 | * 将String list字符串集合用逗号","分割
62 | *
63 | * @param list
64 | * 集合
65 | * @return string
66 | */
67 | public static String splitListByComma(List list) {
68 | StringBuffer result = new StringBuffer();
69 | for (String str : list) {
70 | result.append(",").append(str);
71 | }
72 | return result.substring(1, result.length());
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/common/TypeCastUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.common;
2 |
3 | public class TypeCastUtil {
4 |
5 | public static Number castToNumber(Object value) throws Exception {
6 | if (value == null) {
7 | return null;
8 | }
9 | if (TypeIdentifyUtil.isNumberType(value)) {
10 | return (Number) value;
11 | }
12 | if (value instanceof Boolean || value.getClass().equals(Boolean.TYPE)) {
13 | return Boolean.valueOf((boolean) value) ? 1 : 0;
14 | }
15 | if (value instanceof String) {
16 | // 未完成
17 | }
18 | throw new Exception(value + " : value is not Number type or Boolean type, can not castToNumber");
19 | }
20 |
21 | public static Boolean castToBoolean(Object value) throws Exception {
22 | if (value == null) {
23 | return null;
24 | }
25 | if (TypeIdentifyUtil.isBooleanType(value)) {
26 | return (Boolean) value;
27 | }
28 | if (value instanceof Number) {
29 | return ((Number) value).intValue() == 1 ? true : false;
30 | }
31 | if (value instanceof String) {
32 | return Boolean.parseBoolean((String) value);
33 | }
34 |
35 | throw new Exception(value + " : value is not Number type or Boolean type, can not castToBoolean");
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/common/TypeIdentifyUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.common;
2 |
3 | /**
4 | * 类型识别工具类
5 | *
6 | * @author svili
7 | * @data 2017年3月30日
8 | *
9 | */
10 | public class TypeIdentifyUtil {
11 |
12 | /**
13 | * 布尔类型
14 | * null对象返回false
15 | *
16 | * @param obj
17 | * 待识别的对象
18 | * @return
19 | */
20 | public static boolean isBooleanType(Object obj) {
21 | if (obj == null) {
22 | return false;
23 | }
24 |
25 | if (obj instanceof Boolean || obj.getClass().equals(Boolean.TYPE)) {
26 | return true;
27 | } else {
28 | return false;
29 | }
30 | }
31 |
32 | /**
33 | * 数值类型
34 | * null对象返回false
35 | *
36 | * @param obj
37 | * 待识别的对象
38 | * @return
39 | */
40 | public static boolean isNumberType(Object obj) {
41 |
42 | if (obj == null) {
43 | return false;
44 | }
45 |
46 | if (obj instanceof Number) {
47 | return true;
48 | }
49 |
50 | if (obj.getClass().equals(Byte.TYPE)) {
51 | return true;
52 | }
53 |
54 | if (obj.getClass().equals(Short.TYPE)) {
55 | return true;
56 | }
57 |
58 | if (obj.getClass().equals(Integer.TYPE)) {
59 | return true;
60 | }
61 |
62 | if (obj.getClass().equals(Long.TYPE)) {
63 | return true;
64 | }
65 |
66 | if (obj.getClass().equals(Float.TYPE)) {
67 | return true;
68 | }
69 |
70 | if (obj.getClass().equals(Double.TYPE)) {
71 | return true;
72 | }
73 |
74 | return false;
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/utils/DateFieldReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.utils;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | import org.apache.ibatis.reflection.ReflectionException;
6 | import org.springframework.util.ReflectionUtils;
7 |
8 | import com.svili.portal.crud.common.DateUtil;
9 |
10 | /**
11 | * 时间字段处理
12 | *
13 | * @author svili
14 | * @date 2017年2月18日
15 | *
16 | */
17 | public class DateFieldReflectUtil {
18 |
19 | /**
20 | * 设置Date类型字段值
21 | *
22 | * @param target
23 | * the target object from which to get the field
24 | * @param field
25 | * the field to set
26 | * @param value
27 | * java.util.Date
28 | * @throws Exception
29 | * IllegalArgumentException, IllegalAccess
30 | */
31 | public static void setFieldDateValue(Object target, Field field, Object value) throws Exception {
32 |
33 | if (!java.util.Date.class.isAssignableFrom(field.getType())) {
34 | throw new ReflectionException(target.getClass().getName() + "." + field.getName()
35 | + " : field type is not Date, can not convertToDate");
36 | }
37 |
38 | if (!field.isAccessible()) {
39 | ReflectionUtils.makeAccessible(field);
40 | }
41 |
42 | if (value == null) {
43 | field.set(target, null);
44 | return;
45 | }
46 |
47 | if (!(value instanceof java.util.Date)) {
48 | throw new ReflectionException(value + " : is not Date type value , can not convertToDate to field "
49 | + target.getClass().getName() + "." + field.getName());
50 | }
51 |
52 | if (field.getType().equals(java.sql.Date.class)) {
53 | field.set(target, DateUtil.toSQLDate((java.util.Date) value));
54 | return;
55 | }
56 |
57 | if (field.getType().equals(java.sql.Timestamp.class)) {
58 | field.set(target, DateUtil.toTimestamp((java.util.Date) value));
59 | return;
60 | }
61 | //其他Date类型 未完成
62 |
63 | field.set(target, value);
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/utils/EnumFieldReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.utils;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | import org.apache.ibatis.reflection.ReflectionException;
6 | import org.springframework.util.ReflectionUtils;
7 |
8 | /**
9 | * 枚举类型字段处理
10 | *
11 | * @author svili
12 | * @date 2016年11月10日
13 | *
14 | */
15 | public class EnumFieldReflectUtil {
16 |
17 | /**
18 | * 获取枚举类型的字段值-ordinal
19 | *
20 | * @param target
21 | * the target object from which to get the field
22 | * @param field
23 | * the field to get
24 | * @return enum.ordinal
25 | * @throws Exception
26 | * IllegalArgumentException, IllegalAccess
27 | */
28 | @SuppressWarnings("rawtypes")
29 | public static int getFieldEnumOrdinal(Object target, Field field) throws Exception {
30 |
31 | if (!field.getType().isEnum()) {
32 | throw new ReflectionException(target.getClass().getName() + "." + field.getName()
33 | + ":field type is not Enum, can not convertToEnum");
34 | }
35 |
36 | if (!field.isAccessible()) {
37 | ReflectionUtils.makeAccessible(field);
38 | }
39 |
40 | return ((Enum) field.get(target)).ordinal();
41 | }
42 |
43 | /**
44 | *
45 | * 设置枚举类型的字段值
46 | *
47 | * @param target
48 | * the target object from which to get the field
49 | * @param field
50 | * the field to set
51 | * @param ordinal
52 | * enum.ordinal
53 | * @throws Exception
54 | * IllegalArgumentException, IllegalAccess
55 | */
56 | @SuppressWarnings("rawtypes")
57 | public static void setFieldEnumValueByOrdinal(Object target, Field field, Integer ordinal) throws Exception {
58 | if (!field.getType().isEnum()) {
59 | throw new ReflectionException(target.getClass().getName() + "." + field.getName()
60 | + " : field type is not Enum, can not convertToEnum");
61 | }
62 |
63 | if (!field.isAccessible()) {
64 | ReflectionUtils.makeAccessible(field);
65 | }
66 |
67 | if(ordinal == null){
68 | field.set(target, null);
69 | return;
70 | }
71 |
72 | Enum[] enumObjs = (Enum[]) (field.getType()).getEnumConstants();
73 | for (Enum enumObj : enumObjs) {
74 | if (enumObj.ordinal() == ordinal) {
75 | field.set(target, enumObj);
76 | }
77 | }
78 |
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/utils/FieldReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.utils;
2 |
3 | import java.lang.annotation.Annotation;
4 | import java.lang.reflect.Field;
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | import org.springframework.util.ReflectionUtils;
9 |
10 | import com.svili.portal.crud.common.NumberUtil;
11 | import com.svili.portal.crud.common.TypeCastUtil;
12 |
13 | /**
14 | * Field反射工具类
15 | *
16 | * @author svili
17 | * @date 2016年11月17日
18 | *
19 | */
20 | public class FieldReflectUtil {
21 |
22 | /**
23 | * 设定目标对象指定的字段值
24 | * oracle Number类型返回的是BigDecimal
25 | * mysql tinyint(1)返回的是Boolean类型
26 | *
27 | * @param t
28 | * 对象
29 | * @param field
30 | * 字段
31 | * @throws Exception
32 | * IllegalArgumentException, IllegalAccessException
33 | */
34 | public static void setFieldValue(T t, Field field, Object value) throws Exception {
35 | ReflectionUtils.makeAccessible(field);
36 |
37 | //空值处理
38 | if(value == null){
39 | field.set(t, null);
40 | return;
41 | }
42 |
43 | //基本数据类型 未完成
44 | if(field.getType().isPrimitive()){
45 |
46 | }
47 |
48 | // Enum类型字段处理
49 | if (field.getType().isEnum()) {
50 | //Enum类型的字段在数据库中存储其ordinal
51 | Number number = TypeCastUtil.castToNumber(value);
52 | int ordinal = NumberUtil.toInt(number);
53 | EnumFieldReflectUtil.setFieldEnumValueByOrdinal(t, field, ordinal);
54 | return;
55 | }
56 |
57 | // Boolean类型字段处理
58 | if (field.getType().equals(Boolean.class)) {
59 | boolean b = TypeCastUtil.castToBoolean(value);
60 | field.set(t, b);
61 | return;
62 | }
63 |
64 | // Number类型字段处理
65 | if (Number.class.isAssignableFrom(field.getType())) {
66 | // oracle中Number类型返回的是BigDecimal
67 | NumberFieldReflectUtil.setFieldNumberValue(t, field, (Number) value);
68 | return;
69 | }
70 |
71 | // Date类型字段处理
72 | if (java.util.Date.class.isAssignableFrom(field.getType())) {
73 | DateFieldReflectUtil.setFieldDateValue(t, field, value);
74 | return;
75 | }
76 | field.set(t, value);
77 | }
78 |
79 | /**
80 | * 获取目标对象指定的字段值
81 | *
82 | * 空值返回null
83 | *
84 | *
85 | * @param t
86 | * 对象
87 | * @param field
88 | * 字段
89 | * @return value
90 | * @throws Exception
91 | * IllegalArgumentException, IllegalAccessException
92 | */
93 | public static Object getFieldValue(T t, Field field) throws Exception {
94 | ReflectionUtils.makeAccessible(field);
95 | if (field.get(t) == null) {
96 | return null;
97 | }
98 | // Enum类型字段处理
99 | if (field.getType().isEnum()) {
100 | return EnumFieldReflectUtil.getFieldEnumOrdinal(t, field);
101 | }
102 | return field.get(t);
103 | }
104 |
105 | /**
106 | * 获取class类中指定注解类型的field对象
107 | *
108 | * @param clazz
109 | * pojo类-class对象
110 | * @param annotationType
111 | * 注解类-class对象
112 | * @return Field or null
113 | */
114 | public static Field findField(Class> clazz, Class extends Annotation> annotationType) {
115 | Class> searchType = clazz;
116 | while (!Object.class.equals(searchType) && searchType != null) {
117 | Field[] fields = searchType.getDeclaredFields();
118 | for (Field field : fields) {
119 | if (field.isAnnotationPresent(annotationType)) {
120 | return field;
121 | }
122 | }
123 | searchType = searchType.getSuperclass();
124 | }
125 | return null;
126 | }
127 |
128 | /**
129 | * 获取所有字段
130 | * 暂时无用
131 | *
132 | * @param clazz
133 | * class对象
134 | * @return
135 | */
136 | @Deprecated
137 | public static List getAllField(Class> clazz) {
138 | List list = new ArrayList();
139 | Class> searchType = clazz;
140 | while (!Object.class.equals(searchType) && searchType != null) {
141 | Field[] fields = searchType.getDeclaredFields();
142 | for (Field field : fields) {
143 | list.add(field);
144 | }
145 | searchType = searchType.getSuperclass();
146 | }
147 | return list;
148 | }
149 |
150 | }
151 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/utils/GeneralMapperReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.utils;
2 |
3 | import java.lang.reflect.Field;
4 | import java.util.ArrayList;
5 | import java.util.LinkedHashMap;
6 | import java.util.List;
7 | import java.util.Map;
8 | import java.util.Map.Entry;
9 |
10 | import com.svili.portal.crud.SQLColumn;
11 | import com.svili.portal.crud.common.PersistentUtil;
12 |
13 |
14 | /**
15 | * 映射工具类
16 | *
17 | * @author svili
18 | * @date 2016年11月8日
19 | *
20 | */
21 | public class GeneralMapperReflectUtil {
22 |
23 | /**
24 | * 获取主键值
25 | * @param t pojo对象
26 | * @return
27 | */
28 | public static Object getPrimaryValue(T t) throws Exception{
29 | return FieldReflectUtil.getFieldValue(t, PersistentUtil.getPrimaryField(t.getClass()));
30 | }
31 |
32 | /**
33 | * 获取pojo对应table 的所有列名
34 | *
35 | * @param clazz
36 | * pojo类-class对象
37 | * @return columnNames
38 | */
39 | public static List getAllColumnNames(Class> clazz) {
40 | List columnNames = new ArrayList();
41 | List fields = PersistentUtil.getPersistentFields(clazz);
42 | for (Field field : fields) {
43 | columnNames.add(PersistentUtil.getColumnName(field));
44 | }
45 | return columnNames;
46 | }
47 |
48 | /**
49 | * 获取pojo对应table除主键外的所有列名
50 | *
51 | * @param clazz
52 | * pojo类-class对象
53 | * @return columnNames
54 | */
55 | public static List getColumnNamesExceptPrimaryKey(Class> clazz) {
56 | List columnNames = new ArrayList();
57 |
58 | Field primaryField = PersistentUtil.getPrimaryFieldNotCareNull(clazz);
59 |
60 | List fields = PersistentUtil.getPersistentFields(clazz);
61 |
62 | for (Field field : fields) {
63 | if (primaryField != null && !field.equals(primaryField)) {
64 | columnNames.add(PersistentUtil.getColumnName(field));
65 | }
66 | }
67 | return columnNames;
68 | }
69 |
70 | /**
71 | * 获取pojo除空值外的所有列名-字段值mapping
72 | *
73 | * @param
74 | * pojo类
75 | *
76 | * @param t
77 | * pojo对象
78 | * @return columnName-value
79 | * @throws Exception
80 | */
81 | public static Map getColumnValueMappingExceptNull(T t) throws Exception {
82 | Map mapping = new LinkedHashMap();
83 |
84 | List fields = PersistentUtil.getPersistentFields(t.getClass());
85 |
86 | for (Field field : fields) {
87 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field);
88 | if (fieldValue != null) {
89 | mapping.put(PersistentUtil.getColumnName(field), fieldValue);
90 | }
91 | }
92 |
93 | return mapping;
94 | }
95 |
96 | public static List getSQLColumnsExceptNull(T t) throws Exception{
97 | List sqlColumns = new ArrayList();
98 | List fields = PersistentUtil.getPersistentFields(t.getClass());
99 |
100 | for (Field field : fields) {
101 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field);
102 | if (fieldValue != null) {
103 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field));
104 | }
105 | }
106 | return sqlColumns;
107 | }
108 |
109 | public static List getAllSQLColumns(T t) throws Exception {
110 | List sqlColumns = new ArrayList();
111 |
112 | List fields = PersistentUtil.getPersistentFields(t.getClass());
113 |
114 | for (Field field : fields) {
115 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field));
116 | }
117 | return sqlColumns;
118 | }
119 |
120 | public static List getSQLColumns(T t, boolean isContainsPrimaryKey,
121 | boolean isContainsNullValue) throws Exception {
122 | List sqlColumns = new ArrayList();
123 |
124 | List fields = PersistentUtil.getPersistentFields(t.getClass());
125 |
126 | // 主键字段
127 | Field primaryField = PersistentUtil.getPrimaryField(t.getClass());
128 |
129 | if (!isContainsPrimaryKey && primaryField != null) {
130 | // 不包含主键字段,移除
131 | fields.remove(primaryField);
132 | }
133 |
134 | for (Field field : fields) {
135 | // 字段值
136 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field);
137 |
138 | if (fieldValue == null) {
139 | // 空值
140 | if (isContainsNullValue) {
141 | // 包含空值 添加至结果集
142 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field));
143 | }
144 |
145 | } else {
146 | // 非空值 直接添加至结果集
147 | sqlColumns.add(SQLColumnFactory.createSQLColumn(t, field));
148 | }
149 |
150 | }
151 |
152 | return sqlColumns;
153 | }
154 |
155 |
156 | /**
157 | * 获取pojo对应table的所有列名-字段值mapping
158 | *
159 | * @param
160 | * pojo类
161 | *
162 | * @param t
163 | * pojo对象
164 | * @return columnName-value
165 | * @throws Exception
166 | * IllegalArgumentException, IllegalAccessException
167 | * @see FieldReflectUtil.getFieldValue
168 | */
169 | public static Map getAllColumnValueMapping(T t) throws Exception {
170 | Map mapping = new LinkedHashMap();
171 |
172 | List fields = PersistentUtil.getPersistentFields(t.getClass());
173 |
174 | for (Field field : fields) {
175 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field);
176 | mapping.put(PersistentUtil.getColumnName(field), fieldValue);
177 | }
178 | return mapping;
179 | }
180 |
181 | /**
182 | * 获取pojo对应table的 列名-字段值mapping
183 | *
184 | * @param
185 | * pojo类
186 | *
187 | * @param t
188 | * pojo对象
189 | * @param isContainsPrimaryKey
190 | * 是否包含主键
191 | * @param isContainsNullValue
192 | * 是否包含空值
193 | * @return columnName-value
194 | * @throws Exception
195 | */
196 | public static Map getColumnValueMapping(T t, boolean isContainsPrimaryKey,
197 | boolean isContainsNullValue) throws Exception {
198 | Map mapping = new LinkedHashMap();
199 |
200 | List fields = PersistentUtil.getPersistentFields(t.getClass());
201 |
202 | // 主键字段
203 | Field primaryField = PersistentUtil.getPrimaryField(t.getClass());
204 |
205 | if (!isContainsPrimaryKey && primaryField != null) {
206 | // 不包含主键字段,移除
207 | fields.remove(primaryField);
208 | }
209 |
210 | for (Field field : fields) {
211 | // 字段值
212 | Object fieldValue = FieldReflectUtil.getFieldValue(t, field);
213 |
214 | if (fieldValue == null) {
215 | // 空值
216 | if (isContainsNullValue) {
217 | // 包含空值 添加至结果集
218 | mapping.put(PersistentUtil.getColumnName(field), fieldValue);
219 | }
220 |
221 | } else {
222 | // 非空值 直接添加至结果集
223 | mapping.put(PersistentUtil.getColumnName(field), fieldValue);
224 | }
225 |
226 | }
227 |
228 | return mapping;
229 | }
230 |
231 | /**
232 | * 列名-值 键值对转为pojo
233 | * 若mapping为空,则返回null对象
234 | *
235 | * @param mapping
236 | * columnName-value mapping
237 | * @param clazz
238 | * 待转换的pojo类-class对象
239 | * @throws Exception
240 | * InstantiationException, IllegalAccessException
241 | */
242 | public static T parseToBean(Map mapping, Class clazz) throws Exception {
243 | if (mapping == null || mapping.size() == 0) {
244 | return null;
245 | }
246 |
247 | T t = clazz.newInstance();
248 | for (Entry entry : mapping.entrySet()) {
249 |
250 | // oracle返回的columnName为大写
251 | String columnName = entry.getKey().toLowerCase();
252 |
253 | Field field = PersistentUtil.getFieldByColumnName(clazz, columnName);
254 |
255 | if (field != null) {
256 | FieldReflectUtil.setFieldValue(t, field, entry.getValue());
257 | }
258 | }
259 | return t;
260 | }
261 |
262 | }
263 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/utils/NumberFieldReflectUtil.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.utils;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | import org.apache.ibatis.reflection.ReflectionException;
6 | import org.springframework.util.ReflectionUtils;
7 |
8 | import com.svili.portal.crud.common.NumberUtil;
9 |
10 | /**
11 | * 数值字段处理
12 | *
13 | * @author svili
14 | * @date 2017年2月18日
15 | *
16 | */
17 | public class NumberFieldReflectUtil {
18 |
19 | /**
20 | * 设置Date类型字段值
21 | *
22 | * @param target
23 | * the target object from which to get the field
24 | * @param field
25 | * the field to set
26 | * @param number
27 | * java.lang.Number
28 | * @throws Exception
29 | * IllegalArgumentException, IllegalAccess
30 | */
31 | public static void setFieldNumberValue(Object target, Field field, Object value) throws Exception {
32 |
33 | if (!java.lang.Number.class.isAssignableFrom(field.getType())) {
34 | throw new ReflectionException(target.getClass().getName() + "." + field.getName()
35 | + " : field type is not Number, can not convertToNumber");
36 | }
37 |
38 | if (!field.isAccessible()) {
39 | ReflectionUtils.makeAccessible(field);
40 | }
41 |
42 | if(value == null){
43 | field.set(target, null);
44 | return;
45 | }
46 |
47 | if(!(value instanceof Number)){
48 | throw new ReflectionException(value + " : is not Number type value , can not convertToNumber to field "
49 | + target.getClass().getName() + "." + field.getName());
50 | }
51 |
52 | Number number = (Number)value;
53 |
54 | if (field.getType().equals(java.lang.Byte.class)) {
55 | field.set(target, NumberUtil.toByte(number));
56 | return;
57 | }
58 | if (field.getType().equals(java.lang.Double.class)) {
59 | field.set(target, NumberUtil.toDouble(number));
60 | return;
61 | }
62 | if (field.getType().equals(java.lang.Float.class)) {
63 | field.set(target, NumberUtil.toFloat(number));
64 | return;
65 | }
66 | if (field.getType().equals(java.lang.Integer.class)) {
67 | field.set(target, NumberUtil.toInt(number));
68 | return;
69 | }
70 | if (field.getType().equals(java.lang.Long.class)) {
71 | field.set(target, NumberUtil.toLong(number));
72 | return;
73 | }
74 | if (field.getType().equals(java.lang.Short.class)) {
75 | field.set(target, NumberUtil.toShort(number));
76 | return;
77 | }
78 | if (field.getType().equals(java.math.BigDecimal.class)) {
79 | field.set(target, NumberUtil.toBigDecimal(number));
80 | return;
81 | }
82 |
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/crud/utils/SQLColumnFactory.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.crud.utils;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | import com.svili.portal.crud.SQLColumn;
6 | import com.svili.portal.crud.common.PersistentUtil;
7 |
8 | /**
9 | * 工厂方法
10 | * 未完成
11 | * @author svili
12 | * @data 2017年3月30日
13 | *
14 | */
15 | public class SQLColumnFactory {
16 |
17 | public static SQLColumn createSQLColumn(T t, Field field) throws Exception {
18 | String columnName = PersistentUtil.getColumnName(field);
19 | Object columnValue = FieldReflectUtil.getFieldValue(t, field);
20 | Class> fieldType = field.getType();
21 | return createSQLColumn(columnName, columnValue, matchJdbcType(fieldType));
22 | }
23 |
24 | public static SQLColumn createSQLColumn(String columnName, Object columnValue, String jdbcType) {
25 | SQLColumn column = new SQLColumn();
26 | column.setColumnName(columnName);
27 | column.setColumnValue(columnValue);
28 | column.setJdbcType(jdbcType);
29 | return column;
30 | }
31 |
32 | public static String matchJdbcType(Class> fieldType) {
33 | if(fieldType.isEnum()){
34 | //枚举类型存ordinal
35 | return "Integer";
36 | }
37 | if (String.class.equals(fieldType)) {
38 | //CLOB未完成
39 | return "VARCHAR";
40 | }
41 | if (Integer.class.equals(fieldType) || Integer.TYPE.equals(fieldType)) {
42 | return "INTEGER";
43 | }
44 | if (Double.class.equals(fieldType) || Double.TYPE.equals(fieldType)) {
45 | return "DOUBLE";
46 | }
47 | if (Float.class.equals(fieldType) || Float.TYPE.equals(fieldType)) {
48 | return "FLOAT";
49 | }
50 | if (java.util.Date.class.isAssignableFrom(fieldType)) {
51 | return "TIMESTAMP";
52 | }
53 | //CLOB BLOB未完成
54 | return null;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/com/svili/portal/dao/GeneralDao.java:
--------------------------------------------------------------------------------
1 | package com.svili.portal.dao;
2 |
3 | import java.util.List;
4 | import java.util.Map;
5 |
6 | import org.apache.ibatis.annotations.InsertProvider;
7 | import org.springframework.stereotype.Repository;
8 |
9 | import com.svili.portal.crud.GeneralDaoProvider;
10 |
11 | /**
12 | * crud通用dao
13 | * @author svili
14 | * @date 2016年11月11日
15 | *
16 | */
17 | @Repository("generalDao")
18 | public interface GeneralDao {
19 |
20 | Map selectByPrimaryKey(Map param);
21 |
22 | int deleteByPrimaryKey(Map param);
23 |
24 | int deleteByCondition(Map param);
25 |
26 | int insert(Map param);
27 |
28 | @InsertProvider(type=GeneralDaoProvider.class,method="insertSelectiveSql")
29 | int insertSelective(Map param);
30 |
31 | int insertBatch(Map param);
32 |
33 | int updateByPrimaryKey(Map param);
34 |
35 | int updateByConditionSelective(Map param);
36 |
37 | List