Commit 1589709d authored by hewei's avatar hewei

pull#12 逻辑删除插件增强

parent b1e23b3b
......@@ -33,18 +33,17 @@ public class JavaElementGeneratorTools {
/**
* 生成静态常量
*
* @param fieldName 常量名称
* @param javaType 类型
* @param fieldName 常量名称
* @param javaType 类型
* @param initString 初始化字段
* @return
*/
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString){
public static Field generateStaticFinalField(String fieldName, FullyQualifiedJavaType javaType, String initString) {
Field field = new Field(fieldName, javaType);
field.setVisibility(JavaVisibility.PUBLIC);
field.setStatic(true);
field.setFinal(true);
if (initString != null){
if (initString != null) {
field.setInitializationString(initString);
}
return field;
......@@ -52,17 +51,16 @@ public class JavaElementGeneratorTools {
/**
* 生成属性
*
* @param fieldName 常量名称
* @param visibility 可见性
* @param javaType 类型
* @param fieldName 常量名称
* @param visibility 可见性
* @param javaType 类型
* @param initString 初始化字段
* @return
*/
public static Field generateField(String fieldName, JavaVisibility visibility, FullyQualifiedJavaType javaType, String initString){
public static Field generateField(String fieldName, JavaVisibility visibility, FullyQualifiedJavaType javaType, String initString) {
Field field = new Field(fieldName, javaType);
field.setVisibility(visibility);
if (initString != null){
if (initString != null) {
field.setInitializationString(initString);
}
return field;
......@@ -70,19 +68,18 @@ public class JavaElementGeneratorTools {
/**
* 生成方法
*
* @param methodName 方法名
* @param visibility 可见性
* @param visibility 可见性
* @param returnType 返回值类型
* @param parameters 参数列表
* @return
*/
public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, Parameter ... parameters){
public static Method generateMethod(String methodName, JavaVisibility visibility, FullyQualifiedJavaType returnType, Parameter... parameters) {
Method method = new Method(methodName);
method.setVisibility(visibility);
method.setReturnType(returnType);
if (parameters != null){
for (Parameter parameter: parameters) {
if (parameters != null) {
for (Parameter parameter : parameters) {
method.addParameter(parameter);
}
}
......@@ -92,14 +89,13 @@ public class JavaElementGeneratorTools {
/**
* 生成方法实现体
*
* @param method 方法
* @param method 方法
* @param bodyLines 方法实现行
* @return
*/
public static Method generateMethodBody(Method method, String ... bodyLines){
if (bodyLines != null){
for (String bodyLine: bodyLines){
public static Method generateMethodBody(Method method, String... bodyLines) {
if (bodyLines != null) {
for (String bodyLine : bodyLines) {
method.addBodyLine(bodyLine);
}
}
......@@ -108,11 +104,10 @@ public class JavaElementGeneratorTools {
/**
* 生成Filed的Set方法
*
* @param field field
* @return
*/
public static Method generateSetterMethod(Field field){
public static Method generateSetterMethod(Field field) {
Method method = generateMethod(
"set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
JavaVisibility.PUBLIC,
......@@ -124,11 +119,10 @@ public class JavaElementGeneratorTools {
/**
* 生成Filed的Get方法
*
* @param field field
* @return
*/
public static Method generateGetterMethod(Field field){
public static Method generateGetterMethod(Field field) {
Method method = generateMethod(
"get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1),
JavaVisibility.PUBLIC,
......@@ -139,11 +133,10 @@ public class JavaElementGeneratorTools {
/**
* 获取Model没有BLOBs类时的类型
*
* @param introspectedTable
* @return
*/
public static FullyQualifiedJavaType getModelTypeWithoutBLOBs(IntrospectedTable introspectedTable){
public static FullyQualifiedJavaType getModelTypeWithoutBLOBs(IntrospectedTable introspectedTable) {
FullyQualifiedJavaType type;
if (introspectedTable.getRules().generateBaseRecordClass()) {
type = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
......@@ -157,11 +150,10 @@ public class JavaElementGeneratorTools {
/**
* 获取Model有BLOBs类时的类型
*
* @param introspectedTable
* @return
*/
public static FullyQualifiedJavaType getModelTypeWithBLOBs(IntrospectedTable introspectedTable){
public static FullyQualifiedJavaType getModelTypeWithBLOBs(IntrospectedTable introspectedTable) {
FullyQualifiedJavaType type;
if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
type = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType());
......@@ -171,4 +163,28 @@ public class JavaElementGeneratorTools {
}
return type;
}
/**
* 克隆方法
* @param method
*/
public static Method cloneMethod(Method method) {
Method result = new Method();
result.setConstructor(method.isConstructor());
result.setFinal(method.isFinal());
result.setName(method.getName());
result.setNative(method.isNative());
result.setReturnType(method.getReturnType());
result.setSynchronized(method.isSynchronized());
result.setStatic(method.isStatic());
result.setVisibility(method.getVisibility());
for (Parameter parameter : method.getParameters()) {
result.addParameter(parameter);
}
for (String docLine : method.getJavaDocLines()){
result.addJavaDocLine(docLine);
}
result.addBodyLines(method.getBodyLines());
return result;
}
}
......@@ -68,6 +68,12 @@ public class LogicalDeletePluginTest {
tool.generate();
Assert.assertEquals(tool.getWarnings().get(0), "itfsw(逻辑删除插件):tb没有找到您配置的逻辑删除值,请全局或者局部配置logicalDeleteValue和logicalUnDeleteValue值!");
// 4. 保留关键词冲突
tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-keywords.xml");
tool.generate();
Assert.assertEquals(tool.getWarnings().get(0), "itfsw(逻辑删除插件):tb配置的逻辑删除列和插件保留关键字(andLogicalDeleted)冲突!");
}
/**
......@@ -141,7 +147,7 @@ public class LogicalDeletePluginTest {
// 验证sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject());
Assert.assertEquals(sql, "select id, del_flag, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '1' and id = '3' )");
Assert.assertEquals(sql, "select id, del_flag, and_logical_deleted, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '1' and id = '3' )");
// 验证执行
Object result = tbMapper.invoke("selectByExample", tbExample.getObject());
Assert.assertEquals(((List)result).size(), 1);
......@@ -168,11 +174,56 @@ public class LogicalDeletePluginTest {
// 验证sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject());
Assert.assertEquals(sql, "select id, del_flag, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '0' and id = '3' )");
Assert.assertEquals(sql, "select id, del_flag, and_logical_deleted, ts_1, ts_3, ts_4 from tb WHERE ( del_flag = '0' and id = '3' )");
// 验证执行
Object result = tbMapper.invoke("selectByExample", tbExample.getObject());
Assert.assertEquals(((List)result).size(), 0);
}
});
}
/**
* 测试Model andLogicalDeleted 方法
*/
@Test
public void testModelAndLogicalDeletedMethod() throws InterruptedException, SQLException, InvalidConfigurationException, IOException, XMLParserException {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml");
tool.generate(new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception{
ObjectUtil Tb = new ObjectUtil(loader, packagz + ".Tb");
Tb.invoke("andLogicalDeleted", true);
Assert.assertEquals(Tb.get("delFlag"), (short)1);
Tb.invoke("andLogicalDeleted", false);
Assert.assertEquals(Tb.get("delFlag"), (short)0);
}
});
}
/**
* 测试 selectByPrimaryKeyWithLogicalDelete
*/
@Test
public void testSelectByPrimaryKeyWithLogicalDelete() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator-with-customConstName.xml");
tool.generate(new AbstractShellCallback() {
@Override
public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception{
ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));
// 验证sql
String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByPrimaryKeyWithLogicalDelete", 5l, true);
Assert.assertEquals(sql, "select id, del_flag, and_logical_deleted, ts_1, ts_3, ts_4 , ts_2 from tb where id = 5 and del_flag = '1'");
// 验证执行
Object result = tbMapper.invoke("selectByPrimaryKeyWithLogicalDelete", 5l, true);
Assert.assertNull(result);
// 验证执行
result = tbMapper.invoke("selectByPrimaryKeyWithLogicalDelete", 5l, false);
Assert.assertNotNull(result);
}
});
}
}
......@@ -22,6 +22,7 @@ DROP TABLE IF EXISTS `tb`;
CREATE TABLE `tb` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '注释1',
`del_flag` smallint(1) NOT NULL DEFAULT '0',
`and_logical_deleted` smallint(1) NOT NULL DEFAULT '0',
`ts_1` tinyint(1) DEFAULT NULL,
`ts_2` binary(255) DEFAULT NULL,
`ts_3` float DEFAULT NULL,
......@@ -32,6 +33,7 @@ CREATE TABLE `tb` (
-- ----------------------------
-- Records of tb
-- ----------------------------
INSERT INTO `tb` VALUES ('1', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
INSERT INTO `tb` VALUES ('2', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
INSERT INTO `tb` VALUES ('3', '1', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
INSERT INTO `tb` VALUES ('1', '0', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
INSERT INTO `tb` VALUES ('2', '0', '1', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
INSERT INTO `tb` VALUES ('3', '1', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
INSERT INTO `tb` VALUES ('5', '0', '0', null, 0x300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, null, null);
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2017.
~
~ 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.
-->
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="db.properties"/>
<!--导入属性配置 -->
<context id="default" targetRuntime="MyBatis3">
<!-- 插件 -->
<plugin type="com.itfsw.mybatis.generator.plugins.LogicalDeletePlugin">
<property name="logicalDeleteColumn" value="and_logical_deleted"/>
<property name="logicalDeleteValue" value="1"/>
<property name="logicalUnDeleteValue" value="0"/>
</plugin>
<!--jdbc的数据库连接 -->
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}" />
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="" targetProject=""/>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="" targetProject="" />
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table tableName="tb">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
</table>
</context>
</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