sql生成工具类不加@SqlField、@SqlTable 默认以类名和字段生成,用法 :
@SqlTable("t_table")
@SqlField(defaultVal = "Field",defaultType = SqlType.Int)
import com.act.violationsdatasyn.config.sqlDao.SqlField;
import com.act.violationsdatasyn.config.sqlDao.SqlTable;
import com.act.violationsdatasyn.config.sqlDao.SqlType;
import com.act.violationsdatasyn.vo.Overview;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @Auther: gy
* @Date: 2020/7/29 10:48
* @Description:
*/
public class SqlGenerateUtils {
public static void main(String[] args) {
Overview o=new Overview();
o.setSource("12");
List<Overview> ls=new ArrayList<>();
ls.add(o);
ls.add(o);
String s = generateInset(ls,Overview.class);
System.out.println(s);
}
/**
* 根据对象生成sql
* 对象表名字段自定义 需加@SqlTable、@SqlField
* @Author gy
* @Date 2020/8/11
*/
public static<T> String generateInset(T t) {
StringBuilder sqlSb = new StringBuilder("INSERT INTO ");
Class<?> aClass = t.getClass();
String value = "";
SqlTable sqlTableAnnotation = aClass.getAnnotation(SqlTable.class);
if(sqlTableAnnotation!=null && StringUtils.isNotBlank(sqlTableAnnotation.value())){
value = sqlTableAnnotation.value();
}else{
value = aClass.getName();
}
sqlSb.append(value);
Field[] declaredFields = aClass.getDeclaredFields();
String fieldSql = generateInsetField(declaredFields);
sqlSb.append(fieldSql);
String val = generateInsetValue(t, declaredFields);
sqlSb.append(val);
return sqlSb.toString();
}
/**
* 根据对象生成sql
* 对象表名字段自定义 需加@SqlTable、@SqlField
* @Author gy
* @Date 2020/8/11
*/
public static<T> String generateInset(List<T> ls, Class<T> aClass) {
if(CollectionUtils.isEmpty(ls)) return null;
StringBuilder sqlSb = new StringBuilder("INSERT INTO ");
String value = "";
SqlTable sqlTableAnnotation = aClass.getAnnotation(SqlTable.class);
if(sqlTableAnnotation!=null && StringUtils.isNotBlank(sqlTableAnnotation.value())){
value = sqlTableAnnotation.value();
}else{
value = aClass.getName();
}
sqlSb.append(value);
//生成字段部分------------------------------------------------
Field[] declaredFields = aClass.getDeclaredFields();
String fieldSql = generateInsetField(declaredFields);
sqlSb.append(fieldSql);
//生成value部分----------------------------------------------
String val = generateInsetValue(ls,declaredFields);
sqlSb.append(val);
System.out.println(sqlSb.toString());
return sqlSb.toString();
}
private static <T> String generateInsetField(Field[] declaredFields) {
StringBuilder sqlSb = new StringBuilder();
sqlSb.append(" (");
for(Field f:declaredFields){
try {
f.setAccessible(true);
SqlField SqlFieldAnnotation = f.getAnnotation(SqlField.class);
if(SqlFieldAnnotation!=null && StringUtils.isNotBlank(SqlFieldAnnotation.value())){
sqlSb.append(SqlFieldAnnotation.value() + ",");
}else{
String name = f.getName();
sqlSb.append(name + ",");
}
}catch (Exception e){
e.printStackTrace();
return null;
}
}
sqlSb.deleteCharAt(sqlSb.length()-1);
sqlSb.append(") VALUES ");
return sqlSb.toString();
}
private static <T> String generateInsetValue(T t, Field[] declaredFields) {
StringBuilder sqlSb=new StringBuilder();
sqlSb.append("(");
for(Field f:declaredFields){
try {
SqlField SqlFieldAnnotation = f.getAnnotation(SqlField.class);
Object o = f.get(t);
String sqlObj = "";
if(o == null && SqlFieldAnnotation!=null){
if(SqlFieldAnnotation.defaultType()== SqlType.String){
sqlObj = "'"+SqlFieldAnnotation.defaultVal()+"'";
}else if(SqlFieldAnnotation.defaultType()== SqlType.Int){
sqlObj = SqlFieldAnnotation.defaultVal();
}
}else{
sqlObj = getSqlObj(f, o);
}
sqlSb.append(sqlObj + ",");
}catch (Exception e){
e.printStackTrace();
return null;
}
}
sqlSb.deleteCharAt(sqlSb.length()-1);
sqlSb.append(")");
return sqlSb.toString();
}
private static <T> String generateInsetValue(List<T> ls, Field[] declaredFields) {
StringBuilder sqlSb=new StringBuilder();
for(T t:ls){
String s = generateInsetValue(t, declaredFields);
sqlSb.append(s+",");
}
sqlSb.deleteCharAt(sqlSb.length()-1);
return sqlSb.toString();
}
private static <T> String getSqlObj(Field f, T o) {
Class<?> type = f.getType();
String st = null;
if(type.getName().equals(String.class.getName())){
if(o != null){
st = "'" + o.toString() + "'";
}
}else if(type.getName().equals(Long.class.getName()) || type.getName().equals(Integer.class.getName())){
if(o != null){
st = "'" + o.toString() + "'";
}
}else if(type.getName().equals(Date.class.getName())){
if(o != null){
st = "'" + TimeUtil.formatTime((Date) o, TimeUtil.DATE_FORMAT_HOUR_MINUTE_SECOND) + "'";
}
}
return st;
}
}
注解
import java.lang.annotation.*;
/**
* @Auther: gy
* @Date: 2020/8/11 09:22
* @Description:
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SqlField {
String value() default "";
String defaultVal() default "";
SqlType defaultType() default SqlType.String;
}
import java.lang.annotation.*;
/**
* @Auther: gy
* @Date: 2020/8/11 09:15
* @Description:
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SqlTable {
String value() default "";
}
/**
* @Auther: gy
* @Date: 2020/8/11 15:21
* @Description:
*/
public enum SqlType {
String,Int;
}