v1.0.0 prepare

This commit is contained in:
开源海哥 2023-03-11 14:49:55 +08:00
parent 3bb62da834
commit e5f87b8212
5 changed files with 55 additions and 14 deletions

View File

@ -76,10 +76,6 @@ public class MybatisFlexBootstrap {
}
public <T> MybatisFlexBootstrap addMapper(Class<T> type) {
if (this.mappers == null) {
mappers = new ArrayList<>();
@ -89,8 +85,6 @@ public class MybatisFlexBootstrap {
}
public MybatisFlexBootstrap start() {
if (started.compareAndSet(false, true)) {
if (dataSource == null) {
@ -138,7 +132,7 @@ public class MybatisFlexBootstrap {
private SqlSession openSession() {
return sqlSessionFactory.openSession(configuration.getDefaultExecutorType());
return sqlSessionFactory.openSession(configuration.getDefaultExecutorType(), true);
}

View File

@ -243,11 +243,12 @@ public class RowSqlProvider {
public static String selectListByQuery(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().forSelectListByQuery(queryWrapper);
}
@ -261,6 +262,7 @@ public class RowSqlProvider {
public static String selectCountByQuery(Map params) {
String tableName = ProviderUtil.getTableName(params);
QueryWrapper queryWrapper = ProviderUtil.getQueryWrapper(params);
queryWrapper.from(tableName);
Object[] valueArray = CPI.getValueArray(queryWrapper);
ProviderUtil.setSqlArgs(params, valueArray);

View File

@ -48,8 +48,8 @@ public class Row extends HashMap<String, Object> implements ModifyAttrsRecord {
row.primaryKeys[i] = RowKey.of(primaryKeyStrings[i].trim());
}
if (primaryKeyStrings.length > 0 && !value.getClass().isArray()) {
throw new IllegalArgumentException("the type of value[\"" + value + "\"] must be an array.");
if (primaryKeyStrings.length > 1 && !value.getClass().isArray()) {
throw new IllegalArgumentException("The type of \"" + value + "\" must be an array.");
}
if (primaryKeyStrings.length == 1) {

View File

@ -16,12 +16,17 @@
package com.mybatisflex.test;
import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.paginate.Page;
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.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;
public class MybatisFlexStarter {
@ -37,13 +42,53 @@ public class MybatisFlexStarter {
.setDataSource(dataSource)
.start();
//查询 ID 1 的数据
Row row = bootstrap.execute(RowMapper.class, rowMapper ->
rowMapper.selectOneById("tb_account", "id", 1));
System.out.println(row);
//新增一条数据自增
Row newRow = Row.ofKey(RowKey.ID_AUTO) // id 自增
.set("user_name", "lisi")
.set("age", 22)
.set("birthday", new Date());
bootstrap.execute(RowMapper.class, rowMapper ->
rowMapper.insertRow("tb_account", newRow));
//新增后查看newRow id会自动被赋值
System.out.println(">>>>>>newRow.id: " + newRow.get("id"));
List<Row> newRowList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Row insertRow = Row.ofKey(RowKey.ID_AUTO) // id 自增
.set("user_name", "new_user_"+i)
.set("age", 22)
.set("birthday", new Date());
newRowList.add(insertRow);
}
//批量插入数据
bootstrap.execute(RowMapper.class, rowMapper ->
rowMapper.insertBatchWithFirstRowColumns("tb_account",newRowList));
//查询全部数据
List<Row> rows = bootstrap.execute(RowMapper.class, rowMapper ->
rowMapper.selectAll("tb_account"));
System.out.println("rows count: " + rows.size()); //8
System.out.println(rows);
//分页查询 2 每页 3 条数据
Page<Row> rowPage = bootstrap.execute(RowMapper.class, rowMapper ->
rowMapper.paginate("tb_account", 2, 3, QueryWrapper.create()));
System.out.println(rowPage);
}
}

View File

@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS `tb_account`
(
`id` INTEGER PRIMARY KEY,
`id` INTEGER PRIMARY KEY auto_increment,
`user_name` VARCHAR(100) NOT NULL,
`age` Integer,
`birthday` DATETIME