v1.0.0 prepare

This commit is contained in:
开源海哥 2023-03-11 18:51:18 +08:00
parent 64ec296b18
commit 57c0ff2e0e
10 changed files with 210 additions and 19 deletions

View File

@ -108,17 +108,18 @@ public class MybatisFlexBootstrap {
configuration.setLogImpl(logImpl);
}
//init mappers
if (mappers != null) {
mappers.forEach(configuration::addMapper);
}
//init sqlSessionFactory
this.sqlSessionFactory = new FlexSqlSessionFactoryBuilder().build(configuration);
//init dbType
this.dbType = FlexGlobalConfig.getConfig(environmentId).getDbType();
//init mappers
if (mappers != null) {
mappers.forEach(configuration::addMapper);
}
LogFactory.getLog(MybatisFlexBootstrap.class).debug("Mybatis-Flex has started.");
}

View File

@ -239,7 +239,7 @@ public class CommonsDialectImpl implements IDialect {
if (i > 0) {
sql.append(" AND ");
}
sql.append('`').append(primaryKeys[i]).append("` = ?");
sql.append(wrap(primaryKeys[i])).append(" = ?");
}
return sql.toString();
}
@ -380,6 +380,9 @@ public class CommonsDialectImpl implements IDialect {
StringBuilder sql = new StringBuilder();
sql.append("INSERT INTO ").append(wrap(tableInfo.getTableName()));
String[] insertColumns = tableInfo.obtainInsertColumns();
for (int i = 0; i < insertColumns.length; i++) {
insertColumns[i] = wrap(insertColumns[i]);
}
sql.append("(").append(StringUtil.join(", ", insertColumns)).append(")");
sql.append(" VALUES ");

View File

@ -92,6 +92,8 @@ public class FlexConfiguration extends Configuration {
return statementHandler;
}
@Override
public void addMappedStatement(MappedStatement ms) {
//替换 RowMapper.insertRow 的主键生成器
@ -106,8 +108,8 @@ public class FlexConfiguration extends Configuration {
}
//entity select
else if (StringUtil.endsWithAny(ms.getId(), "selectOneById", "selectListByIds"
, "selectListByQuery", "selectCountByQuery")) {
ms = replaceResultHandler(ms);
, "selectListByQuery")) {
ms = replaceResultMap(ms);
}
super.addMappedStatement(ms);
@ -115,9 +117,9 @@ public class FlexConfiguration extends Configuration {
/**
* 替换 entity 查询的 ResultHandler
* 替换 entity 查询的 ResultMap
*/
private MappedStatement replaceResultHandler(MappedStatement ms) {
private MappedStatement replaceResultMap(MappedStatement ms) {
TableInfo tableInfo = getTableInfo(ms);
if (tableInfo == null) {
@ -221,8 +223,8 @@ public class FlexConfiguration extends Configuration {
.timeout(ms.getTimeout())
.statementType(ms.getStatementType())
.keyGenerator(keyGenerator) // 替换主键生成器
.keyProperty(tableInfo.getMappedStatementKeyProperties())
.keyColumn(tableInfo.getMappedStatementKeyColumns())
.keyProperty(tableInfo.getKeyProperties())
.keyColumn(tableInfo.getKeyColumns())
.databaseId(databaseId)
.lang(ms.getLang())
.resultOrdered(ms.isResultOrdered())

View File

@ -228,7 +228,7 @@ public class TableInfo {
IdInfo idInfo = primaryKeyList.get(i);
primaryKeys[i] = idInfo.getColumn();
if (idInfo.getKeyType() != KeyType.Auto || (idInfo.getBefore() != null && idInfo.getBefore())) {
if (idInfo.getKeyType() != KeyType.Auto && (idInfo.getBefore() != null && idInfo.getBefore())) {
insertIdFields.add(idInfo.getColumn());
}
@ -409,7 +409,7 @@ public class TableInfo {
}
public String getMappedStatementKeyProperties() {
public String getKeyProperties() {
StringJoiner joiner = new StringJoiner(",");
for (IdInfo value : primaryKeyList) {
joiner.add(FlexConsts.ENTITY + "." + value.getProperty());
@ -418,10 +418,10 @@ public class TableInfo {
}
public String getMappedStatementKeyColumns() {
public String getKeyColumns() {
StringJoiner joiner = new StringJoiner(",");
for (IdInfo value : primaryKeyList) {
joiner.add(FlexConsts.ENTITY + "." + value.getColumn());
joiner.add(value.getColumn());
}
return joiner.toString();
}

View File

@ -99,7 +99,8 @@ public class TableInfos {
tableInfo.setCamelToUnderline(table.camelToUnderline());
} else {
//默认为类名转驼峰下划线
tableInfo.setTableName(StringUtil.camelToUnderline(entityClass.getSimpleName()));
String tableName = StringUtil.camelToUnderline(entityClass.getSimpleName());
tableInfo.setTableName(tableName);
}
//初始化字段相关

View File

@ -0,0 +1,62 @@
package com.mybatisflex.test;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import com.mybatisflex.core.enums.KeyType;
import java.util.Date;
@Table("tb_account")
public class Account {
@Id(keyType = KeyType.Auto)
private Long id;
private String userName;
private int age;
private Date birthday;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
}

View File

@ -0,0 +1,6 @@
package com.mybatisflex.test;
import com.mybatisflex.core.BaseMapper;
public interface AccountMapper extends BaseMapper<Account> {
}

View File

@ -0,0 +1,116 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.util.UpdateEntity;
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.*;
public class EntityTestStarter {
public static void main(String[] args) {
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.build();
MybatisFlexBootstrap bootstrap = new MybatisFlexBootstrap()
.setDataSource(dataSource)
.setLogImpl(StdOutImpl.class)
.addMapper(AccountMapper.class)
.start();
// //查询 ID 1 的数据
Account account = bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.selectOneById(1));
System.out.println(account);
Account newAccount = new Account();
newAccount.setUserName("lisi");
newAccount.setAge(18);
newAccount.setBirthday(new Date());
bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.insert(newAccount));
//新增后自动回填主键
System.out.println("newAccount.id >>>>>> " + newAccount.getId());
List<Account> newAccountList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Account insertAccount = new Account();
insertAccount.setUserName("new_user_" + i);
insertAccount.setAge(22);
insertAccount.setBirthday(new Date());
newAccountList.add(insertAccount);
}
//批量插入数据
bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.insertBatch(newAccountList));
bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.deleteById(1));
bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.deleteBatchByIds(Arrays.asList(1, 2, 3)));
Map<String, Object> where = new HashMap<>();
where.put("id", 2);
bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.deleteByMap(where));
Account updateAccount1 = UpdateEntity.wrap(Account.class);
updateAccount1.setId(5L);
updateAccount1.setUserName(null);
updateAccount1.setAge(60);
bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.update(updateAccount1, false));
Account updateAccount2 = UpdateEntity.wrap(Account.class);
updateAccount2.setId(6L);
updateAccount2.setAge(40);
bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.update(updateAccount2));
List<Account> allAccounts = bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.selectListByQuery(QueryWrapper.create()));
System.out.println(allAccounts); //count 5
//分页查询 2 每页 3 条数据
Page<Account> accountPage = bootstrap.execute(AccountMapper.class, accountMapper ->
accountMapper.paginate(2, 3, QueryWrapper.create()));
System.out.println(accountPage);
}
}

View File

@ -28,7 +28,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import java.util.*;
public class MybatisFlexStarter {
public class RowTestStarter {
public static void main(String[] args) {
DataSource dataSource = new EmbeddedDatabaseBuilder()

View File

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