新增mybatis-generator

如果是在eclipse 中,选择pom.xml文件,击右键先择Run AS——>Maven Build…
——>在Goals框中输入:mybatis-generator:generate
This commit is contained in:
lixiangrong 2016-03-07 15:40:49 +08:00
parent 18e4dfdaa7
commit 414edce9a4
5 changed files with 135 additions and 40 deletions

13
.gitignore vendored
View File

@ -1,8 +1,9 @@
*.class
# Package Files #
*.jar
*.war
*.class
# Package Files #
*.jar
*.war
*.ear
/target/
target
target
com

View File

@ -1,5 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8

40
pom.xml
View File

@ -13,6 +13,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.4.RELEASE</spring.version>
<servlet.version>2.5</servlet.version>
<mybatis.version>3.2.7</mybatis.version>
<mybatis.spring.version>1.1.1</mybatis.spring.version>
<mybatis.paging.version>0.0.1</mybatis.paging.version>
<mybatis.generator.core.version>1.3.2</mybatis.generator.core.version>
</properties>
<dependencies>
@ -112,8 +116,44 @@
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- mybatis start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.loafer</groupId>
<artifactId>mybatis-paging</artifactId>
<version>${mybatis.paging.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>${mybatis.generator.core.version}</version>
</dependency>
<!-- mybatis end -->
</dependencies>
<build>
<finalName>useful-code</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -1,11 +1,17 @@
package osc.git.eh3.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.caucho.hessian.client.HessianProxyFactory;
@ -119,17 +125,47 @@ public class TestCode {
// System.out.println(jj.optString("pring"));
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
String ipAddress = inet.getHostAddress();
// // 根据网卡取本机配置的IP
// InetAddress inet = null;
// try {
// inet = InetAddress.getLocalHost();
// } catch (UnknownHostException e) {
// e.printStackTrace();
// }
// String ipAddress = inet.getHostAddress();
//
// System.out.println(ipAddress);
System.out.println(ipAddress);
// TestCode test = new TestCode();
// System.out.println(test.dd("ddd"));
// Package pkg = Package.getPackage("osc.git.eh3.test");
// Annotation[] annotations = pkg.getAnnotations();
// for (Annotation annotation : annotations) {
// System.out.println(annotation);
// }
// String[] arrs = new String[]{"111","111","2222"};
// for (String string : Array2Set(arrs)) {
//
// System.out.println(string);
// }
// Class<?> clazz = StatisByHourModel.class;
// Method[] methods = clazz.getMethods();
// for (Method method : methods) {
// System.out.println(method.getName());
// }
Object dd = new Date();
System.out.println(dd instanceof Date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(sdf.format(dd));
}
public static Long parseDate(String s) {
@ -147,4 +183,13 @@ public class TestCode {
}
return time;
}
public <B> B dd(B t){
return t;
}
public static <T extends Object> Set<T> Array2Set(T[] tArray) {
Set<T> tSet = new HashSet<T>(Arrays.asList(tArray));
return tSet;
}
}

View File

@ -22,6 +22,7 @@ public class CommonUtils {
* @return 转换后数据对象
*/
public static <T> T convertClass(Object bean, Class<T> clazz) {
Map<String, Object> maps = new HashMap<String, Object>();
T dataBean = null;
if (null == bean) {
@ -34,67 +35,76 @@ public class CommonUtils {
Field[] beanFields = clazz.getDeclaredFields();
for (Field field : fields) {
try {
String fieldName = field.getName();
String strGet = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
Method methodGet = cls.getDeclaredMethod(strGet);
Object object = methodGet.invoke(bean);
maps.put(fieldName, object == null ? "" : object);
String fieldName = field.getName().toLowerCase();
String strGet = "get" + fieldName;
Method[] methods = cls.getMethods();
for (Method method : methods) {
if (strGet.equalsIgnoreCase(method.getName())) {
Object object = method.invoke(bean);
maps.put(fieldName, object == null ? "" : object);
}
}
} catch (Exception e) {
}
}
for (Field field : beanFields) {
field.setAccessible(true);
String fieldName = field.getName();
String fieldName = field.getName().toLowerCase();
Class<?> fieldType = field.getType();
String fieldValue = (maps.get(fieldName) == null || "".equals(maps.get(fieldName))) ? null : maps.get(fieldName).toString();
Object fieldValue = (maps.get(fieldName) == null || "".equals(maps.get(fieldName))) ? null : maps.get(fieldName);
if (fieldValue != null) {
if (String.class.equals(fieldType)) {
field.set(dataBean, fieldValue);
if (fieldValue instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
field.set(dataBean, sdf.format(fieldValue));
} else {
field.set(dataBean, fieldValue.toString());
}
} else if (byte.class.equals(fieldType)) {
field.setByte(dataBean, Byte.parseByte(fieldValue));
field.setByte(dataBean, Byte.parseByte(fieldValue.toString()));
} else if (Byte.class.equals(fieldType)) {
field.set(dataBean, Byte.valueOf(fieldValue));
field.set(dataBean, Byte.valueOf(fieldValue.toString()));
} else if (boolean.class.equals(fieldType)) {
field.setBoolean(dataBean, Boolean.parseBoolean(fieldValue));
field.setBoolean(dataBean, Boolean.parseBoolean(fieldValue.toString()));
} else if (Boolean.class.equals(fieldType)) {
field.set(dataBean, Boolean.valueOf(fieldValue));
field.set(dataBean, Boolean.valueOf(fieldValue.toString()));
} else if (short.class.equals(fieldType)) {
field.setShort(dataBean, Short.parseShort(fieldValue));
field.setShort(dataBean, Short.parseShort(fieldValue.toString()));
} else if (Short.class.equals(fieldType)) {
field.set(dataBean, Short.valueOf(fieldValue));
field.set(dataBean, Short.valueOf(fieldValue.toString()));
} else if (int.class.equals(fieldType)) {
field.setInt(dataBean, Integer.parseInt(fieldValue));
field.setInt(dataBean, Integer.parseInt(fieldValue.toString()));
} else if (Integer.class.equals(fieldType)) {
field.set(dataBean, Integer.valueOf(fieldValue));
field.set(dataBean, Integer.valueOf(fieldValue.toString()));
} else if (long.class.equals(fieldType)) {
field.setLong(dataBean, Long.parseLong(fieldValue));
field.setLong(dataBean, Long.parseLong(fieldValue.toString()));
} else if (Long.class.equals(fieldType)) {
field.set(dataBean, Long.valueOf(fieldValue));
field.set(dataBean, Long.valueOf(fieldValue.toString()));
} else if (float.class.equals(fieldType)) {
field.setFloat(dataBean, Float.parseFloat(fieldValue));
field.setFloat(dataBean, Float.parseFloat(fieldValue.toString()));
} else if (Float.class.equals(fieldType)) {
field.set(dataBean, Float.valueOf(fieldValue));
field.set(dataBean, Float.valueOf(fieldValue.toString()));
} else if (double.class.equals(fieldType)) {
field.setDouble(dataBean, Double.parseDouble(fieldValue));
field.setDouble(dataBean, Double.parseDouble(fieldValue.toString()));
} else if (Double.class.equals(fieldType)) {
field.set(dataBean, Double.valueOf(fieldValue));
field.set(dataBean, Double.valueOf(fieldValue.toString()));
} else if (Date.class.equals(fieldType)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
field.set(dataBean, sdf.parse(fieldValue));
field.set(dataBean, fieldValue);
}
}
}