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