diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java index aaa06549..9fc07528 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/MybatisFlexBootstrap.java @@ -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."); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/CommonsDialectImpl.java index abad7446..7f6e9945 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/CommonsDialectImpl.java @@ -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 "); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index 9fbae71b..6835b00d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -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()) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index b69b2943..f36a54db 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -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(); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfos.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfos.java index 4f3993dd..8371f2be 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfos.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfos.java @@ -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); } //初始化字段相关 diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java new file mode 100644 index 00000000..fa78d964 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java @@ -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 + + '}'; + } +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountMapper.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountMapper.java new file mode 100644 index 00000000..249d1300 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountMapper.java @@ -0,0 +1,6 @@ +package com.mybatisflex.test; + +import com.mybatisflex.core.BaseMapper; + +public interface AccountMapper extends BaseMapper { +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java new file mode 100644 index 00000000..6e986dc5 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java @@ -0,0 +1,116 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 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 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 allAccounts = bootstrap.execute(AccountMapper.class, accountMapper -> + accountMapper.selectListByQuery(QueryWrapper.create())); + System.out.println(allAccounts); //count 5 + + + //分页查询,第 2 页,每页 3 条数据 + Page accountPage = bootstrap.execute(AccountMapper.class, accountMapper -> + accountMapper.paginate(2, 3, QueryWrapper.create())); + System.out.println(accountPage); + + } +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MybatisFlexStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java similarity index 99% rename from mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MybatisFlexStarter.java rename to mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java index 54d788fc..0613605e 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MybatisFlexStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/RowTestStarter.java @@ -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() diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql index 3e2dacf5..d7e21fac 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/resources/schema.sql @@ -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 ); \ No newline at end of file