优化--mybatis增强器
parent
2142b7ffcd
commit
bb4e54bca6
|
@ -0,0 +1,16 @@
|
|||
package com.genersoft.iot.vmp.conf.dao;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 通过注解控制实体中的ID属性
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Id {
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.genersoft.iot.vmp.conf.dao;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 通过注解控制实体中的属性不存数据库
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Invisible {
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.genersoft.iot.vmp.conf.dao;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.apache.ibatis.mapping.SqlSource;
|
||||
import org.apache.ibatis.scripting.LanguageDriver;
|
||||
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* mybatis 注解模式 select 支持传入对象 驱动
|
||||
* SELECT * FROM xxx_user where 1=1 (#{userObject})
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
public class SelectByEntityExtendedLanguageDriver extends XMLLanguageDriver implements LanguageDriver {
|
||||
|
||||
private final Pattern inPattern = Pattern.compile("\\(#\\{(\\w+)\\}\\)");
|
||||
|
||||
@Override
|
||||
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
|
||||
|
||||
Matcher matcher = inPattern.matcher(script);
|
||||
if (matcher.find()) {
|
||||
StringBuffer ss = new StringBuffer();
|
||||
for (Field field : parameterType.getDeclaredFields()) {
|
||||
//如果不是加了忽略注解的字段就去拼接
|
||||
if (!field.isAnnotationPresent(Invisible.class)) {
|
||||
//and type !=''
|
||||
String temp = "<if test=\"__field != null and __field!='' \">and __column=#{__field}</if>";
|
||||
ss.append(temp.replaceAll("__field", field.getName()).replaceAll("__column",
|
||||
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName())));
|
||||
}
|
||||
}
|
||||
script = matcher.replaceAll(ss.toString());
|
||||
script = "<script>" + script + "</script>";
|
||||
}
|
||||
return super.createSqlSource(configuration, script, parameterType);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.genersoft.iot.vmp.conf.dao;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.apache.ibatis.mapping.SqlSource;
|
||||
import org.apache.ibatis.scripting.LanguageDriver;
|
||||
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* mybatis 注解模式 insert 支持传入对象 驱动
|
||||
* INSERT INTO xxx_user (#{userObject})
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
public class SimpleInsertExtendedLanguageDriver extends XMLLanguageDriver implements LanguageDriver {
|
||||
private final Pattern inPattern = Pattern.compile("\\(#\\{(\\w+)\\}\\)");
|
||||
|
||||
@Override
|
||||
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
|
||||
Matcher matcher = inPattern.matcher(script);
|
||||
if (matcher.find()) {
|
||||
|
||||
// 组建 (xx, xx , xx)字段语句
|
||||
StringBuffer ss = new StringBuffer("( <trim suffixOverrides=\",\">");
|
||||
for (Field field : parameterType.getDeclaredFields()) {
|
||||
//如果不是加了忽略注解的字段就去拼接
|
||||
if (field.isAnnotationPresent(Invisible.class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String column = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName());
|
||||
String temp = "<if test=\"__field != null\"> __column,</if>";
|
||||
ss.append(temp.replaceAll("__field", field.getName()).replaceAll("__column", column));
|
||||
}
|
||||
ss.append("</trim>) VALUES ( <trim suffixOverrides=\",\">");
|
||||
|
||||
// 组建 ("1", "xx", "")值语句
|
||||
for (Field field : parameterType.getDeclaredFields()) {
|
||||
if (field.isAnnotationPresent(Invisible.class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String temp = "<if test=\"__field != null\"> #{__field},</if>";
|
||||
ss.append(temp.replaceAll("__field", field.getName()));
|
||||
}
|
||||
|
||||
ss.append("</trim>) ");
|
||||
script = matcher.replaceAll(ss.toString());
|
||||
|
||||
script = "<script>" + script + "</script>";
|
||||
}
|
||||
return super.createSqlSource(configuration, script, parameterType);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.genersoft.iot.vmp.conf.dao;
|
||||
|
||||
import org.apache.ibatis.mapping.SqlSource;
|
||||
import org.apache.ibatis.scripting.LanguageDriver;
|
||||
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* mybatis 注解模式 select 支持传入list 驱动
|
||||
* SELECT * FROM xxx_user WHERE user_id IN (#{userIds})
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
public class SimpleSelectInExtendedLanguageDriver extends XMLLanguageDriver implements LanguageDriver {
|
||||
|
||||
private final Pattern inPattern = Pattern.compile("\\(#\\{(\\w+)\\}\\)");
|
||||
|
||||
@Override
|
||||
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
|
||||
|
||||
Matcher matcher = inPattern.matcher(script);
|
||||
if (matcher.find()) {
|
||||
script = matcher
|
||||
.replaceAll("(<foreach collection=\"$1\" item=\"__item\" separator=\",\" >#{__item}</foreach>)");
|
||||
}
|
||||
|
||||
script = "<script>" + script + "</script>";
|
||||
return super.createSqlSource(configuration, script, parameterType);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.genersoft.iot.vmp.conf.dao;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.apache.ibatis.mapping.SqlSource;
|
||||
import org.apache.ibatis.scripting.LanguageDriver;
|
||||
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* mybatis 注解模式 update 支持传入对象 驱动
|
||||
* UPDATE xxx_user (#{userObject}) WHERE true and xxxx
|
||||
*
|
||||
* @author
|
||||
*/
|
||||
public class SimpleUpdateExtendedLanguageDriver extends XMLLanguageDriver implements LanguageDriver {
|
||||
private final Pattern inPattern = Pattern.compile("\\(#\\{(\\w+)\\}\\)");
|
||||
|
||||
@Override
|
||||
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
|
||||
Matcher matcher = inPattern.matcher(script);
|
||||
if (matcher.find()) {
|
||||
StringBuffer ss = new StringBuffer();
|
||||
ss.append("<set>");
|
||||
|
||||
for (Field field : parameterType.getDeclaredFields()) {
|
||||
if (!field.isAnnotationPresent(Id.class)){
|
||||
String temp = "";
|
||||
if(field.getType()== Date.class){
|
||||
temp = "<if test=\"__field != null \">__column=#{__field},</if>";
|
||||
}else {
|
||||
temp = "<if test=\"__field != null \">__column=#{__field},</if>";
|
||||
}
|
||||
ss.append(temp.replaceAll("__field", field.getName()).replaceAll("__column",
|
||||
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field.getName())));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ss.deleteCharAt(ss.lastIndexOf(","));
|
||||
ss.append("</set>");
|
||||
|
||||
script = matcher.replaceAll(ss.toString());
|
||||
|
||||
script = "<script>" + script + "</script>";
|
||||
}
|
||||
return super.createSqlSource(configuration, script, parameterType);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue