mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
v1.0.0 beta2 release
This commit is contained in:
parent
e38273bb9e
commit
e51d0d49bc
@ -380,12 +380,12 @@ public class CommonsDialectImpl implements IDialect {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("INSERT INTO ").append(wrap(tableInfo.getTableName()));
|
||||
String[] insertColumns = tableInfo.obtainInsertColumns();
|
||||
sql.append("(").append(StringUtil.join(",", insertColumns)).append(")");
|
||||
sql.append("(").append(StringUtil.join(", ", insertColumns)).append(")");
|
||||
sql.append(" VALUES ");
|
||||
|
||||
Map<String, String> onInsertColumns = tableInfo.getOnInsertColumns();
|
||||
for (int i = 0; i < entities.size(); i++) {
|
||||
StringJoiner stringJoiner = new StringJoiner("(", ", ", ")");
|
||||
StringJoiner stringJoiner = new StringJoiner(", ", "(", ")");
|
||||
for (String insertColumn : insertColumns) {
|
||||
if (onInsertColumns != null && onInsertColumns.containsKey(insertColumn)) {
|
||||
stringJoiner.add(onInsertColumns.get(insertColumn));
|
||||
@ -440,10 +440,11 @@ public class CommonsDialectImpl implements IDialect {
|
||||
}
|
||||
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append("UPDATE");
|
||||
sql.append("UPDATE ");
|
||||
sql.append(wrap(tableInfo.getTableName()));
|
||||
sql.append("SET ").append(wrap(logicDeleteColumn)).append(" = ").append(FlexConsts.DEL_STATUS_DELETED);
|
||||
sql.append(" SET ").append(wrap(logicDeleteColumn)).append(" = ").append(FlexConsts.DEL_STATUS_DELETED);
|
||||
sql.append(" WHERE ");
|
||||
sql.append("(");
|
||||
|
||||
String[] primaryKeys = tableInfo.getPrimaryKeys();
|
||||
|
||||
@ -472,6 +473,10 @@ public class CommonsDialectImpl implements IDialect {
|
||||
sql.append(wrap(primaryKeys[0])).append(" = ?");
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtil.isNotBlank(logicDeleteColumn)) {
|
||||
sql.append(") AND ").append(wrap(logicDeleteColumn)).append(" = ").append(FlexConsts.DEL_STATUS_NORMAL);
|
||||
}
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
@ -598,6 +603,9 @@ public class CommonsDialectImpl implements IDialect {
|
||||
//乐观锁条件
|
||||
if (StringUtil.isNotBlank(versionColumn)) {
|
||||
Object versionValue = tableInfo.getColumnValue(entity, versionColumn);
|
||||
if (versionValue == null) {
|
||||
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
|
||||
}
|
||||
queryWrapper.and(new StringQueryCondition(wrap(versionColumn) + " = " + versionValue));
|
||||
}
|
||||
|
||||
|
||||
@ -23,7 +23,76 @@ public class Article {
|
||||
@Column(onInsertValue = "now()")
|
||||
private Date created;
|
||||
|
||||
@Column(onUpdateValue = "now()")
|
||||
@Column(onUpdateValue = "now()", onInsertValue = "now()")
|
||||
private Date modified;
|
||||
|
||||
@Column(isLogicDelete = true)
|
||||
private Boolean isDelete;
|
||||
|
||||
@Column(version = true)
|
||||
private Long version;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(Long accountId) {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public Date getModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
public void setModified(Date modified) {
|
||||
this.modified = modified;
|
||||
}
|
||||
|
||||
public Boolean getDelete() {
|
||||
return isDelete;
|
||||
}
|
||||
|
||||
public void setDelete(Boolean delete) {
|
||||
isDelete = delete;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,104 @@
|
||||
package com.mybatisflex.test;
|
||||
|
||||
import com.mybatisflex.core.dialect.CommonsDialectImpl;
|
||||
import com.mybatisflex.core.dialect.IDialect;
|
||||
import com.mybatisflex.core.querywrapper.QueryWrapper;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfos;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.mybatisflex.test.table.Tables.ARTICLE;
|
||||
|
||||
public class ArticleSqlTester {
|
||||
|
||||
|
||||
@Test
|
||||
public void testSelectSql() {
|
||||
QueryWrapper query = new QueryWrapper()
|
||||
.select()
|
||||
.from(ARTICLE);
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
String sql = dialect.forSelectListByQuery(query);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertSql() {
|
||||
Article article = new Article();
|
||||
article.setAccountId(1L);
|
||||
article.setContent("aaa");
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
String sql = dialect.forInsertEntity(tableInfo, article);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertBatchSql() {
|
||||
Article article1 = new Article();
|
||||
article1.setAccountId(1L);
|
||||
article1.setContent("aaa");
|
||||
|
||||
Article article2 = new Article();
|
||||
article2.setAccountId(2L);
|
||||
article2.setContent("bbb");
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
String sql = dialect.forInsertEntityBatch(tableInfo, CollectionUtil.newArrayList(article1, article2));
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDeleteSql() {
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
String sql = dialect.forDeleteEntityById(tableInfo);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDeleteByIdsSql() {
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
String sql = dialect.forDeleteEntityBatchByIds(tableInfo, new Object[]{1, 2, 3});
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUpdateSql() {
|
||||
Article article = new Article();
|
||||
article.setAccountId(1L);
|
||||
article.setContent("aaa");
|
||||
article.setVersion(1L);
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
String sql = dialect.forUpdateEntity(tableInfo, article, true);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUpdateByQuerySql() {
|
||||
Article article = new Article();
|
||||
article.setAccountId(1L);
|
||||
article.setContent("aaa");
|
||||
article.setVersion(1L);
|
||||
|
||||
QueryWrapper queryWrapper = new QueryWrapper()
|
||||
.where(ARTICLE.ID.ge(100));
|
||||
|
||||
IDialect dialect = new CommonsDialectImpl();
|
||||
TableInfo tableInfo = TableInfos.ofEntityClass(Article.class);
|
||||
String sql = dialect.forUpdateEntityByQuery(tableInfo, article, true, queryWrapper);
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user