mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
v1.0.0 prepare
This commit is contained in:
parent
d9e7b7d43f
commit
64ec296b18
@ -19,6 +19,7 @@ import com.mybatisflex.core.dialect.DbType;
|
||||
import com.mybatisflex.core.dialect.DialectFactory;
|
||||
import com.mybatisflex.core.mybatis.FlexConfiguration;
|
||||
import com.mybatisflex.core.mybatis.FlexSqlSessionFactoryBuilder;
|
||||
import org.apache.ibatis.logging.Log;
|
||||
import org.apache.ibatis.logging.LogFactory;
|
||||
import org.apache.ibatis.mapping.Environment;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
@ -60,6 +61,7 @@ public class MybatisFlexBootstrap {
|
||||
|
||||
private DbType dbType;
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
private Class<? extends Log> logImpl;
|
||||
|
||||
|
||||
private static volatile MybatisFlexBootstrap instance;
|
||||
@ -102,6 +104,10 @@ public class MybatisFlexBootstrap {
|
||||
configuration = new FlexConfiguration(environment);
|
||||
}
|
||||
|
||||
if (logImpl != null) {
|
||||
configuration.setLogImpl(logImpl);
|
||||
}
|
||||
|
||||
//init mappers
|
||||
if (mappers != null) {
|
||||
mappers.forEach(configuration::addMapper);
|
||||
@ -194,4 +200,13 @@ public class MybatisFlexBootstrap {
|
||||
this.sqlSessionFactory = sqlSessionFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Class<? extends Log> getLogImpl() {
|
||||
return logImpl;
|
||||
}
|
||||
|
||||
public MybatisFlexBootstrap setLogImpl(Class<? extends Log> logImpl) {
|
||||
this.logImpl = logImpl;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,8 +15,9 @@
|
||||
*/
|
||||
package com.mybatisflex.core.javassist;
|
||||
|
||||
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public interface ModifyAttrsRecord extends Serializable {
|
||||
@ -26,22 +27,25 @@ public interface ModifyAttrsRecord extends Serializable {
|
||||
* 对于 entity 来说,这里存放的只是 属性的名称,而非字段
|
||||
* 对于 row 来说,存放的则是 字段 名称
|
||||
*/
|
||||
Set<String> modifyAttrs = new LinkedHashSet<>();
|
||||
default Set<String> getModifyAttrs(){
|
||||
ModifyAttrsRecordHandler handler = (ModifyAttrsRecordHandler) ((ProxyObject) this).getHandler();
|
||||
return handler.getModifyAttrs();
|
||||
}
|
||||
|
||||
default void addModifyAttr(String attr) {
|
||||
modifyAttrs.add(attr);
|
||||
getModifyAttrs().add(attr);
|
||||
}
|
||||
|
||||
default void removeModifyAttr(String attr) {
|
||||
modifyAttrs.remove(attr);
|
||||
getModifyAttrs().remove(attr);
|
||||
}
|
||||
|
||||
default Set<String> obtainModifyAttrs() {
|
||||
return new LinkedHashSet<>(modifyAttrs);
|
||||
return getModifyAttrs();
|
||||
}
|
||||
|
||||
default void clearModifyFlag() {
|
||||
modifyAttrs.clear();
|
||||
getModifyAttrs().clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -20,17 +20,26 @@ import com.mybatisflex.core.util.StringUtil;
|
||||
import org.apache.ibatis.javassist.util.proxy.MethodHandler;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class ModifyAttrsRecordHandler implements MethodHandler {
|
||||
|
||||
private Set<String> modifyAttrs = new LinkedHashSet<>();
|
||||
|
||||
public Set<String> getModifyAttrs() {
|
||||
return modifyAttrs;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object invoke(Object self, Method originalMethod, Method proxyMethod, Object[] args) throws Throwable {
|
||||
|
||||
if (originalMethod.getName().startsWith("set")){
|
||||
String property = StringUtil.firstCharToLowerCase(originalMethod.getName().substring(3));
|
||||
((ModifyAttrsRecord) self).addModifyAttr(property);
|
||||
modifyAttrs.add(property);
|
||||
// ((ModifyAttrsRecord) self).addModifyAttr(property);
|
||||
}
|
||||
|
||||
return proxyMethod.invoke(self, args);
|
||||
@ -38,7 +47,6 @@ public class ModifyAttrsRecordHandler implements MethodHandler {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -36,11 +36,11 @@ public class ModifyAttrsRecordProxyFactory {
|
||||
ProxyFactory factory = new ProxyFactory();
|
||||
factory.setSuperclass(target);
|
||||
|
||||
|
||||
Class<?>[] interfaces = Arrays.copyOf(target.getInterfaces(), target.getInterfaces().length + 1);
|
||||
interfaces[interfaces.length - 1] = ModifyAttrsRecord.class;
|
||||
factory.setInterfaces(interfaces);
|
||||
|
||||
|
||||
final Class<?> proxyClass = factory.createClass();
|
||||
|
||||
T proxyObject = null;
|
||||
@ -55,6 +55,8 @@ public class ModifyAttrsRecordProxyFactory {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -24,10 +24,7 @@ import com.mybatisflex.core.row.RowMapper;
|
||||
import com.mybatisflex.core.util.ArrayUtil;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
public class RowSqlProvider {
|
||||
|
||||
@ -83,7 +80,8 @@ public class RowSqlProvider {
|
||||
}
|
||||
|
||||
//让所有 row 的列顺序和值的数量与第条数据保持一致
|
||||
Set<String> modifyAttrs = rows.get(0).obtainModifyAttrs();
|
||||
//这个必须 new 一个 LinkedHashSet,因为 keepModifyAttrs 会清除 row 所有的 modifyAttrs
|
||||
Set<String> modifyAttrs = new LinkedHashSet<>(rows.get(0).obtainModifyAttrs());
|
||||
rows.forEach(row -> row.keepModifyAttrs(modifyAttrs));
|
||||
|
||||
|
||||
@ -146,11 +144,11 @@ public class RowSqlProvider {
|
||||
public static String deleteByQuery(Map params) {
|
||||
String tableName = ProviderUtil.getTableName(params);
|
||||
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
|
||||
queryWrapper.from(tableName);
|
||||
|
||||
Object[] valueArray = CPI.getValueArray(queryWrapper);
|
||||
ProviderUtil.setSqlArgs(params, valueArray);
|
||||
|
||||
queryWrapper.from(tableName);
|
||||
return DialectFactory.getDialect().forDeleteByQuery(queryWrapper);
|
||||
}
|
||||
|
||||
|
||||
@ -23,9 +23,7 @@ import com.mybatisflex.core.util.ArrayUtil;
|
||||
import com.mybatisflex.core.util.SqlUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class Row extends HashMap<String, Object> implements ModifyAttrsRecord {
|
||||
private static final Object[] NULL_ARGS = new Object[0];
|
||||
@ -39,6 +37,14 @@ public class Row extends HashMap<String, Object> implements ModifyAttrsRecord {
|
||||
}
|
||||
|
||||
|
||||
private Set<String> modifyAttrs = new LinkedHashSet<>();
|
||||
|
||||
@Override
|
||||
public Set<String> getModifyAttrs() {
|
||||
return modifyAttrs;
|
||||
}
|
||||
|
||||
|
||||
public static Row ofKey(String primaryKey, Object value) {
|
||||
Row row = new Row();
|
||||
String[] primaryKeyStrings = primaryKey.split(",");
|
||||
@ -211,4 +217,5 @@ public class Row extends HashMap<String, Object> implements ModifyAttrsRecord {
|
||||
return ArrayUtil.concat(obtainModifyValues(), obtainsPrimaryValues());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -21,13 +21,12 @@ import com.mybatisflex.core.querywrapper.QueryWrapper;
|
||||
import com.mybatisflex.core.row.Row;
|
||||
import com.mybatisflex.core.row.RowKey;
|
||||
import com.mybatisflex.core.row.RowMapper;
|
||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class MybatisFlexStarter {
|
||||
|
||||
@ -40,6 +39,7 @@ public class MybatisFlexStarter {
|
||||
|
||||
MybatisFlexBootstrap bootstrap = new MybatisFlexBootstrap()
|
||||
.setDataSource(dataSource)
|
||||
.setLogImpl(StdOutImpl.class)
|
||||
.start();
|
||||
|
||||
|
||||
@ -83,11 +83,46 @@ public class MybatisFlexStarter {
|
||||
rowMapper.insertBatchWithFirstRowColumns("tb_account", newRowList));
|
||||
|
||||
|
||||
//根据主键 ID 删除数据
|
||||
bootstrap.execute(RowMapper.class, rowMapper ->
|
||||
rowMapper.deleteById("tb_account", Row.ofKey(RowKey.ID_AUTO, 1)));
|
||||
|
||||
|
||||
//根据原生 SQL 删除数据
|
||||
bootstrap.execute(RowMapper.class, rowMapper ->
|
||||
rowMapper.deleteBySql("delete from tb_account where id = ? ", 2));
|
||||
|
||||
|
||||
//根据主键 列表 删除数据
|
||||
bootstrap.execute(RowMapper.class, rowMapper ->
|
||||
rowMapper.deleteBatchByIds("tb_account", "id", Arrays.asList(2, 3, 4)));
|
||||
|
||||
|
||||
Map<String, Object> where = new HashMap<>();
|
||||
where.put("id", 2);
|
||||
//根据 map 删除数据
|
||||
bootstrap.execute(RowMapper.class, rowMapper ->
|
||||
rowMapper.deleteByByMap("tb_account", where));
|
||||
|
||||
|
||||
//更新数据
|
||||
Row updateRow = Row.ofKey(RowKey.ID_AUTO, 6)
|
||||
.set("user_name", "newNameTest");
|
||||
bootstrap.execute(RowMapper.class, rowMapper ->
|
||||
rowMapper.updateById("tb_account", updateRow));
|
||||
|
||||
|
||||
//更新数据
|
||||
bootstrap.execute(RowMapper.class, rowMapper ->
|
||||
rowMapper.updateBySql("update tb_account set user_name = ? where id = ?", "李四", 7));
|
||||
|
||||
|
||||
//查询全部数据
|
||||
List<Row> rows = bootstrap.execute(RowMapper.class, rowMapper ->
|
||||
rowMapper.selectAll("tb_account"));
|
||||
|
||||
System.out.println("rows count: " + rows.size()); //9
|
||||
|
||||
System.out.println("rows count: " + rows.size()); // 7
|
||||
System.out.println(rows);
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user