新增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

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
*.ear *.ear
/target/ /target/
target target
com

View File

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

40
pom.xml
View File

@ -13,6 +13,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.4.RELEASE</spring.version> <spring.version>4.2.4.RELEASE</spring.version>
<servlet.version>2.5</servlet.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> </properties>
<dependencies> <dependencies>
@ -112,8 +116,44 @@
<version>${servlet.version}</version> <version>${servlet.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </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> </dependencies>
<build> <build>
<finalName>useful-code</finalName> <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> </build>
</project> </project>

View File

@ -1,11 +1,17 @@
package osc.git.eh3.test; package osc.git.eh3.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.caucho.hessian.client.HessianProxyFactory; import com.caucho.hessian.client.HessianProxyFactory;
@ -119,16 +125,46 @@ public class TestCode {
// System.out.println(jj.optString("pring")); // System.out.println(jj.optString("pring"));
// 根据网卡取本机配置的IP // // 根据网卡取本机配置的IP
InetAddress inet = null; // InetAddress inet = null;
try { // try {
inet = InetAddress.getLocalHost(); // inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) { // } catch (UnknownHostException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
String ipAddress = inet.getHostAddress(); // 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));
} }
@ -147,4 +183,13 @@ public class TestCode {
} }
return time; 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 转换后数据对象 * @return 转换后数据对象
*/ */
public static <T> T convertClass(Object bean, Class<T> clazz) { public static <T> T convertClass(Object bean, Class<T> clazz) {
Map<String, Object> maps = new HashMap<String, Object>(); Map<String, Object> maps = new HashMap<String, Object>();
T dataBean = null; T dataBean = null;
if (null == bean) { if (null == bean) {
@ -34,67 +35,76 @@ public class CommonUtils {
Field[] beanFields = clazz.getDeclaredFields(); Field[] beanFields = clazz.getDeclaredFields();
for (Field field : fields) { for (Field field : fields) {
try { try {
String fieldName = field.getName(); String fieldName = field.getName().toLowerCase();
String strGet = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); String strGet = "get" + fieldName;
Method methodGet = cls.getDeclaredMethod(strGet);
Object object = methodGet.invoke(bean); Method[] methods = cls.getMethods();
for (Method method : methods) {
if (strGet.equalsIgnoreCase(method.getName())) {
Object object = method.invoke(bean);
maps.put(fieldName, object == null ? "" : object); maps.put(fieldName, object == null ? "" : object);
}
}
} catch (Exception e) { } catch (Exception e) {
} }
} }
for (Field field : beanFields) { for (Field field : beanFields) {
field.setAccessible(true); field.setAccessible(true);
String fieldName = field.getName(); String fieldName = field.getName().toLowerCase();
Class<?> fieldType = field.getType(); 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 (fieldValue != null) {
if (String.class.equals(fieldType)) { 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } 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)) { } else if (Date.class.equals(fieldType)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); field.set(dataBean, fieldValue);
field.set(dataBean, sdf.parse(fieldValue));
} }
} }
} }