mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
v1.0.0 prepare
This commit is contained in:
parent
3bb62da834
commit
e5f87b8212
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS `tb_account`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY,
|
||||
`user_name` VARCHAR(100) NOT NULL,
|
||||
`age` Integer,
|
||||
`birthday` DATETIME
|
||||
`id` INTEGER PRIMARY KEY auto_increment,
|
||||
`user_name` VARCHAR(100) NOT NULL,
|
||||
`age` Integer,
|
||||
`birthday` DATETIME
|
||||
);
|
||||
Loading…
x
Reference in New Issue
Block a user