hjg
2024-07-09 4fd62ace17390b0b9cc37c55e88582134ec18c28
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3 import java.lang.reflect.Field;
4
5 import org.apache.commons.lang3.StringUtils;
6
7 public class SQLautogenerateUtils {
8     
9     public static String getinsertSql(Class<?> c){
10         StringBuffer t=new StringBuffer();
11         StringBuffer t1=new StringBuffer();
12         StringBuffer t2=new StringBuffer();
13         t.append("insert into "+StringUtils.lowerCase(c.getSimpleName()));
14         // 获取类中的全部定义字段
15         Field[] fields = c.getDeclaredFields( );
16         for (Field field : fields) {
17             field.setAccessible(true);
18             String fieldName=field.getName();
19             if(!"serialVersionUID".equals(fieldName)){
20                 //第一次放入
21                 if(StringUtils.isEmpty(t1.toString())){
22                     t1.append("("+fieldName);
23                     t2.append("(#{"+fieldName+"}");
24                 }else{
25                     t1.append(","+fieldName);
26                     t2.append(",#{"+fieldName+"}");
27                 }
28             }
29         }
30         return t.append(t1).append(") values ").append(t2).append(")").toString();
31     }
32     
33     public static String getupdateSql(Class<?> c){
34         StringBuffer t=new StringBuffer();
35         t.append("update "+StringUtils.lowerCase(c.getSimpleName())+" set ");
36         // 获取类中的全部定义字段
37         Field[] fields = c.getDeclaredFields( );
38         boolean first=true;
39         for (Field field : fields) {
40             field.setAccessible(true);
41             String fieldName=field.getName();
42             if(!"serialVersionUID".equals(fieldName)){
43                 if(first){
44                     t.append(fieldName+"=").append("#{"+fieldName+"}");
45                     first=false;
46                 }else{
47                     t.append(","+fieldName+"=").append("#{"+fieldName+"}");
48                 }
49             }
50         }
51         return t.append(" where ").toString();
52     }
53     
54     
55 }