top.doudou.common.tool.datebase.jpa.JpaInterceptor Maven / Gradle / Ivy
package top.doudou.common.tool.datebase.jpa;
import org.hibernate.proxy.map.MapProxyFactory;
import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
/**
* @author 傻男人<[email protected]>
* @description jpa拦截器(格式化sql)
* @date 2020-07-27
*/
@Component
@ConditionalOnClass(MapProxyFactory.class)
public class JpaInterceptor implements StatementInspector {
@Override
public String inspect(String sql) {
System.out.println("==> sql sentence " + replace(sql));
return sql;
}
private String replace(String sql){
sql = sql.replaceAll("as [\\w]+", "");
sql = sql.replaceAll("[\\w]+\\d_","");
sql = sql.replaceAll(", ", ",").replaceAll(" ,", ",");
return sql.replaceAll("\\.","");
}
}