package com.kongjs.wp.mybatis;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface CommonMapper<T,ID> {
String $pk = "$pk";
String $entity="$entity";
@DeleteProvider(value = CommonSqlProvider.class)
int deleteByPrimaryKey(@Param($pk) ID id);
@DeleteProvider(value = CommonSqlProvider.class)
int deleteAll();
@InsertProvider(value = CommonSqlProvider.class)
int insert(@Param($entity) T row);
@InsertProvider(value = CommonSqlProvider.class)
int insertSelective(@Param($entity) T row);
@SelectProvider(value = CommonSqlProvider.class)
T selectByPrimaryKey(@Param($pk) ID id);
@SelectProvider(value = CommonSqlProvider.class)
List<T> selectAll();
@UpdateProvider(value = CommonSqlProvider.class)
int updateByPrimaryKeySelective(@Param($entity) T row);
@UpdateProvider(value = CommonSqlProvider.class)
int updateByPrimaryKey(@Param($entity) T row);
}
package com.kongjs.wp.mybatis;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.builder.annotation.ProviderContext;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectorFactory;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Objects;
// mybatis SqlProvider实现
public class CommonSqlProvider implements ProviderMethodResolver {
private static final String $pk = "$pk";
private static final String $entity="$entity";
private static final ObjectFactory objectFactory = new DefaultObjectFactory();
private static final ObjectWrapperFactory objectWrapperFactory = new DefaultObjectWrapperFactory();
private static final ReflectorFactory reflectorFactory = new DefaultReflectorFactory();
public static String toUnderScoreCase(String camelCaseStr) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < camelCaseStr.length(); i++) {
char c = camelCaseStr.charAt(i);
if (Character.isUpperCase(c)) {
if (i > 0) {
builder.append('_');
}
builder.append(Character.toLowerCase(c));
} else {
builder.append(c);
}
}
return builder.toString();
}
/*
* 获取继承接口上泛型
* */
public static Class<?> getSuperMapperTypeArguments(ProviderContext context){
Type[] genericInterfaces = context.getMapperType().getGenericInterfaces();
Type genericInterface = genericInterfaces[0];
Type[] actualTypeArguments = ((ParameterizedType) genericInterface).getActualTypeArguments();
Class<?> entityType = (Class<?>)actualTypeArguments[0];
// Class<?> idTpe = (Class<?>) actualTypeArguments[1];
return entityType;
}
public String deleteByPrimaryKey(Map<String, Object> params, ProviderContext context){
Class<?> entityType = getSuperMapperTypeArguments(context);
String pk = String.valueOf(params.get($pk));
String table = toUnderScoreCase(entityType.getSimpleName());
SQL sql = new SQL();
sql.DELETE_FROM(table);
if (StringUtils.isNumeric(pk)){
sql.WHERE("id="+pk);
}else {
sql.WHERE("id='"+pk+"'");
}
return sql.toString();
}
public String deleteAll(Map<String, Object> params, ProviderContext context){
Class<?> entityType = getSuperMapperTypeArguments(context);
String table = toUnderScoreCase(entityType.getSimpleName());
SQL sql = new SQL();
sql.DELETE_FROM(table);
return sql.toString();
}
public String insert(Map<String, Object> params, ProviderContext context){
Object entity = params.get($entity);
String table = toUnderScoreCase(entity.getClass().getSimpleName());
MetaObject metaObject = MetaObject.forObject(entity, objectFactory, objectWrapperFactory, reflectorFactory);
SQL sql = new SQL();
sql.INSERT_INTO(table);
for (String getterName : metaObject.getGetterNames()) {
String column = toUnderScoreCase(getterName);
String value = String.valueOf(metaObject.getValue(getterName));
if (StringUtils.isNumeric(value)|| Objects.equals(value, "null")){
sql.VALUES(column, value);
}else {
sql.VALUES(column,"'"+value+"'");
}
}
return sql.toString();
}
public String insertSelective(Map<String, Object> params, ProviderContext context){
Object entity = params.get($entity);
String table = toUnderScoreCase(entity.getClass().getSimpleName());
MetaObject metaObject = MetaObject.forObject(entity, objectFactory, objectWrapperFactory, reflectorFactory);
SQL sql = new SQL();
sql.INSERT_INTO(table);
for (String getterName : metaObject.getGetterNames()) {
String column = toUnderScoreCase(getterName);
String value = String.valueOf(metaObject.getValue(getterName));
if (StringUtils.isBlank(value)){
continue;
}
if (StringUtils.isNumeric(value)|| Objects.equals(value, "null")){
sql.VALUES(column, value);
}else {
sql.VALUES(column,"'"+value+"'");
}
}
return sql.toString();
}
public String selectByPrimaryKey(Map<String, Object> params, ProviderContext context){
Class<?> entityType = getSuperMapperTypeArguments(context);
String pk = String.valueOf(params.get($pk));
String table = toUnderScoreCase(entityType.getSimpleName());
MetaClass metaClass = MetaClass.forClass(entityType,reflectorFactory);
SQL sql = new SQL();
sql.FROM(table);
for (String getterName : metaClass.getGetterNames()) {
String column = toUnderScoreCase(getterName);
sql.SELECT(column);
}
if (StringUtils.isNumeric(pk)){
sql.WHERE("id="+pk);
}else {
sql.WHERE("id='"+pk+"'");
}
return sql.toString();
}
public String selectAll(Map<String, Object> params, ProviderContext context){
Class<?> entityType = getSuperMapperTypeArguments(context);
String table = toUnderScoreCase(entityType.getSimpleName());
MetaClass metaClass = MetaClass.forClass(entityType,reflectorFactory);
SQL sql = new SQL();
sql.FROM(table);
for (String getterName : metaClass.getGetterNames()) {
String column = toUnderScoreCase(getterName);
sql.SELECT(column);
}
return sql.toString();
}
public String updateByPrimaryKey(Map<String, Object> params, ProviderContext context){
Object entity = params.get($entity);
String pk = null;
String table = toUnderScoreCase(entity.getClass().getSimpleName());
MetaObject metaObject = MetaObject.forObject(entity, objectFactory, objectWrapperFactory, reflectorFactory);
SQL sql = new SQL();
sql.UPDATE(table);
for (String getterName : metaObject.getGetterNames()) {
String column = toUnderScoreCase(getterName);
String value = String.valueOf(metaObject.getValue(getterName));
if (column.equalsIgnoreCase("id")&&!value.equals("null")){
pk=value;
}
if (StringUtils.isNumeric(value)|| Objects.equals(value, "null")){
sql.SET(column+"="+ value);
}else {
sql.SET(column+"='"+value+"'");
}
}
if (StringUtils.isNumeric(pk)){
sql.WHERE("id="+pk);
}else {
sql.WHERE("id='"+pk+"'");
}
return sql.toString();
}
public String updateByPrimaryKeySelective(Map<String, Object> params, ProviderContext context){
Object entity = params.get($entity);
String pk = null;
String table = toUnderScoreCase(entity.getClass().getSimpleName());
MetaObject metaObject = MetaObject.forObject(entity, objectFactory, objectWrapperFactory, reflectorFactory);
SQL sql = new SQL();
sql.UPDATE(table);
for (String getterName : metaObject.getGetterNames()) {
String column = toUnderScoreCase(getterName);
String value = String.valueOf(metaObject.getValue(getterName));
if (column.equalsIgnoreCase("id")&&!value.equals("null")){
pk=value;
}
if (StringUtils.isBlank(value)){
continue;
}
if (StringUtils.isNumeric(value)|| Objects.equals(value, "null")){
sql.SET(column+"="+ value);
}else {
sql.SET(column+"='"+value+"'");
}
}
if (StringUtils.isNumeric(pk)){
sql.WHERE("id="+pk);
}else {
sql.WHERE("id='"+pk+"'");
}
return sql.toString();
}
}
// 使用
package com.kongjs.wp.mapper;
import com.kongjs.wp.model.Menu;
import com.mybatisflex.core.BaseMapper;
public interface MenuMapper extends BaseMapper<Menu> {
}
mybatis原生实现crud demo
最新推荐文章于 2024-08-20 10:58:52 发布