Commit e8c4641f authored by hewei's avatar hewei

乐观锁插件实现

parent d38e66e5
...@@ -50,33 +50,27 @@ public class XmlElementGeneratorTools { ...@@ -50,33 +50,27 @@ public class XmlElementGeneratorTools {
* @return the selectKey element * @return the selectKey element
*/ */
public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) { public static Element getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
String identityColumnType = introspectedColumn String identityColumnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName();
.getFullyQualifiedJavaType().getFullyQualifiedName();
XmlElement answer = new XmlElement("selectKey"); XmlElement answer = new XmlElement("selectKey");
answer.addAttribute(new Attribute("resultType", identityColumnType)); answer.addAttribute(new Attribute("resultType", identityColumnType));
answer.addAttribute(new Attribute( answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
"keyProperty", introspectedColumn.getJavaProperty())); answer.addAttribute(new Attribute("order", generatedKey.getMyBatis3Order()));
answer.addAttribute(new Attribute("order",
generatedKey.getMyBatis3Order()));
answer.addElement(new TextElement(generatedKey answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement()));
.getRuntimeSqlStatement()));
return answer; return answer;
} }
public static Element getBaseColumnListElement(IntrospectedTable introspectedTable) { public static Element getBaseColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include"); XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid", answer.addAttribute(new Attribute("refid", introspectedTable.getBaseColumnListId()));
introspectedTable.getBaseColumnListId()));
return answer; return answer;
} }
public static Element getBlobColumnListElement(IntrospectedTable introspectedTable) { public static Element getBlobColumnListElement(IntrospectedTable introspectedTable) {
XmlElement answer = new XmlElement("include"); XmlElement answer = new XmlElement("include");
answer.addAttribute(new Attribute("refid", answer.addAttribute(new Attribute("refid", introspectedTable.getBlobColumnListId()));
introspectedTable.getBlobColumnListId()));
return answer; return answer;
} }
...@@ -85,8 +79,7 @@ public class XmlElementGeneratorTools { ...@@ -85,8 +79,7 @@ public class XmlElementGeneratorTools {
ifElement.addAttribute(new Attribute("test", "_parameter != null")); ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement includeElement = new XmlElement("include"); XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid", includeElement.addAttribute(new Attribute("refid", introspectedTable.getExampleWhereClauseId()));
introspectedTable.getExampleWhereClauseId()));
ifElement.addElement(includeElement); ifElement.addElement(includeElement);
return ifElement; return ifElement;
...@@ -97,8 +90,7 @@ public class XmlElementGeneratorTools { ...@@ -97,8 +90,7 @@ public class XmlElementGeneratorTools {
ifElement.addAttribute(new Attribute("test", "_parameter != null")); ifElement.addAttribute(new Attribute("test", "_parameter != null"));
XmlElement includeElement = new XmlElement("include"); XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new Attribute("refid", includeElement.addAttribute(new Attribute("refid", introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
introspectedTable.getMyBatis3UpdateByExampleWhereClauseId()));
ifElement.addElement(includeElement); ifElement.addElement(includeElement);
return ifElement; return ifElement;
...@@ -270,18 +262,7 @@ public class XmlElementGeneratorTools { ...@@ -270,18 +262,7 @@ public class XmlElementGeneratorTools {
* @return * @return
*/ */
public static List<Element> generateSets(List<IntrospectedColumn> columns, String prefix) { public static List<Element> generateSets(List<IntrospectedColumn> columns, String prefix) {
return generateSets(columns, prefix, false); return generateCommColumns(columns, prefix, false, 3);
}
/**
* 生成sets Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public static List<Element> generateSets(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumns(columns, prefix, bracket, 3);
} }
/** /**
...@@ -290,7 +271,7 @@ public class XmlElementGeneratorTools { ...@@ -290,7 +271,7 @@ public class XmlElementGeneratorTools {
* @return * @return
*/ */
public static XmlElement generateSetsSelective(List<IntrospectedColumn> columns) { public static XmlElement generateSetsSelective(List<IntrospectedColumn> columns) {
return generateSetsSelective(columns, null, false); return generateSetsSelective(columns, null);
} }
/** /**
...@@ -300,18 +281,7 @@ public class XmlElementGeneratorTools { ...@@ -300,18 +281,7 @@ public class XmlElementGeneratorTools {
* @return * @return
*/ */
public static XmlElement generateSetsSelective(List<IntrospectedColumn> columns, String prefix) { public static XmlElement generateSetsSelective(List<IntrospectedColumn> columns, String prefix) {
return generateSetsSelective(columns, prefix, false); return generateCommColumnsSelective(columns, prefix, false, 3);
}
/**
* 生成sets Selective Ele
* @param columns
* @param prefix
* @param bracket
* @return
*/
public static XmlElement generateSetsSelective(List<IntrospectedColumn> columns, String prefix, boolean bracket) {
return generateCommColumnsSelective(columns, prefix, bracket, 3);
} }
/** /**
...@@ -325,6 +295,7 @@ public class XmlElementGeneratorTools { ...@@ -325,6 +295,7 @@ public class XmlElementGeneratorTools {
private static List<Element> generateCommColumns(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) { private static List<Element> generateCommColumns(List<IntrospectedColumn> columns, String prefix, boolean bracket, int type) {
List<Element> list = new ArrayList<>(); List<Element> list = new ArrayList<>();
// 只有upsert插件才会传入 IdentityAndGeneratedAlwaysColumn
if (hasIdentityAndGeneratedAlwaysColumns(columns)) { if (hasIdentityAndGeneratedAlwaysColumns(columns)) {
XmlElement trimEle = generateTrim(bracket); XmlElement trimEle = generateTrim(bracket);
......
...@@ -47,8 +47,28 @@ public class OptimisticLockerPluginTest { ...@@ -47,8 +47,28 @@ public class OptimisticLockerPluginTest {
*/ */
@Test @Test
public void testUpdateWithVersionByExampleSelective() throws Exception { public void testUpdateWithVersionByExampleSelective() throws Exception {
// 测试不带or的更新
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OptimisticLockerPlugin/mybatis-generator.xml"); MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/OptimisticLockerPlugin/mybatis-generator.xml");
tool.generate(new AbstractShellCallback() { tool.generate(() -> DBHelper.createDB("scripts/OptimisticLockerPlugin/init.sql"), new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
criteria.invoke("andIdEqualTo", 1l);
ObjectUtil tb = new ObjectUtil(loader, packagz + ".Tb");
tb.set("incF2", 10L);
tb.set("incF3", 5L);
// sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "updateWithVersionByExample", 1L, tb.getObject(), tbExample.getObject());
Assert.assertEquals(sql, "update tb SET field_1 = 'null' , inc_f2 = 5 WHERE ( id = '1' )");
}
});
tool.generate(() -> DBHelper.createDB("scripts/OptimisticLockerPlugin/init.sql"), new AbstractShellCallback() {
@Override @Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception { public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper"))); ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
......
...@@ -20,7 +20,7 @@ SET FOREIGN_KEY_CHECKS=0; ...@@ -20,7 +20,7 @@ SET FOREIGN_KEY_CHECKS=0;
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `tb`; DROP TABLE IF EXISTS `tb`;
CREATE TABLE `tb` ( CREATE TABLE `tb` (
`id` bigint(20) NOT NULL COMMENT '注释1', `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
`field1` varchar(255) DEFAULT NULL COMMENT '注释2', `field1` varchar(255) DEFAULT NULL COMMENT '注释2',
`inc_f1` bigint(20) NOT NULL DEFAULT '0', `inc_f1` bigint(20) NOT NULL DEFAULT '0',
`inc_f2` bigint(20) DEFAULT '0', `inc_f2` bigint(20) DEFAULT '0',
......
...@@ -53,9 +53,13 @@ ...@@ -53,9 +53,13 @@
</table> </table>
<table tableName="tb_keys"> <table tableName="tb_keys">
<property name="versionColumn" value="inc_f1"/> <property name="versionColumn" value="inc_f1"/>
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table> </table>
<table tableName="tb_blobs"> <table tableName="tb_blobs">
<property name="versionColumn" value="inc_f1"/> <property name="versionColumn" value="inc_f1"/>
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table> </table>
</context> </context>
</generatorConfiguration> </generatorConfiguration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment