generatedJavaFiles = getGeneratedJavaFiles();
30 | new GenerateBase().generateBase(projectName, generatedJavaFiles, true);
31 | }
32 |
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/mybatis/Dalgen.java:
--------------------------------------------------------------------------------
1 | package com.autotest.mybatis;
2 |
3 | import com.autotest.utils.StringUtils;
4 | import org.mybatis.generator.config.*;
5 | import org.mybatis.generator.internal.DefaultShellCallback;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
9 | import org.springframework.core.io.ClassPathResource;
10 |
11 | import java.util.*;
12 |
13 | /**
14 | * mybatis代码生成工具
15 | *
16 | * Created by huairen on 2017/8/2.
17 | */
18 | public class Dalgen {
19 |
20 | private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
21 |
22 | public static void main(String[] args) {
23 | Dalgen dal = new Dalgen();
24 | dal.gen("rap");
25 | }
26 |
27 | public void gen(String projectName) {
28 | if (StringUtils.isBlank(projectName)) {
29 | return;
30 | }
31 | logger.info("开始执行mybatis代码生成工具");
32 | List warnings = new ArrayList<>();
33 | /*覆盖原生成的代码*/
34 | boolean overwrite = true;
35 | try {
36 | YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
37 | yaml.setResources(new ClassPathResource("application.yml"));
38 | Properties prop = yaml.getObject();
39 | Context context = new Context(ModelType.FLAT);
40 | if (StringUtils.isBlank(prop.getProperty("ds." + projectName + ".url"))) {
41 | logger.info("ds." + projectName + ".url" + "未找到");
42 | return;
43 | }
44 | JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
45 | jdbcConnectionConfiguration.setDriverClass(prop.getProperty("ds." + projectName + ".driverClassName"));
46 | jdbcConnectionConfiguration.setConnectionURL(prop.getProperty("ds." + projectName + ".url"));
47 | jdbcConnectionConfiguration.setUserId(prop.getProperty("ds." + projectName + ".username"));
48 | jdbcConnectionConfiguration.setPassword(prop.getProperty("ds." + projectName + ".password"));
49 | //生成模型的位置
50 | JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
51 | String tarProject = Dalgen.class.getClassLoader().getResource(".").getFile()
52 | .replace("target/test-classes", "src/main/java")
53 | .replace("target/classes", "src/main/java");
54 | String name = tarProject.replace("apitest", "core").replace("webtest", "core");
55 | javaModelGeneratorConfiguration.setTargetProject(name);
56 | javaModelGeneratorConfiguration.setTargetPackage("dal.model." + projectName);
57 | javaModelGeneratorConfiguration.addProperty("enableSubPackages", "true");
58 | javaModelGeneratorConfiguration.addProperty("trimStrings", "true");
59 | //生成映射文件的位置
60 | String resourceName = Dalgen.class.getClassLoader().getResource(".").getFile()
61 | .replace("target/test-classes", "src/main/resources")
62 | .replace("target/classes", "src/main/resources");
63 | String nameResource = resourceName.replace("apitest", "core").replace("webtest", "core");
64 | SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
65 | sqlMapGeneratorConfiguration.setTargetProject(nameResource);
66 | sqlMapGeneratorConfiguration.setTargetPackage("dal.mapping." + projectName);
67 | sqlMapGeneratorConfiguration.addProperty("enableSubPackages", "true");
68 | //生成DAO的位置
69 | JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
70 | javaClientGeneratorConfiguration.setTargetProject(name);
71 | javaClientGeneratorConfiguration.setTargetPackage("dal.dao." + projectName);
72 | javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
73 | javaClientGeneratorConfiguration.addProperty("enableSubPackages", "true");
74 |
75 | PluginConfiguration pluginConfiguration = new PluginConfiguration();
76 | pluginConfiguration.setConfigurationType("com.autotest.mybatis.ToStringPlugin");
77 | PluginConfiguration pluginConfiguration1 = new PluginConfiguration();
78 | pluginConfiguration1.setConfigurationType("org.mybatis.generator.plugins.EqualsHashCodePlugin");
79 | PluginConfiguration pluginConfiguration2 = new PluginConfiguration();
80 | pluginConfiguration2.setConfigurationType("org.mybatis.generator.plugins.SerializablePlugin");
81 | PluginConfiguration pluginConfiguration3 = new PluginConfiguration();
82 | pluginConfiguration3.setConfigurationType("com.autotest.mybatis.RenameExampleClassPlugin");
83 | PluginConfiguration pluginConfiguration4 = new PluginConfiguration();
84 | pluginConfiguration4.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
85 | pluginConfiguration4.addProperty("mappers", "com.autotest.annotation.MyMapper");
86 |
87 | context.addPluginConfiguration(pluginConfiguration);
88 | context.addPluginConfiguration(pluginConfiguration1);
89 | context.addPluginConfiguration(pluginConfiguration2);
90 | context.addPluginConfiguration(pluginConfiguration3);
91 | context.addPluginConfiguration(pluginConfiguration4);
92 |
93 | context.setId(projectName);
94 | context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
95 | context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
96 | context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
97 | context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
98 | String table = prop.getProperty("ds." + projectName + ".tables");
99 | if (StringUtils.isNotBlank(table)) {
100 | String[] tables = table.split(",");
101 | GeneratedKey generatedKey = new GeneratedKey("id", "MYSQL", true, "");
102 | Set tabs = new HashSet(Arrays.asList(tables));
103 | for (String str : tabs) {
104 | if (StringUtils.isBlank(str)) {
105 | continue;
106 | }
107 | TableConfiguration tableConfiguration = new TableConfiguration(context);
108 | tableConfiguration.setTableName(str.trim());
109 | tableConfiguration.setGeneratedKey(generatedKey);
110 | String column = prop.getProperty("ds." + projectName + ".columnOverride");
111 | addColumnOverride(tableConfiguration, column);
112 | tableConfiguration.setInsertStatementEnabled(true);
113 | context.addTableConfiguration(tableConfiguration);
114 | }
115 | } else {
116 | TableConfiguration tableConfiguration = new TableConfiguration(context);
117 | tableConfiguration.setTableName("%");
118 | String column = prop.getProperty("ds." + projectName + ".columnOverride");
119 | addColumnOverride(tableConfiguration, column);
120 | GeneratedKey generatedKey = new GeneratedKey("id", "MYSQL", true, "");
121 | tableConfiguration.setGeneratedKey(generatedKey);
122 | context.addTableConfiguration(tableConfiguration);
123 | }
124 |
125 | context.addProperty("beginningDelimiter", "`");
126 | context.addProperty("endingDelimiter", "`");
127 | context.addProperty("javaFileEncoding", "UTF-8");
128 | context.setTargetRuntime("MyBatis3");
129 | JavaTypeResolverConfiguration javaTypeResolverConfiguration = new JavaTypeResolverConfiguration();
130 | javaTypeResolverConfiguration.addProperty("forceBigDecimals", "false");
131 | context.setJavaTypeResolverConfiguration(javaTypeResolverConfiguration);
132 |
133 | Configuration config = new Configuration();
134 | config.addContext(context);
135 | config.addClasspathEntry(this.getClass().getClassLoader()
136 | .getResource("lib/mysql-connector-java-5.1.30.jar").getFile());
137 | DefaultShellCallback callback = new DefaultShellCallback(overwrite);
138 | AutoTestMyBatisGenerator myBatisGenerator = new AutoTestMyBatisGenerator(config, callback, warnings);
139 | myBatisGenerator.generate(null, projectName);
140 | if (warnings.size() != 0) {
141 | logger.info("请注意下面的警告");
142 | }
143 | for (String warning : warnings) {
144 | logger.info(warning);
145 | }
146 | logger.info("代码生成完成");
147 | } catch (Exception e) {
148 | e.printStackTrace();
149 | }
150 | }
151 |
152 | public void addColumnOverride(TableConfiguration tableConfiguration, String column) {
153 | if (StringUtils.isNotBlank(column)) {
154 | String[] columns = column.split(",");
155 | Set cols = new HashSet(Arrays.asList(columns));
156 | for (String col : cols) {
157 | if (StringUtils.isBlank(col)) {
158 | continue;
159 | }
160 | ColumnOverride columnOverride = new ColumnOverride(col.trim());
161 | columnOverride.setJdbcType("VARCHAR");
162 | tableConfiguration.addColumnOverride(columnOverride);
163 | }
164 | }
165 | }
166 |
167 | }
168 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/mybatis/DefaultCommentGenerator.java:
--------------------------------------------------------------------------------
1 | package com.autotest.mybatis;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 | import java.util.Properties;
6 |
7 | import org.mybatis.generator.api.CommentGenerator;
8 | import org.mybatis.generator.api.IntrospectedColumn;
9 | import org.mybatis.generator.api.IntrospectedTable;
10 | import org.mybatis.generator.api.dom.java.*;
11 | import org.mybatis.generator.api.dom.xml.TextElement;
12 | import org.mybatis.generator.api.dom.xml.XmlElement;
13 | import org.mybatis.generator.internal.util.StringUtility;
14 |
15 | /**
16 | * Created by huairen on 2017/8/2.
17 | */
18 | public class DefaultCommentGenerator implements CommentGenerator {
19 | private Properties properties = new Properties();
20 | private boolean suppressDate = false;
21 | private boolean suppressAllComments = false;
22 | private boolean addRemarkComments = false;
23 | private SimpleDateFormat dateFormat;
24 |
25 | public DefaultCommentGenerator() {
26 | }
27 |
28 | public void addJavaFileComment(CompilationUnit compilationUnit) {
29 | compilationUnit.addFileCommentLine("/**");
30 | compilationUnit.addFileCommentLine(" * This class was generated by MyBatis Generator, do not modify.");
31 | compilationUnit.addFileCommentLine(" *");
32 | compilationUnit.addFileCommentLine(" * @Filename " +
33 | compilationUnit.getType().getShortName() + ".java");
34 | compilationUnit.addFileCommentLine(" *");
35 | compilationUnit.addFileCommentLine(" * @Author huairen");
36 | compilationUnit.addFileCommentLine(" *");
37 | compilationUnit.addFileCommentLine(" */");
38 |
39 | }
40 |
41 | public void addComment(XmlElement xmlElement) {
42 | if (!this.suppressAllComments) {
43 | xmlElement.addElement(new TextElement(""));
50 | }
51 | }
52 |
53 | public void addRootComment(XmlElement rootElement) {
54 | // rootElement.addElement(new TextElement(""));
63 | rootElement.addElement(new TextElement(""));
64 | return;
65 |
66 | }
67 |
68 | public void addConfigurationProperties(Properties properties) {
69 | this.properties.putAll(properties);
70 | this.suppressDate = StringUtility.isTrue(properties.getProperty("suppressDate"));
71 | this.suppressAllComments = StringUtility.isTrue(properties.getProperty("suppressAllComments"));
72 | this.addRemarkComments = StringUtility.isTrue(properties.getProperty("addRemarkComments"));
73 | String dateFormatString = properties.getProperty("dateFormat");
74 | if (StringUtility.stringHasValue(dateFormatString)) {
75 | this.dateFormat = new SimpleDateFormat(dateFormatString);
76 | }
77 |
78 | }
79 |
80 | protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
81 | javaElement.addJavaDocLine(" *");
82 | StringBuilder sb = new StringBuilder();
83 | sb.append(" * ");
84 | sb.append("@mbg.generated");
85 | if (markAsDoNotDelete) {
86 | sb.append(" do_not_delete_during_merge");
87 | }
88 | // String s = this.getDateString();
89 | // if(s != null) {
90 | // sb.append(' ');
91 | // sb.append(s);
92 | // }
93 | javaElement.addJavaDocLine(sb.toString());
94 | }
95 |
96 | protected String getDateString() {
97 | return this.suppressDate ? null : (this.dateFormat != null ? this.dateFormat.format(new Date()) : (new Date()).toString());
98 | }
99 |
100 | public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
101 | if (!this.suppressAllComments) {
102 | StringBuilder sb = new StringBuilder();
103 | innerClass.addJavaDocLine("/**");
104 | innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
105 | sb.append(" * This class corresponds to the database table ");
106 | sb.append(introspectedTable.getFullyQualifiedTable());
107 | innerClass.addJavaDocLine(sb.toString());
108 | this.addJavadocTag(innerClass, false);
109 | innerClass.addJavaDocLine(" */");
110 | }
111 | }
112 |
113 | public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
114 | if (!this.suppressAllComments && this.addRemarkComments) {
115 | StringBuilder sb = new StringBuilder();
116 | topLevelClass.addJavaDocLine("/**");
117 | String remarks = introspectedTable.getRemarks();
118 | if (this.addRemarkComments && StringUtility.stringHasValue(remarks)) {
119 | topLevelClass.addJavaDocLine(" * Database Table Remarks:");
120 | String[] remarkLines = remarks.split(System.getProperty("line.separator"));
121 | String[] var6 = remarkLines;
122 | int var7 = remarkLines.length;
123 |
124 | for (int var8 = 0; var8 < var7; ++var8) {
125 | String remarkLine = var6[var8];
126 | topLevelClass.addJavaDocLine(" * " + remarkLine);
127 | }
128 | }
129 |
130 | topLevelClass.addJavaDocLine(" *");
131 | topLevelClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
132 | sb.append(" * This class corresponds to the database table ");
133 | sb.append(introspectedTable.getFullyQualifiedTable());
134 | topLevelClass.addJavaDocLine(sb.toString());
135 | this.addJavadocTag(topLevelClass, true);
136 | topLevelClass.addJavaDocLine(" */");
137 | }
138 | }
139 |
140 | public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
141 | if (!this.suppressAllComments) {
142 | StringBuilder sb = new StringBuilder();
143 | innerEnum.addJavaDocLine("/**");
144 | innerEnum.addJavaDocLine(" * This enum was generated by MyBatis Generator.");
145 | sb.append(" * This enum corresponds to the database table ");
146 | sb.append(introspectedTable.getFullyQualifiedTable());
147 | innerEnum.addJavaDocLine(sb.toString());
148 | this.addJavadocTag(innerEnum, false);
149 | innerEnum.addJavaDocLine(" */");
150 | }
151 | }
152 |
153 | public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
154 | if (!this.suppressAllComments) {
155 | field.addJavaDocLine("/**");
156 | String remarks = introspectedColumn.getRemarks();
157 | if (this.addRemarkComments && StringUtility.stringHasValue(remarks)) {
158 | field.addJavaDocLine(" * Database Column Remarks:");
159 | String[] remarkLines = remarks.split(System.getProperty("line.separator"));
160 | String[] var6 = remarkLines;
161 | int var7 = remarkLines.length;
162 |
163 | for (int var8 = 0; var8 < var7; ++var8) {
164 | String remarkLine = var6[var8];
165 | field.addJavaDocLine(" * " + remarkLine);
166 | }
167 | }
168 |
169 | field.addJavaDocLine(" *");
170 | field.addJavaDocLine(" * This field was generated by MyBatis Generator.");
171 | StringBuilder sb = new StringBuilder();
172 | sb.append(" * This field corresponds to the database column ");
173 | sb.append(introspectedTable.getFullyQualifiedTable());
174 | sb.append('.');
175 | sb.append(introspectedColumn.getActualColumnName());
176 | field.addJavaDocLine(sb.toString());
177 | this.addJavadocTag(field, false);
178 | field.addJavaDocLine(" */");
179 | }
180 | }
181 |
182 | public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
183 | if (!this.suppressAllComments) {
184 | StringBuilder sb = new StringBuilder();
185 | field.addJavaDocLine("/**");
186 | field.addJavaDocLine(" * This field was generated by MyBatis Generator.");
187 | sb.append(" * This field corresponds to the database table ");
188 | sb.append(introspectedTable.getFullyQualifiedTable());
189 | field.addJavaDocLine(sb.toString());
190 | this.addJavadocTag(field, false);
191 | field.addJavaDocLine(" */");
192 | }
193 | }
194 |
195 | public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
196 | if (!this.suppressAllComments) {
197 | StringBuilder sb = new StringBuilder();
198 | method.addJavaDocLine("/**");
199 | method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
200 | sb.append(" * This method corresponds to the database table ");
201 | sb.append(introspectedTable.getFullyQualifiedTable());
202 | method.addJavaDocLine(sb.toString());
203 | this.addJavadocTag(method, false);
204 | method.addJavaDocLine(" */");
205 | }
206 | }
207 |
208 | public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
209 | if (!this.suppressAllComments) {
210 | StringBuilder sb = new StringBuilder();
211 | method.addJavaDocLine("/**");
212 | method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
213 | sb.append(" * This method returns the value of the database column ");
214 | sb.append(introspectedTable.getFullyQualifiedTable());
215 | sb.append('.');
216 | sb.append(introspectedColumn.getActualColumnName());
217 | method.addJavaDocLine(sb.toString());
218 | method.addJavaDocLine(" *");
219 | sb.setLength(0);
220 | sb.append(" * @return the value of ");
221 | sb.append(introspectedTable.getFullyQualifiedTable());
222 | sb.append('.');
223 | sb.append(introspectedColumn.getActualColumnName());
224 | method.addJavaDocLine(sb.toString());
225 | this.addJavadocTag(method, false);
226 | method.addJavaDocLine(" */");
227 | }
228 | }
229 |
230 | public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
231 | if (!this.suppressAllComments) {
232 | StringBuilder sb = new StringBuilder();
233 | method.addJavaDocLine("/**");
234 | method.addJavaDocLine(" * This method was generated by MyBatis Generator.");
235 | sb.append(" * This method sets the value of the database column ");
236 | sb.append(introspectedTable.getFullyQualifiedTable());
237 | sb.append('.');
238 | sb.append(introspectedColumn.getActualColumnName());
239 | method.addJavaDocLine(sb.toString());
240 | method.addJavaDocLine(" *");
241 | Parameter parm = (Parameter) method.getParameters().get(0);
242 | sb.setLength(0);
243 | sb.append(" * @param ");
244 | sb.append(parm.getName());
245 | sb.append(" the value for ");
246 | sb.append(introspectedTable.getFullyQualifiedTable());
247 | sb.append('.');
248 | sb.append(introspectedColumn.getActualColumnName());
249 | method.addJavaDocLine(sb.toString());
250 | this.addJavadocTag(method, false);
251 | method.addJavaDocLine(" */");
252 | }
253 | }
254 |
255 | public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
256 | if (!this.suppressAllComments) {
257 | StringBuilder sb = new StringBuilder();
258 | innerClass.addJavaDocLine("/**");
259 | innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
260 | sb.append(" * This class corresponds to the database table ");
261 | sb.append(introspectedTable.getFullyQualifiedTable());
262 | innerClass.addJavaDocLine(sb.toString());
263 | this.addJavadocTag(innerClass, markAsDoNotDelete);
264 | innerClass.addJavaDocLine(" */");
265 | }
266 | }
267 | }
268 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/mybatis/MapperConfigPlugin.java:
--------------------------------------------------------------------------------
1 | package com.autotest.mybatis;
2 |
3 |
4 | import java.util.ArrayList;
5 | import java.util.Iterator;
6 | import java.util.List;
7 | import org.mybatis.generator.api.GeneratedXmlFile;
8 | import org.mybatis.generator.api.IntrospectedTable;
9 | import org.mybatis.generator.api.PluginAdapter;
10 | import org.mybatis.generator.api.dom.xml.Attribute;
11 | import org.mybatis.generator.api.dom.xml.Document;
12 | import org.mybatis.generator.api.dom.xml.TextElement;
13 | import org.mybatis.generator.api.dom.xml.XmlElement;
14 | import org.mybatis.generator.internal.util.StringUtility;
15 | import org.mybatis.generator.internal.util.messages.Messages;
16 |
17 | /**
18 | * Created by huairen on 2017/8/2.
19 | */
20 | public class MapperConfigPlugin extends PluginAdapter {
21 | private List mapperFiles = new ArrayList();
22 |
23 | public MapperConfigPlugin() {
24 | }
25 |
26 | @Override
27 | public boolean validate(List warnings) {
28 | boolean valid = true;
29 | if (!StringUtility.stringHasValue(this.properties.getProperty("targetProject"))) {
30 | warnings.add(Messages.getString("ValidationError.18", "MapperConfigPlugin", "targetProject"));
31 | valid = false;
32 | }
33 |
34 | if (!StringUtility.stringHasValue(this.properties.getProperty("targetPackage"))) {
35 | warnings.add(Messages.getString("ValidationError.18", "MapperConfigPlugin", "targetPackage"));
36 | valid = false;
37 | }
38 |
39 | return valid;
40 | }
41 |
42 | @Override
43 | public List contextGenerateAdditionalXmlFiles() {
44 | Document document = new Document("-//mybatis.org//DTD Config 3.0//EN", "http://mybatis.org/dtd/mybatis-3-config.dtd");
45 | XmlElement root = new XmlElement("configuration");
46 | document.setRootElement(root);
47 | root.addElement(new TextElement(""));
50 | XmlElement mappers = new XmlElement("mappers");
51 | root.addElement(mappers);
52 | Iterator var6 = this.mapperFiles.iterator();
53 |
54 | while (var6.hasNext()) {
55 | String mapperFile = (String) var6.next();
56 | XmlElement mapper = new XmlElement("mapper");
57 | mapper.addAttribute(new Attribute("resource", mapperFile));
58 | mappers.addElement(mapper);
59 | }
60 |
61 | GeneratedXmlFile gxf = new GeneratedXmlFile(document, this.properties.getProperty("fileName", "MapperConfig.xml"), this.properties.getProperty("targetPackage"), this.properties.getProperty("targetProject"), false, this.context.getXmlFormatter());
62 | List answer = new ArrayList(1);
63 | answer.add(gxf);
64 | return answer;
65 | }
66 |
67 | @Override
68 | public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
69 | StringBuilder sb = new StringBuilder();
70 | sb.append(sqlMap.getTargetPackage());
71 | sb.append('.');
72 | String temp = sb.toString();
73 | sb.setLength(0);
74 | sb.append(temp.replace('.', '/'));
75 | sb.append(sqlMap.getFileName());
76 | this.mapperFiles.add(sb.toString());
77 | return true;
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/mybatis/MultiBeanNameGenerator.java:
--------------------------------------------------------------------------------
1 | package com.autotest.mybatis;
2 |
3 | import org.springframework.beans.factory.config.BeanDefinition;
4 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
5 | import org.springframework.beans.factory.support.BeanNameGenerator;
6 |
7 | /**
8 | * Created by huairen on 2017/9/6.
9 | */
10 | public class MultiBeanNameGenerator implements BeanNameGenerator {
11 |
12 | @Override
13 | public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
14 | return definition.getBeanClassName();
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/mybatis/RenameExampleClassPlugin.java:
--------------------------------------------------------------------------------
1 | package com.autotest.mybatis;
2 |
3 | import org.mybatis.generator.api.IntrospectedTable;
4 | import org.mybatis.generator.api.PluginAdapter;
5 | import org.mybatis.generator.internal.util.StringUtility;
6 | import org.mybatis.generator.internal.util.messages.Messages;
7 |
8 | import java.util.List;
9 | import java.util.regex.Matcher;
10 | import java.util.regex.Pattern;
11 |
12 | /**
13 | * Created by huairen on 2017/8/2.
14 | */
15 | public class RenameExampleClassPlugin extends PluginAdapter {
16 | private String searchString;
17 | private String replaceString;
18 | private Pattern pattern;
19 | private boolean valid;
20 |
21 |
22 | public RenameExampleClassPlugin() {
23 | }
24 |
25 | @Override
26 | public boolean validate(List warnings) {
27 | this.searchString = this.properties.getProperty("searchString");
28 | this.replaceString = this.properties.getProperty("replaceString");
29 | valid = StringUtility.stringHasValue(this.searchString) && StringUtility.stringHasValue(this.replaceString);
30 | if(valid) {
31 | this.pattern = Pattern.compile(this.searchString);
32 | } else {
33 | // if(!StringUtility.stringHasValue(this.searchString)) {
34 | // warnings.add(Messages.getString("ValidationError.18", "RenameExampleClassPlugin", "searchString"));
35 | // }
36 | //
37 | // if(!StringUtility.stringHasValue(this.replaceString)) {
38 | // warnings.add(Messages.getString("ValidationError.18", "RenameExampleClassPlugin", "replaceString"));
39 | // }
40 | }
41 |
42 | return true;
43 | }
44 | @Override
45 | public void initialized(IntrospectedTable introspectedTable) {
46 | if(valid) {
47 | String oldType = introspectedTable.getExampleType();
48 | Matcher matcher = this.pattern.matcher(oldType);
49 | oldType = matcher.replaceAll(this.replaceString);
50 | introspectedTable.setExampleType(oldType);
51 | }else {
52 | String oldType = introspectedTable.getBaseRecordType();
53 | String newType = oldType + "DO";
54 | introspectedTable.setBaseRecordType(newType);
55 | introspectedTable.setExampleType(newType + "Example");
56 | String oldMapper = introspectedTable.getMyBatis3JavaMapperType();
57 | String newMapper = oldMapper.replaceAll("Mapper$", "DAO");
58 | introspectedTable.setMyBatis3JavaMapperType(newMapper);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/mybatis/ToStringPlugin.java:
--------------------------------------------------------------------------------
1 | package com.autotest.mybatis;
2 |
3 | import org.mybatis.generator.api.IntrospectedTable;
4 | import org.mybatis.generator.api.PluginAdapter;
5 | import org.mybatis.generator.api.dom.java.Field;
6 | import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
7 | import org.mybatis.generator.api.dom.java.JavaVisibility;
8 | import org.mybatis.generator.api.dom.java.Method;
9 | import org.mybatis.generator.api.dom.java.TopLevelClass;
10 | import org.mybatis.generator.internal.util.StringUtility;
11 |
12 | import java.util.Iterator;
13 | import java.util.List;
14 | import java.util.Properties;
15 |
16 | /**
17 | * Created by huairen on 2017/8/2.
18 | */
19 | public class ToStringPlugin extends PluginAdapter {
20 | private boolean useToStringFromRoot;
21 |
22 | public ToStringPlugin() {
23 | }
24 |
25 | @Override
26 | public void setProperties(Properties properties) {
27 | super.setProperties(properties);
28 | this.useToStringFromRoot = StringUtility.isTrue(properties.getProperty("useToStringFromRoot"));
29 | }
30 |
31 | @Override
32 | public boolean validate(List warnings) {
33 | return true;
34 | }
35 |
36 | @Override
37 | public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
38 | this.generateToString(introspectedTable, topLevelClass);
39 | return true;
40 | }
41 |
42 | @Override
43 | public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
44 | this.generateToString(introspectedTable, topLevelClass);
45 | return true;
46 | }
47 |
48 | @Override
49 | public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
50 | this.generateToString(introspectedTable, topLevelClass);
51 | return true;
52 | }
53 |
54 | private void generateToString(IntrospectedTable introspectedTable, TopLevelClass topLevelClass) {
55 | Method method = new Method();
56 | method.setVisibility(JavaVisibility.PUBLIC);
57 | method.setReturnType(FullyQualifiedJavaType.getStringInstance());
58 | method.setName("toString");
59 | if(introspectedTable.isJava5Targeted()) {
60 | method.addAnnotation("@Override");
61 | }
62 |
63 | this.context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
64 | method.addBodyLine("StringBuilder sb = new StringBuilder();");
65 | method.addBodyLine("sb.append(getClass().getSimpleName());");
66 | method.addBodyLine("sb.append(\" [\");");
67 | StringBuilder sb = new StringBuilder();
68 | Iterator var5 = topLevelClass.getFields().iterator();
69 |
70 | while(var5.hasNext()) {
71 | Field field = (Field)var5.next();
72 | String property = field.getName();
73 | sb.setLength(0);
74 | sb.append("sb.append(\"").append(", ").append(property).append("=\")").append(".append(").append(property).append(");");
75 | method.addBodyLine(sb.toString());
76 | }
77 |
78 | method.addBodyLine("sb.append(\"]\");");
79 |
80 | method.addBodyLine("return sb.toString();");
81 | topLevelClass.addMethod(method);
82 | }
83 | }
84 |
85 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/utils/HttpClient.java:
--------------------------------------------------------------------------------
1 | package com.autotest.utils;
2 |
3 | import org.apache.http.client.methods.CloseableHttpResponse;
4 | import org.apache.http.client.methods.HttpGet;
5 | import org.apache.http.client.methods.HttpPost;
6 | import org.apache.http.entity.StringEntity;
7 | import org.apache.http.impl.client.CloseableHttpClient;
8 | import org.apache.http.impl.client.HttpClients;
9 | import org.apache.http.util.EntityUtils;
10 |
11 | import java.io.IOException;
12 |
13 | /**
14 | * Created by huairen on 2017/8/21.
15 | */
16 | public class HttpClient {
17 |
18 | /**
19 | * 发送get请求,返回内容字符串
20 | */
21 | public static String doGet(String url) {
22 | String result = null;
23 | HttpGet httpGet = new HttpGet(url);
24 | CloseableHttpClient httpclient = HttpClients.createDefault();
25 | try {
26 | CloseableHttpResponse response = httpclient.execute(httpGet);
27 | if (response.getStatusLine().getStatusCode() == 200) {
28 | result = EntityUtils.toString(response.getEntity(), "UTF-8");
29 | }
30 | response.close();
31 | } catch (IOException e) {
32 | e.printStackTrace();
33 | }
34 | return result;
35 | }
36 | /**
37 | * 发送get请求,返回response
38 | */
39 | public static CloseableHttpResponse httpGet(String url) {
40 | HttpGet httpGet = new HttpGet(url);
41 | CloseableHttpClient httpclient = HttpClients.createDefault();
42 | CloseableHttpResponse response = null;
43 | try {
44 | response = httpclient.execute(httpGet);
45 | response.close();
46 | } catch (IOException e) {
47 | e.printStackTrace();
48 | }
49 | return response;
50 | }
51 |
52 | public static CloseableHttpResponse httpPost(String data,String url) {
53 | HttpPost httpPost = new HttpPost(url);
54 | httpPost.setEntity(new StringEntity(data,"UTF-8"));
55 | CloseableHttpClient httpclient = HttpClients.createDefault();
56 | CloseableHttpResponse response = null;
57 | try {
58 | response = httpclient.execute(httpPost);
59 | response.close();
60 | } catch (IOException e) {
61 | e.printStackTrace();
62 | }
63 | return response;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/utils/ProcessObject.java:
--------------------------------------------------------------------------------
1 | package com.autotest.utils;
2 |
3 | import java.math.BigDecimal;
4 | import java.util.ArrayList;
5 | import java.util.Date;
6 | import java.util.List;
7 |
8 | import org.apache.log4j.Logger;
9 |
10 | import javassist.Modifier;
11 |
12 |
13 | public class ProcessObject {
14 | private static final Logger logger = Logger.getLogger(ProcessObject.class);
15 |
16 | public static Object processing(Class cl, String con) {
17 | Object o = null;
18 | String fieldType = cl.getName();
19 | try {
20 | if (String.class.getName().equals(fieldType)) {
21 | return con;
22 | } else if (Integer.class.getName().equals(fieldType) || "int".equals(fieldType)) {
23 | if (StringUtils.isBlank(con)) {
24 | con = "0";
25 | }
26 | o = Integer.parseInt(con);
27 | return o;
28 | } else if (Short.class.getName().equals(fieldType) || "short".equals(fieldType)) {
29 | if (StringUtils.isBlank(con)) {
30 | con = "0";
31 | }
32 | o = Short.parseShort(con);
33 | return o;
34 | } else if (Character.class.getName().equals(fieldType) || "char".equals(fieldType)) {
35 | if (StringUtils.isBlank(con)) {
36 | con = "0";
37 | }
38 | o = con.charAt(0);
39 | return o;
40 | } else if (Byte.class.getName().equals(fieldType) || "byte".equals(fieldType)) {
41 | if (StringUtils.isBlank(con)) {
42 | con = "0";
43 | }
44 | o = con.getBytes()[0];
45 | return o;
46 | } else if (Long.class.getName().equals(fieldType) || "long".equals(fieldType)) {
47 | if (StringUtils.isBlank(con)) {
48 | con = "0";
49 | }
50 | o = Long.parseLong(con);
51 | return o;
52 | } else if (Double.class.getName().equals(fieldType) || "double".equals(fieldType)) {
53 | if (StringUtils.isBlank(con)) {
54 | con = "0";
55 | }
56 | o = Double.parseDouble(con);
57 | return o;
58 | } else if (Boolean.class.getName().equals(fieldType) || "boolean".equals(fieldType)) {
59 | o = Boolean.parseBoolean(con);
60 | return o;
61 | } else if (Float.class.getName().equals(fieldType) || "float".equals(fieldType)) {
62 | if (StringUtils.isBlank(con)) {
63 | con = "0";
64 | }
65 | o = Float.parseFloat(con);
66 | return o;
67 | } else if ((cl.getModifiers() & Modifier.ENUM) != 0) {
68 | if (StringUtils.isNotBlank(con)) {
69 | o = Enum.valueOf(cl, con);
70 | }
71 | return o;
72 | } else if (ArrayList.class.getName().equals(fieldType)) {
73 | String[] strs = con.split("#");
74 | List ls = new ArrayList();
75 | for (String str : strs) {
76 | ls.add(str);
77 | }
78 | o = ls;
79 | return o;
80 | } else if (Date.class.getName().equals(fieldType)) {
81 | if (StringUtils.isBlank(con)) {
82 | return null;
83 | }
84 | return new Class[0];
85 | } else if (BigDecimal.class.getName().equals(fieldType)) {
86 | if (StringUtils.isBlank(con)) {
87 | return null;
88 | }
89 | o = new BigDecimal(con);
90 | return o;
91 | } else {
92 | o = StringUtils.isBlank(con) ? null : con;
93 | }
94 | } catch (RuntimeException e) {
95 | logger.error("参数类型:" + fieldType + " 不能转换,参数值为:" + con);
96 | throw e;
97 | }
98 |
99 | return o;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/utils/PropertyUtil.java:
--------------------------------------------------------------------------------
1 | package com.autotest.utils;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import java.io.*;
7 | import java.util.Properties;
8 | /**
9 | * @author huairen
10 | * Created on 18/1/25.
11 | */
12 | public class PropertyUtil {
13 | private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
14 | private static Properties props;
15 | static{
16 | loadProps();
17 | }
18 |
19 | synchronized static private void loadProps(){
20 | logger.info("开始加载properties文件内容.......");
21 | props = new Properties();
22 | InputStream in = null;
23 | try {
24 | in = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");
25 | props.load(in);
26 | } catch (FileNotFoundException e) {
27 | logger.error("jdbc.properties文件未找到");
28 | } catch (IOException e) {
29 | logger.error("出现IOException");
30 | } finally {
31 | try {
32 | if(null != in) {
33 | in.close();
34 | }
35 | } catch (IOException e) {
36 | logger.error("jdbc.properties文件流关闭出现异常");
37 | }
38 | }
39 | logger.info("加载properties文件内容完成...........");
40 | }
41 |
42 | public static String getProperty(String key){
43 | if(null == props) {
44 | loadProps();
45 | }
46 | return props.getProperty(key);
47 | }
48 |
49 | public static String getProperty(String key, String defaultValue) {
50 | if(null == props) {
51 | loadProps();
52 | }
53 | return props.getProperty(key, defaultValue);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/com/autotest/utils/StringUtils.java:
--------------------------------------------------------------------------------
1 | package com.autotest.utils;
2 |
3 | /**
4 | * Created by yu on 17/11/15.
5 | */
6 | public class StringUtils {
7 |
8 | public static boolean isBlank(String str) {
9 | return str == null || str.trim().isEmpty();
10 | }
11 |
12 | public static boolean isNotBlank(String str) {
13 | return !isBlank(str);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/dal/dao/school/StudentDAO.java:
--------------------------------------------------------------------------------
1 | package dal.dao.school;
2 |
3 | import com.autotest.annotation.MyMapper;
4 | import dal.model.school.StudentDO;
5 | import dal.model.school.StudentDOExample;
6 | import java.util.List;
7 | import org.apache.ibatis.annotations.Param;
8 |
9 | public interface StudentDAO extends MyMapper {
10 | long countByExample(StudentDOExample example);
11 |
12 | int deleteByExample(StudentDOExample example);
13 |
14 | List selectByExample(StudentDOExample example);
15 |
16 | int updateByExampleSelective(@Param("record") StudentDO record, @Param("example") StudentDOExample example);
17 |
18 | int updateByExample(@Param("record") StudentDO record, @Param("example") StudentDOExample example);
19 | }
--------------------------------------------------------------------------------
/src/main/java/dal/model/school/StudentDO.java:
--------------------------------------------------------------------------------
1 | package dal.model.school;
2 |
3 | import java.io.Serializable;
4 | import javax.persistence.*;
5 |
6 | @Table(name = "student")
7 | public class StudentDO implements Serializable {
8 | /**
9 | * id
10 | */
11 | @Id
12 | @GeneratedValue(strategy = GenerationType.IDENTITY)
13 | private Long id;
14 |
15 | /**
16 | * 名称
17 | */
18 | private String name;
19 |
20 | /**
21 | * 年龄
22 | */
23 | private Integer age;
24 |
25 | private static final long serialVersionUID = 1L;
26 |
27 | /**
28 | * 获取id
29 | *
30 | * @return id - id
31 | */
32 | public Long getId() {
33 | return id;
34 | }
35 |
36 | /**
37 | * 设置id
38 | *
39 | * @param id id
40 | */
41 | public void setId(Long id) {
42 | this.id = id;
43 | }
44 |
45 | /**
46 | * 获取名称
47 | *
48 | * @return name - 名称
49 | */
50 | public String getName() {
51 | return name;
52 | }
53 |
54 | /**
55 | * 设置名称
56 | *
57 | * @param name 名称
58 | */
59 | public void setName(String name) {
60 | this.name = name == null ? null : name.trim();
61 | }
62 |
63 | /**
64 | * 获取年龄
65 | *
66 | * @return age - 年龄
67 | */
68 | public Integer getAge() {
69 | return age;
70 | }
71 |
72 | /**
73 | * 设置年龄
74 | *
75 | * @param age 年龄
76 | */
77 | public void setAge(Integer age) {
78 | this.age = age;
79 | }
80 |
81 | @Override
82 | public String toString() {
83 | StringBuilder sb = new StringBuilder();
84 | sb.append(getClass().getSimpleName());
85 | sb.append(" [");
86 | sb.append(", id=").append(id);
87 | sb.append(", name=").append(name);
88 | sb.append(", age=").append(age);
89 | sb.append("]");
90 | return sb.toString();
91 | }
92 |
93 | @Override
94 | public boolean equals(Object that) {
95 | if (this == that) {
96 | return true;
97 | }
98 | if (that == null) {
99 | return false;
100 | }
101 | if (getClass() != that.getClass()) {
102 | return false;
103 | }
104 | StudentDO other = (StudentDO) that;
105 | return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
106 | && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
107 | && (this.getAge() == null ? other.getAge() == null : this.getAge().equals(other.getAge()));
108 | }
109 |
110 | @Override
111 | public int hashCode() {
112 | final int prime = 31;
113 | int result = 1;
114 | result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
115 | result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
116 | result = prime * result + ((getAge() == null) ? 0 : getAge().hashCode());
117 | return result;
118 | }
119 | }
--------------------------------------------------------------------------------
/src/main/java/dal/model/school/StudentDOExample.java:
--------------------------------------------------------------------------------
1 | package dal.model.school;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class StudentDOExample {
7 | protected String orderByClause;
8 |
9 | protected boolean distinct;
10 |
11 | protected List oredCriteria;
12 |
13 | public StudentDOExample() {
14 | oredCriteria = new ArrayList();
15 | }
16 |
17 | public void setOrderByClause(String orderByClause) {
18 | this.orderByClause = orderByClause;
19 | }
20 |
21 | public String getOrderByClause() {
22 | return orderByClause;
23 | }
24 |
25 | public void setDistinct(boolean distinct) {
26 | this.distinct = distinct;
27 | }
28 |
29 | public boolean isDistinct() {
30 | return distinct;
31 | }
32 |
33 | public List getOredCriteria() {
34 | return oredCriteria;
35 | }
36 |
37 | public void or(Criteria criteria) {
38 | oredCriteria.add(criteria);
39 | }
40 |
41 | public Criteria or() {
42 | Criteria criteria = createCriteriaInternal();
43 | oredCriteria.add(criteria);
44 | return criteria;
45 | }
46 |
47 | public Criteria createCriteria() {
48 | Criteria criteria = createCriteriaInternal();
49 | if (oredCriteria.size() == 0) {
50 | oredCriteria.add(criteria);
51 | }
52 | return criteria;
53 | }
54 |
55 | protected Criteria createCriteriaInternal() {
56 | Criteria criteria = new Criteria();
57 | return criteria;
58 | }
59 |
60 | public void clear() {
61 | oredCriteria.clear();
62 | orderByClause = null;
63 | distinct = false;
64 | }
65 |
66 | protected abstract static class GeneratedCriteria {
67 | protected List criteria;
68 |
69 | protected GeneratedCriteria() {
70 | super();
71 | criteria = new ArrayList();
72 | }
73 |
74 | public boolean isValid() {
75 | return criteria.size() > 0;
76 | }
77 |
78 | public List getAllCriteria() {
79 | return criteria;
80 | }
81 |
82 | public List getCriteria() {
83 | return criteria;
84 | }
85 |
86 | protected void addCriterion(String condition) {
87 | if (condition == null) {
88 | throw new RuntimeException("Value for condition cannot be null");
89 | }
90 | criteria.add(new Criterion(condition));
91 | }
92 |
93 | protected void addCriterion(String condition, Object value, String property) {
94 | if (value == null) {
95 | throw new RuntimeException("Value for " + property + " cannot be null");
96 | }
97 | criteria.add(new Criterion(condition, value));
98 | }
99 |
100 | protected void addCriterion(String condition, Object value1, Object value2, String property) {
101 | if (value1 == null || value2 == null) {
102 | throw new RuntimeException("Between values for " + property + " cannot be null");
103 | }
104 | criteria.add(new Criterion(condition, value1, value2));
105 | }
106 |
107 | public Criteria andIdIsNull() {
108 | addCriterion("id is null");
109 | return (Criteria) this;
110 | }
111 |
112 | public Criteria andIdIsNotNull() {
113 | addCriterion("id is not null");
114 | return (Criteria) this;
115 | }
116 |
117 | public Criteria andIdEqualTo(Long value) {
118 | addCriterion("id =", value, "id");
119 | return (Criteria) this;
120 | }
121 |
122 | public Criteria andIdNotEqualTo(Long value) {
123 | addCriterion("id <>", value, "id");
124 | return (Criteria) this;
125 | }
126 |
127 | public Criteria andIdGreaterThan(Long value) {
128 | addCriterion("id >", value, "id");
129 | return (Criteria) this;
130 | }
131 |
132 | public Criteria andIdGreaterThanOrEqualTo(Long value) {
133 | addCriterion("id >=", value, "id");
134 | return (Criteria) this;
135 | }
136 |
137 | public Criteria andIdLessThan(Long value) {
138 | addCriterion("id <", value, "id");
139 | return (Criteria) this;
140 | }
141 |
142 | public Criteria andIdLessThanOrEqualTo(Long value) {
143 | addCriterion("id <=", value, "id");
144 | return (Criteria) this;
145 | }
146 |
147 | public Criteria andIdIn(List values) {
148 | addCriterion("id in", values, "id");
149 | return (Criteria) this;
150 | }
151 |
152 | public Criteria andIdNotIn(List values) {
153 | addCriterion("id not in", values, "id");
154 | return (Criteria) this;
155 | }
156 |
157 | public Criteria andIdBetween(Long value1, Long value2) {
158 | addCriterion("id between", value1, value2, "id");
159 | return (Criteria) this;
160 | }
161 |
162 | public Criteria andIdNotBetween(Long value1, Long value2) {
163 | addCriterion("id not between", value1, value2, "id");
164 | return (Criteria) this;
165 | }
166 |
167 | public Criteria andNameIsNull() {
168 | addCriterion("name is null");
169 | return (Criteria) this;
170 | }
171 |
172 | public Criteria andNameIsNotNull() {
173 | addCriterion("name is not null");
174 | return (Criteria) this;
175 | }
176 |
177 | public Criteria andNameEqualTo(String value) {
178 | addCriterion("name =", value, "name");
179 | return (Criteria) this;
180 | }
181 |
182 | public Criteria andNameNotEqualTo(String value) {
183 | addCriterion("name <>", value, "name");
184 | return (Criteria) this;
185 | }
186 |
187 | public Criteria andNameGreaterThan(String value) {
188 | addCriterion("name >", value, "name");
189 | return (Criteria) this;
190 | }
191 |
192 | public Criteria andNameGreaterThanOrEqualTo(String value) {
193 | addCriterion("name >=", value, "name");
194 | return (Criteria) this;
195 | }
196 |
197 | public Criteria andNameLessThan(String value) {
198 | addCriterion("name <", value, "name");
199 | return (Criteria) this;
200 | }
201 |
202 | public Criteria andNameLessThanOrEqualTo(String value) {
203 | addCriterion("name <=", value, "name");
204 | return (Criteria) this;
205 | }
206 |
207 | public Criteria andNameLike(String value) {
208 | addCriterion("name like", value, "name");
209 | return (Criteria) this;
210 | }
211 |
212 | public Criteria andNameNotLike(String value) {
213 | addCriterion("name not like", value, "name");
214 | return (Criteria) this;
215 | }
216 |
217 | public Criteria andNameIn(List values) {
218 | addCriterion("name in", values, "name");
219 | return (Criteria) this;
220 | }
221 |
222 | public Criteria andNameNotIn(List values) {
223 | addCriterion("name not in", values, "name");
224 | return (Criteria) this;
225 | }
226 |
227 | public Criteria andNameBetween(String value1, String value2) {
228 | addCriterion("name between", value1, value2, "name");
229 | return (Criteria) this;
230 | }
231 |
232 | public Criteria andNameNotBetween(String value1, String value2) {
233 | addCriterion("name not between", value1, value2, "name");
234 | return (Criteria) this;
235 | }
236 |
237 | public Criteria andAgeIsNull() {
238 | addCriterion("age is null");
239 | return (Criteria) this;
240 | }
241 |
242 | public Criteria andAgeIsNotNull() {
243 | addCriterion("age is not null");
244 | return (Criteria) this;
245 | }
246 |
247 | public Criteria andAgeEqualTo(Integer value) {
248 | addCriterion("age =", value, "age");
249 | return (Criteria) this;
250 | }
251 |
252 | public Criteria andAgeNotEqualTo(Integer value) {
253 | addCriterion("age <>", value, "age");
254 | return (Criteria) this;
255 | }
256 |
257 | public Criteria andAgeGreaterThan(Integer value) {
258 | addCriterion("age >", value, "age");
259 | return (Criteria) this;
260 | }
261 |
262 | public Criteria andAgeGreaterThanOrEqualTo(Integer value) {
263 | addCriterion("age >=", value, "age");
264 | return (Criteria) this;
265 | }
266 |
267 | public Criteria andAgeLessThan(Integer value) {
268 | addCriterion("age <", value, "age");
269 | return (Criteria) this;
270 | }
271 |
272 | public Criteria andAgeLessThanOrEqualTo(Integer value) {
273 | addCriterion("age <=", value, "age");
274 | return (Criteria) this;
275 | }
276 |
277 | public Criteria andAgeIn(List values) {
278 | addCriterion("age in", values, "age");
279 | return (Criteria) this;
280 | }
281 |
282 | public Criteria andAgeNotIn(List values) {
283 | addCriterion("age not in", values, "age");
284 | return (Criteria) this;
285 | }
286 |
287 | public Criteria andAgeBetween(Integer value1, Integer value2) {
288 | addCriterion("age between", value1, value2, "age");
289 | return (Criteria) this;
290 | }
291 |
292 | public Criteria andAgeNotBetween(Integer value1, Integer value2) {
293 | addCriterion("age not between", value1, value2, "age");
294 | return (Criteria) this;
295 | }
296 | }
297 |
298 | public static class Criteria extends GeneratedCriteria {
299 |
300 | protected Criteria() {
301 | super();
302 | }
303 | }
304 |
305 | public static class Criterion {
306 | private String condition;
307 |
308 | private Object value;
309 |
310 | private Object secondValue;
311 |
312 | private boolean noValue;
313 |
314 | private boolean singleValue;
315 |
316 | private boolean betweenValue;
317 |
318 | private boolean listValue;
319 |
320 | private String typeHandler;
321 |
322 | public String getCondition() {
323 | return condition;
324 | }
325 |
326 | public Object getValue() {
327 | return value;
328 | }
329 |
330 | public Object getSecondValue() {
331 | return secondValue;
332 | }
333 |
334 | public boolean isNoValue() {
335 | return noValue;
336 | }
337 |
338 | public boolean isSingleValue() {
339 | return singleValue;
340 | }
341 |
342 | public boolean isBetweenValue() {
343 | return betweenValue;
344 | }
345 |
346 | public boolean isListValue() {
347 | return listValue;
348 | }
349 |
350 | public String getTypeHandler() {
351 | return typeHandler;
352 | }
353 |
354 | protected Criterion(String condition) {
355 | super();
356 | this.condition = condition;
357 | this.typeHandler = null;
358 | this.noValue = true;
359 | }
360 |
361 | protected Criterion(String condition, Object value, String typeHandler) {
362 | super();
363 | this.condition = condition;
364 | this.value = value;
365 | this.typeHandler = typeHandler;
366 | if (value instanceof List>) {
367 | this.listValue = true;
368 | } else {
369 | this.singleValue = true;
370 | }
371 | }
372 |
373 | protected Criterion(String condition, Object value) {
374 | this(condition, value, null);
375 | }
376 |
377 | protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
378 | super();
379 | this.condition = condition;
380 | this.value = value;
381 | this.secondValue = secondValue;
382 | this.typeHandler = typeHandler;
383 | this.betweenValue = true;
384 | }
385 |
386 | protected Criterion(String condition, Object value, Object secondValue) {
387 | this(condition, value, secondValue, null);
388 | }
389 | }
390 | }
--------------------------------------------------------------------------------
/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | spring:
2 | #Dubbo 服务消费者配置
3 | dubbo:
4 | application:
5 | name: autotest
6 | registries[0]:
7 | address: zookeeper://192.168.2.223:2181
8 | timeout: 60000
9 | scan: com.autotest
10 | #数据库配置
11 | ds:
12 | #default
13 | default:
14 | url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=true
15 | username: root
16 | password: 123456
17 | driverClassName: com.mysql.jdbc.Driver
18 | #school
19 | school:
20 | url: jdbc:mysql://127.0.0.1:3306/school?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&useSSL=true
21 | username: root
22 | password: 123456
23 | driverClassName: com.mysql.jdbc.Driver
24 | tables: student
25 |
--------------------------------------------------------------------------------
/src/main/resources/dal/mapping/school/StudentMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
8 |
9 |
10 |
11 |
12 |
13 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | and ${criterion.condition}
24 |
25 |
26 | and ${criterion.condition} #{criterion.value}
27 |
28 |
29 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
30 |
31 |
32 | and ${criterion.condition}
33 |
34 | #{listItem}
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | and ${criterion.condition}
56 |
57 |
58 | and ${criterion.condition} #{criterion.value}
59 |
60 |
61 | and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
62 |
63 |
64 | and ${criterion.condition}
65 |
66 | #{listItem}
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
80 | id, name, age
81 |
82 |
99 |
100 |
103 | delete from student
104 |
105 |
106 |
107 |
108 |
117 |
118 |
121 | update student
122 |
123 |
124 | id = #{record.id,jdbcType=BIGINT},
125 |
126 |
127 | name = #{record.name,jdbcType=VARCHAR},
128 |
129 |
130 | age = #{record.age,jdbcType=INTEGER},
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
141 | update student
142 | set id = #{record.id,jdbcType=BIGINT},
143 | name = #{record.name,jdbcType=VARCHAR},
144 | age = #{record.age,jdbcType=INTEGER}
145 |
146 |
147 |
148 |
149 |
--------------------------------------------------------------------------------
/src/main/resources/ftl/generateDataSourceConfig.ftl:
--------------------------------------------------------------------------------
1 | package com.autotest.config;
2 |
3 | import com.alibaba.druid.pool.DruidDataSource;
4 | import com.autotest.mybatis.MultiBeanNameGenerator;
5 | import org.apache.ibatis.session.SqlSessionFactory;
6 | import org.mybatis.spring.SqlSessionFactoryBean;
7 | import tk.mybatis.spring.annotation.MapperScan;
8 | import org.springframework.beans.factory.annotation.Qualifier;
9 | import org.springframework.beans.factory.annotation.Value;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.jdbc.datasource.DataSourceTransactionManager;
13 | import javax.sql.DataSource;
14 |
15 | @Configuration
16 | @MapperScan(basePackages = "dal.dao.${projectName}", sqlSessionFactoryRef = "${projectName}SqlSessionFactory", nameGenerator =
17 | MultiBeanNameGenerator.class)
18 | public class ${className} extends DruidBaseConfig {
19 |
20 | @Value("${r"${"}ds.${projectName}.url}")
21 | private String url;
22 |
23 | @Value("${r"${"}ds.${projectName}.username}")
24 | private String username;
25 |
26 | @Value("${r"${"}ds.${projectName}.password}")
27 | private String password;
28 |
29 | @Value("${r"${"}ds.${projectName}.driverClassName}")
30 | private String driverClassName;
31 |
32 | @Bean(name = "${projectName}DataSource")
33 | public DataSource ${projectName}DataSource() {
34 | DruidDataSource dataSource = new DruidDataSource();
35 | return setDataSource(dataSource, url, username, password, driverClassName);
36 | }
37 |
38 | @Bean(name = "${projectName}TransactionManager")
39 | public DataSourceTransactionManager ${projectName}TransactionManager() {
40 | return new DataSourceTransactionManager(${projectName}DataSource());
41 | }
42 |
43 | @Bean(name = "${projectName}SqlSessionFactory")
44 | public SqlSessionFactory ${projectName}SqlSessionFactory(@Qualifier("${projectName}DataSource") DataSource ${projectName}DataSource)
45 | throws Exception {
46 | final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
47 | sessionFactory.setDataSource(${projectName}DataSource);
48 | return sessionFactory.getObject();
49 | }
50 | }
--------------------------------------------------------------------------------
/src/main/resources/ftl/generateFacadeCase.ftl:
--------------------------------------------------------------------------------
1 | package ${packageName};
2 |
3 | import com.autotest.base.SpringBootTestBase;
4 | import com.autotest.annotation.AutoTest;
5 | import org.junit.jupiter.api.DisplayName;
6 | import com.alibaba.dubbo.config.annotation.Reference;
7 | import java.util.Date;
8 | import java.util.ArrayList;
9 | import java.util.List;
10 | import ${resultType};
11 | import ${facadeType};
12 | <#list importlist as im>
13 | import ${im};
14 | #list>
15 |
16 |
17 |
18 | /**
19 | * @author ${author!'huairen'}
20 | * Created on ${time}.
21 | */
22 | public class ${className} extends SpringBootTestBase{
23 |
24 | @Reference(version = "1.0")
25 | ${facadeName} ${facadeName?uncap_first};
26 |
27 | @AutoTest(file = "${projectName}/${funcName}Success.csv")
28 | public void ${funcName}Success(
29 | // 基本参数
30 | int testId,
31 | // 业务参数
32 | <#list childlist as child>
33 | <#if child ??>
34 | <#list pars as parm>
35 | <#if 0 == parm.layer>
36 | ${parm.typeName} ${parm.name}<#if (child_has_next)>,#if>
37 | #if>
38 | #list>
39 | #if>
40 | #list>
41 | <#list pars_order as parm>
42 | ${parm.typeName} ${parm.name}<#if (parm_has_next)>,#if>
43 | #list>
44 | ) {
45 | // 清除数据
46 | // 准备数据
47 | // 测试过程
48 | <#--次子订单-->
49 | <#list pars as parm>
50 | <#if 2 == parm.layer>
51 | <#if "List" ==parm.typeName>
52 | <#list pars_order as p>
53 | <#if p.name == parm.children>
54 | List<${p.name?cap_first}> ${parm.secondName} = new ArrayList<${p.name?cap_first}>();
55 | ${parm.name}.add(${parm.children});
56 | #if>
57 | #list>
58 | #if>
59 | <#if "Map" == parm.typeName>
60 | Map ${parm.name} = new HashMap<>();
61 | #if>
62 | #if>
63 | #list>
64 | <#--子订单-->
65 | <#list pars as parm>
66 | <#if 1 == parm.layer>
67 | <#if "List" ==parm.typeName>
68 | <#list pars_order as p>
69 | <#if p.name == parm.children>
70 | List<${p.name?cap_first}> ${parm.secondName} = new ArrayList<${p.name?cap_first}>();
71 | ${parm.name}.add(${parm.children});
72 | #if>
73 | #list>
74 | #if>
75 | <#if "Map" == parm.typeName>
76 | Map ${parm.name} = new HashMap<>();
77 | #if>
78 | #if>
79 | #list>
80 | <#--主订单-->
81 | <#list pars_order as parm>
82 | <#if 0 == parm.layer>
83 | <#list pars_order as par>
84 | <#if 1 == par.layer>
85 | ${parm.name}.set${par.name?cap_first}(${par.name});
86 | #if>
87 | #list>
88 | #if>
89 | #list>
90 | // 调用接口
91 | ${resultName} result = ${facadeName?uncap_first}.${methodName}(<#list baselist as base>${base}<#if (base_has_next)>,#if>#list>);
92 | // 结果验证
93 | // 数据验证
94 | <#--
95 | -->
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/src/main/resources/ftl/generateTestBase.ftl:
--------------------------------------------------------------------------------
1 | package ${packageName};
2 |
3 | import java.util.List;
4 | import java.util.Date;
5 | import org.junit.platform.commons.util.StringUtils;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 | import java.math.BigDecimal;
9 | import dal.dao.${projectName}.*;
10 | import dal.model.${projectName}.*;
11 |
12 | /**
13 | * @author ${author!'huairen'}
14 | * Created on ${time}.
15 | */
16 | @Service
17 | public class ${className} {
18 | <#list tableList as table>
19 |
20 | @Autowired
21 | ${table.tableName}DAO ${table.tableName?uncap_first}DAO;
22 | #list>
23 |
24 | <#list tableList as table>
25 | <#--insert插入方法-->
26 | /**
27 | * 插入${table.table}表数据
28 | */
29 | public void insert${table.tableName}(${table.tableName}DO ${table.tableName?uncap_first}DO) {
30 | ${table.tableName?uncap_first}DAO.insert(${table.tableName?uncap_first}DO);
31 | }
32 |
33 | <#--insert插入方法-->
34 | /**
35 | * 插入${table.table}表数据
36 | */
37 | public void insert${table.tableName}(
38 | <#list table.pars as par>
39 | ${par.typeName} ${par.name}<#if (par_has_next)>, #if>
40 | #list>
41 | ) {
42 | <#list table.pars as par>
43 | <#if "Date"==par.typeName>
44 | if (${par.name} == null) {
45 | ${par.name} = new Date();
46 | }
47 | #if >
48 | <#if "Money"==par.typeName>
49 | if (${par.name} == null) {
50 | ${par.name} = new Money(0);
51 | }
52 | #if >
53 | #list>
54 | ${table.tableName}DO ${table.tableName?uncap_first}DO = new ${table.tableName}DO();
55 | <#list table.pars as par>
56 | <#if par.annotation ??>
57 | ${table.tableName?uncap_first}DO.set${par.name}(${par.name});
58 | <#else>
59 | ${table.tableName?uncap_first}DO.set${par.name?cap_first}(${par.name});
60 | #if >
61 | #list>
62 | ${table.tableName?uncap_first}DAO.insert(${table.tableName?uncap_first}DO);
63 | }
64 |
65 | <#list table.pars as par>
66 | <#if par.name?index_of("versionId")!=-1>
67 | <#continue>
68 | #if>
69 | <#if par.name?index_of("Id")!=-1 || par.name?index_of("id")!=-1>
70 |
71 | /**
72 | * 根据${par.name}删除${table.table}表数据
73 | */
74 | public void clean${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
75 | <#if "Date"==par.typeName>
76 | if (${par.name} == null) {
77 | ${par.name} = new Date();
78 | }
79 | #if >
80 | <#if "Money"==par.typeName>
81 | if (${par.name} == null) {
82 | ${par.name} = new Money(0);
83 | }
84 | #if >
85 | <#if "String"==par.typeName>
86 | if (StringUtils.isBlank(${par.name})){
87 | ${par.name} = "test_${par.name}";
88 | }
89 | #if >
90 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
91 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
92 | ${table.tableName?uncap_first}DAO.deleteByExample(exam);
93 | }
94 | #if>
95 | <#if par.name?index_of("No")!=-1 || par.name?index_of("loginAccount")!=-1>
96 |
97 | /**
98 | * 根据${par.name}删除${table.table}表数据
99 | */
100 | public void clean${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
101 | <#if "Date"==par.typeName>
102 | if (${par.name} == null) {
103 | ${par.name} = new Date();
104 | }
105 | #if >
106 | <#if "Money"==par.typeName>
107 | if (${par.name} == null) {
108 | ${par.name} = new Money(0);
109 | }
110 | #if >
111 | <#if "String"==par.typeName>
112 | if (StringUtils.isBlank(${par.name})){
113 | ${par.name} = "test_${par.name}";
114 | }
115 | #if >
116 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
117 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
118 | ${table.tableName?uncap_first}DAO.deleteByExample(exam);
119 | }
120 | #if>
121 | <#if par.name?index_of("Name")!=-1 || par.name?index_of("name")!=-1>
122 |
123 | /**
124 | * 根据${par.name}删除${table.table}表数据
125 | */
126 | public void clean${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
127 | <#if "Date"==par.typeName>
128 | if (${par.name} == null) {
129 | ${par.name} = new Date();
130 | }
131 | #if >
132 | <#if "Money"==par.typeName>
133 | if (${par.name} == null) {
134 | ${par.name} = new Money(0);
135 | }
136 | #if >
137 | <#if "String"==par.typeName>
138 | if (StringUtils.isBlank(${par.name})){
139 | ${par.name} = "test_${par.name}";
140 | }
141 | #if >
142 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
143 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
144 | ${table.tableName?uncap_first}DAO.deleteByExample(exam);
145 | }
146 | #if>
147 | <#if par.name?index_of("Number")!=-1>
148 |
149 | /**
150 | * 根据${par.name}删除${table.table}表数据
151 | */
152 | public void clean${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
153 | <#if "Date"==par.typeName>
154 | if (${par.name} == null) {
155 | ${par.name} = new Date();
156 | }
157 | #if >
158 | <#if "Money"==par.typeName>
159 | if (${par.name} == null) {
160 | ${par.name} = new Money(0);
161 | }
162 | #if >
163 | <#if "String"==par.typeName>
164 | if (StringUtils.isBlank(${par.name})){
165 | ${par.name} = "test_${par.name}";
166 | }
167 | #if >
168 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
169 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
170 | ${table.tableName?uncap_first}DAO.deleteByExample(exam);
171 | }
172 | #if>
173 | #list>
174 |
175 | <#list table.pars as par>
176 | <#if par.name?index_of("versionId")!=-1>
177 | <#continue>
178 | #if>
179 | <#if par.name?index_of("Id")!=-1 || par.name?index_of("id")!=-1>
180 |
181 | /**
182 | * 根据${par.name}查询${table.table}表数据
183 | */
184 | public List<${table.tableName}DO> find${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
185 | <#if "Date"==par.typeName>
186 | if (${par.name} == null) {
187 | ${par.name} = new Date();
188 | }
189 | #if >
190 | <#if "Money"==par.typeName>
191 | if (${par.name} == null) {
192 | ${par.name} = new Money(0);
193 | }
194 | #if >
195 | <#if "String"==par.typeName>
196 | if (StringUtils.isBlank(${par.name})){
197 | ${par.name} = "test_${par.name}";
198 | }
199 | #if >
200 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
201 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
202 | return ${table.tableName?uncap_first}DAO.selectByExample(exam);
203 | }
204 | #if>
205 | <#if par.name?index_of("No")!=-1 || par.name?index_of("loginAccount")!=-1>
206 |
207 | /**
208 | * 根据${par.name}查询${table.table}表数据
209 | */
210 | public List<${table.tableName}DO> find${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
211 | <#if "Date"==par.typeName>
212 | if (${par.name} == null) {
213 | ${par.name} = new Date();
214 | }
215 | #if >
216 | <#if "Money"==par.typeName>
217 | if (${par.name} == null) {
218 | ${par.name} = new Money(0);
219 | }
220 | #if >
221 | <#if "String"==par.typeName>
222 | if (StringUtils.isBlank(${par.name})){
223 | ${par.name} = "test_${par.name}";
224 | }
225 | #if >
226 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
227 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
228 | return ${table.tableName?uncap_first}DAO.selectByExample(exam);
229 | }
230 | #if>
231 | <#if par.name?index_of("Name")!=-1 || par.name?index_of("name")!=-1>
232 |
233 | /**
234 | * 根据${par.name}查询${table.table}表数据
235 | */
236 | public List<${table.tableName}DO> find${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
237 | <#if "Date"==par.typeName>
238 | if (${par.name} == null) {
239 | ${par.name} = new Date();
240 | }
241 | #if >
242 | <#if "Money"==par.typeName>
243 | if (${par.name} == null) {
244 | ${par.name} = new Money(0);
245 | }
246 | #if >
247 | <#if "String"==par.typeName>
248 | if (StringUtils.isBlank(${par.name})){
249 | ${par.name} = "test_${par.name}";
250 | }
251 | #if >
252 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
253 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
254 | return ${table.tableName?uncap_first}DAO.selectByExample(exam);
255 | }
256 | #if>
257 | <#if par.name?index_of("Number")!=-1>
258 |
259 | /**
260 | * 根据${par.name}查询${table.table}表数据
261 | */
262 | public List<${table.tableName}DO> find${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name}) {
263 | <#if "Date"==par.typeName>
264 | if (${par.name} == null) {
265 | ${par.name} = new Date();
266 | }
267 | #if >
268 | <#if "Money"==par.typeName>
269 | if (${par.name} == null) {
270 | ${par.name} = new Money(0);
271 | }
272 | #if >
273 | <#if "String"==par.typeName>
274 | if (StringUtils.isBlank(${par.name})){
275 | ${par.name} = "test_${par.name}";
276 | }
277 | #if >
278 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
279 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
280 | return ${table.tableName?uncap_first}DAO.selectByExample(exam);
281 | }
282 | #if>
283 | #list>
284 | <#list table.pars as par>
285 | <#if par.name?index_of("versionId")!=-1>
286 | <#continue>
287 | #if>
288 | <#if par.name?index_of("Id")!=-1 || par.name?index_of("id")!=-1>
289 |
290 | /**
291 | * 根据${par.name}更新${table.table}表数据
292 | */
293 | public void update${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name},${table.tableName}DO ${table.tableName?uncap_first}DO) {
294 | <#if "Date"==par.typeName>
295 | if (${par.name} == null) {
296 | ${par.name} = new Date();
297 | }
298 | #if >
299 | <#if "Money"==par.typeName>
300 | if (${par.name} == null) {
301 | ${par.name} = new Money(0);
302 | }
303 | #if >
304 | <#if "String"==par.typeName>
305 | if (StringUtils.isBlank(${par.name})){
306 | ${par.name} = "test_${par.name}";
307 | }
308 | #if >
309 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
310 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
311 | ${table.tableName?uncap_first}DAO.updateByExampleSelective(${table.tableName?uncap_first}DO, exam);
312 | }
313 | #if>
314 | <#if par.name?index_of("No")!=-1 || par.name?index_of("loginAccount")!=-1>
315 |
316 | /**
317 | * 根据${par.name}更新${table.table}表数据
318 | */
319 | public void update${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name},${table.tableName}DO ${table.tableName?uncap_first}DO) {
320 | <#if "Date"==par.typeName>
321 | if (${par.name} == null) {
322 | ${par.name} = new Date();
323 | }
324 | #if >
325 | <#if "Money"==par.typeName>
326 | if (${par.name} == null) {
327 | ${par.name} = new Money(0);
328 | }
329 | #if >
330 | <#if "String"==par.typeName>
331 | if (StringUtils.isBlank(${par.name})){
332 | ${par.name} = "test_${par.name}";
333 | }
334 | #if >
335 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
336 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
337 | ${table.tableName?uncap_first}DAO.updateByExampleSelective(${table.tableName?uncap_first}DO, exam);
338 | }
339 | #if>
340 | <#if par.name?index_of("Name")!=-1 || par.name?index_of("name")!=-1>
341 |
342 | /**
343 | * 根据${par.name}更新${table.table}表数据
344 | */
345 | public void update${table.tableName}By${par.name?cap_first}(${par.typeName} ${par.name},${table.tableName}DO ${table.tableName?uncap_first}DO) {
346 | <#if "Date"==par.typeName>
347 | if (${par.name} == null) {
348 | ${par.name} = new Date();
349 | }
350 | #if >
351 | <#if "Money"==par.typeName>
352 | if (${par.name} == null) {
353 | ${par.name} = new Money(0);
354 | }
355 | #if >
356 | <#if "String"==par.typeName>
357 | if (StringUtils.isBlank(${par.name})){
358 | ${par.name} = "test_${par.name}";
359 | }
360 | #if >
361 | ${table.tableName}DOExample exam = new ${table.tableName}DOExample();
362 | exam.createCriteria().and${par.name?cap_first}EqualTo(${par.name});
363 | ${table.tableName?uncap_first}DAO.updateByExampleSelective(${table.tableName?uncap_first}DO, exam);
364 | }
365 | #if>
366 | #list>
367 | #list>
368 |
369 | }
370 |
--------------------------------------------------------------------------------
/src/main/resources/lib/mysql-connector-java-5.1.30.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ychaoyang/autotest/858e5cc8dd0a27585ff494e0d79303d3f4c0b9ca/src/main/resources/lib/mysql-connector-java-5.1.30.jar
--------------------------------------------------------------------------------
/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 | ${LOG_HOME}/autotest-%d{yyyy-MM-dd}.log
18 |
19 | 30
20 |
21 |
22 |
23 | %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
24 |
25 |
26 |
27 | 5MB
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/main/resources/webdriver/chromedriver.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ychaoyang/autotest/858e5cc8dd0a27585ff494e0d79303d3f4c0b9ca/src/main/resources/webdriver/chromedriver.exe
--------------------------------------------------------------------------------
/src/test/java/com/autotest/Generator.java:
--------------------------------------------------------------------------------
1 | package com.autotest;
2 |
3 | import com.autotest.generate.GenerateFacadeCase;
4 | import com.autotest.example.QueryUserFacade;
5 | import org.junit.jupiter.api.Test;
6 | import org.junit.jupiter.api.extension.ExtendWith;
7 | import org.springframework.boot.test.context.SpringBootTest;
8 | import org.springframework.test.context.junit.jupiter.SpringExtension;
9 |
10 | public class Generator {
11 |
12 | @Test
13 | public void genFacadeCase() {
14 | String author = "huairen";
15 | String methodName = "queryUserById";
16 | String packag = "test";
17 | new GenerateFacadeCase().generate(QueryUserFacade.class, methodName, packag, author, Generator.class);
18 | }
19 |
20 |
21 | }
--------------------------------------------------------------------------------
/src/test/java/com/autotest/example/QueryOrder.java:
--------------------------------------------------------------------------------
1 | package com.autotest.example;
2 |
3 | /**
4 | * @author huairen
5 | * Created on 18/6/2.
6 | */
7 | public class QueryOrder {
8 |
9 | private int id;
10 |
11 | private String name;
12 |
13 | public int getId() {
14 | return id;
15 | }
16 |
17 | public void setId(int id) {
18 | this.id = id;
19 | }
20 |
21 | public String getName() {
22 | return name;
23 | }
24 |
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return "QueryOrder{" +
32 | "id=" + id +
33 | ", name='" + name + '\'' +
34 | '}';
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/example/QueryUserFacade.java:
--------------------------------------------------------------------------------
1 | package com.autotest.example;
2 |
3 |
4 | /**
5 | * @author huairen
6 | * Created on 18/6/2.
7 | */
8 | public interface QueryUserFacade {
9 |
10 | UserResult queryUserById(QueryOrder order);
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/example/UserResult.java:
--------------------------------------------------------------------------------
1 | package com.autotest.example;
2 |
3 | /**
4 | * @author huairen
5 | * Created on 18/6/2.
6 | */
7 | public class UserResult {
8 |
9 | private String code;
10 |
11 | private Long id;
12 |
13 | private String name;
14 |
15 | private Integer age;
16 |
17 | public String getCode() {
18 | return code;
19 | }
20 |
21 | public void setCode(String code) {
22 | this.code = code;
23 | }
24 |
25 | public Long getId() {
26 | return id;
27 | }
28 |
29 | public void setId(Long id) {
30 | this.id = id;
31 | }
32 |
33 | public String getName() {
34 | return name;
35 | }
36 |
37 | public void setName(String name) {
38 | this.name = name;
39 | }
40 |
41 | public Integer getAge() {
42 | return age;
43 | }
44 |
45 | public void setAge(Integer age) {
46 | this.age = age;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | StringBuilder sb = new StringBuilder();
52 | sb.append(getClass().getSimpleName());
53 | sb.append(" [");
54 | sb.append(", id=").append(id);
55 | sb.append(", name=").append(name);
56 | sb.append(", age=").append(age);
57 | sb.append("]");
58 | return sb.toString();
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/test/HttpTest.java:
--------------------------------------------------------------------------------
1 | package com.autotest.test;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.autotest.annotation.AutoTest;
5 | import com.autotest.base.AutoTestBase;
6 | import org.springframework.http.ResponseEntity;
7 | import org.springframework.util.LinkedMultiValueMap;
8 | import org.springframework.util.MultiValueMap;
9 | import org.springframework.web.client.RestTemplate;
10 |
11 | /**
12 | * Created by yu on 17/12/26.
13 | */
14 | public class HttpTest extends AutoTestBase {
15 |
16 | protected RestTemplate restTemplate = new RestTemplate();
17 |
18 | /**
19 | * 有道翻译接口
20 | */
21 | @AutoTest(file = "httpTest.csv")
22 | void httpTest(int testId) {
23 | String url = "http://fanyi.youdao.com/translate";
24 | MultiValueMap map = new LinkedMultiValueMap();
25 | //需要翻译的单词
26 | String word = "today";
27 | map.add("i", word);
28 | map.add("type", "auto");
29 | map.add("doctype", "json");
30 | map.add("xmlVersion", "1.8");
31 | // 调用接口
32 | ResponseEntity response = restTemplate.postForEntity(url, map, String.class);
33 | print("原单词:" + word);
34 | print("翻译后:" + JSON.parseObject(response.getBody()).getJSONArray("translateResult").getJSONArray(0).get(0));
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/test/MybatisTest.java:
--------------------------------------------------------------------------------
1 | package com.autotest.test;
2 |
3 | import com.autotest.annotation.AutoTest;
4 | import com.autotest.base.SpringBootTestBase;
5 | import com.autotest.testbase.SchoolTestBase;
6 | import dal.model.school.StudentDO;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 |
9 | public class MybatisTest extends SpringBootTestBase {
10 |
11 | @Autowired
12 | SchoolTestBase schoolTestBase;
13 |
14 | @AutoTest(file = "mybatisTest.csv")
15 | void mybatisTest(
16 | int testId,
17 | StudentDO studentDO
18 | ) {
19 | //插入数据
20 | schoolTestBase.insertStudent(studentDO);
21 | //查询数据
22 | print(schoolTestBase.findStudentByName(studentDO.getName()));
23 | //清除数据
24 | schoolTestBase.cleanStudentByName(studentDO.getName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/test/QueryUserFacadeQueryUserByIdTest.java:
--------------------------------------------------------------------------------
1 | package com.autotest.test;
2 |
3 | import com.autotest.base.SpringBootTestBase;
4 | import com.autotest.annotation.AutoTest;
5 | import org.junit.jupiter.api.DisplayName;
6 | import com.alibaba.dubbo.config.annotation.Reference;
7 | import java.util.Date;
8 | import java.util.ArrayList;
9 | import java.util.List;
10 | import com.autotest.example.UserResult;
11 | import com.autotest.example.QueryUserFacade;
12 | import com.autotest.example.QueryOrder;
13 |
14 |
15 |
16 | /**
17 | * @author huairen
18 | * Created on 2018年06月02日.
19 | */
20 | public class QueryUserFacadeQueryUserByIdTest extends SpringBootTestBase{
21 |
22 | @Reference(version = "1.0")
23 | QueryUserFacade queryUserFacade;
24 |
25 | @AutoTest(file = "test/queryUserFacadeQueryUserByIdTestSuccess.csv")
26 | public void queryUserFacadeQueryUserByIdTestSuccess(
27 | // 基本参数
28 | int testId,
29 | // 业务参数
30 | QueryOrder order
31 | ) {
32 | // 清除数据
33 | // 准备数据
34 | // 测试过程
35 | // 调用接口
36 | UserResult result = queryUserFacade.queryUserById(order);
37 | // 结果验证
38 | // 数据验证
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/test/WebTest.java:
--------------------------------------------------------------------------------
1 | package com.autotest.test;
2 |
3 | import com.autotest.testbase.web.WebTestBase;
4 | import org.junit.jupiter.api.DisplayName;
5 | import org.junit.jupiter.api.Test;
6 | import org.openqa.selenium.By;
7 |
8 | import static com.codeborne.selenide.Selenide.$;
9 | import static com.codeborne.selenide.Selenide.open;
10 | import static com.codeborne.selenide.Selenide.title;
11 |
12 | /**
13 | * Created by huairen on 2017/8/7.
14 | */
15 | public class WebTest extends WebTestBase {
16 |
17 | @Test
18 | @DisplayName("测试百度搜索页面")
19 | void baiduTest() {
20 | open("http://www.baidu.com");
21 | $(By.id("kw")).setValue("junit");
22 | $(By.id("su")).click();
23 | print(title());
24 | }
25 |
26 | }
27 |
28 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/testbase/SchoolTestBase.java:
--------------------------------------------------------------------------------
1 | package com.autotest.testbase;
2 |
3 | import java.util.List;
4 | import java.util.Date;
5 | import org.junit.platform.commons.util.StringUtils;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 | import java.math.BigDecimal;
9 | import dal.dao.school.*;
10 | import dal.model.school.*;
11 |
12 | /**
13 | * @author autotest
14 | * Created on 2018/06/02.
15 | */
16 | @Service
17 | public class SchoolTestBase {
18 |
19 | @Autowired
20 | StudentDAO studentDAO;
21 |
22 | /**
23 | * 插入student表数据
24 | */
25 | public void insertStudent(StudentDO studentDO) {
26 | studentDAO.insert(studentDO);
27 | }
28 |
29 | /**
30 | * 插入student表数据
31 | */
32 | public void insertStudent(
33 | long id,
34 | String name,
35 | Integer age
36 | ) {
37 | StudentDO studentDO = new StudentDO();
38 | studentDO.setId(id);
39 | studentDO.setName(name);
40 | studentDO.setAge(age);
41 | studentDAO.insert(studentDO);
42 | }
43 |
44 |
45 | /**
46 | * 根据id删除student表数据
47 | */
48 | public void cleanStudentById(long id) {
49 | StudentDOExample exam = new StudentDOExample();
50 | exam.createCriteria().andIdEqualTo(id);
51 | studentDAO.deleteByExample(exam);
52 | }
53 |
54 | /**
55 | * 根据name删除student表数据
56 | */
57 | public void cleanStudentByName(String name) {
58 | if (StringUtils.isBlank(name)){
59 | name = "test_name";
60 | }
61 | StudentDOExample exam = new StudentDOExample();
62 | exam.createCriteria().andNameEqualTo(name);
63 | studentDAO.deleteByExample(exam);
64 | }
65 |
66 |
67 | /**
68 | * 根据id查询student表数据
69 | */
70 | public List findStudentById(long id) {
71 | StudentDOExample exam = new StudentDOExample();
72 | exam.createCriteria().andIdEqualTo(id);
73 | return studentDAO.selectByExample(exam);
74 | }
75 |
76 | /**
77 | * 根据name查询student表数据
78 | */
79 | public List findStudentByName(String name) {
80 | if (StringUtils.isBlank(name)){
81 | name = "test_name";
82 | }
83 | StudentDOExample exam = new StudentDOExample();
84 | exam.createCriteria().andNameEqualTo(name);
85 | return studentDAO.selectByExample(exam);
86 | }
87 |
88 | /**
89 | * 根据id更新student表数据
90 | */
91 | public void updateStudentById(long id,StudentDO studentDO) {
92 | StudentDOExample exam = new StudentDOExample();
93 | exam.createCriteria().andIdEqualTo(id);
94 | studentDAO.updateByExampleSelective(studentDO, exam);
95 | }
96 |
97 | /**
98 | * 根据name更新student表数据
99 | */
100 | public void updateStudentByName(String name,StudentDO studentDO) {
101 | if (StringUtils.isBlank(name)){
102 | name = "test_name";
103 | }
104 | StudentDOExample exam = new StudentDOExample();
105 | exam.createCriteria().andNameEqualTo(name);
106 | studentDAO.updateByExampleSelective(studentDO, exam);
107 | }
108 |
109 | }
110 |
--------------------------------------------------------------------------------
/src/test/java/com/autotest/testbase/web/WebTestBase.java:
--------------------------------------------------------------------------------
1 | package com.autotest.testbase.web;
2 |
3 | import com.autotest.base.SpringBootTestBase;
4 | import com.codeborne.selenide.Configuration;
5 | import org.junit.jupiter.api.AfterEach;
6 | import org.junit.jupiter.api.BeforeAll;
7 |
8 | import static com.codeborne.selenide.Selenide.close;
9 |
10 | public class WebTestBase extends SpringBootTestBase {
11 |
12 | @BeforeAll
13 | static void init() {
14 | System.setProperty("webdriver.chrome.driver", Thread.currentThread().getContextClassLoader()
15 | .getResource("webdriver/" + "chromedriver.exe").getPath());
16 | System.setProperty("chromeoptions.args", "disable-infobars");
17 | Configuration.browser = "chrome";
18 | Configuration.browserSize = "1920x1080";
19 | Configuration.timeout = 6000;
20 | Configuration.screenshots = false;
21 | }
22 |
23 | @AfterEach
24 | void tearDown() {
25 | close();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/test/resources/autotest/csvTest.csv:
--------------------------------------------------------------------------------
1 | testId,memo,result
2 | 1001, 第一条测试用例 , SUCCESS
3 | 1002, 第二条测试用例 , FAIL
--------------------------------------------------------------------------------
/src/test/resources/autotest/httpTest.csv:
--------------------------------------------------------------------------------
1 | result,testId
2 | SUCCESS,1001
3 |
--------------------------------------------------------------------------------
/src/test/resources/autotest/mybatisTest.csv:
--------------------------------------------------------------------------------
1 | testId,id,name,age
2 | 1001,1,~小明,18
3 | 1001,2,~小红,20
--------------------------------------------------------------------------------
/src/test/resources/autotest/test/queryUserFacadeQueryUserByIdTestSuccess.csv:
--------------------------------------------------------------------------------
1 | testId,id,name
2 | 1001,20181206,xiaoming
--------------------------------------------------------------------------------
/student.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE `student` (
2 | `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
3 | `name` VARCHAR(40) NOT NULL DEFAULT '' COMMENT '名称',
4 | `age` INT(6) NOT NULL DEFAULT '0' COMMENT '年龄',
5 | PRIMARY KEY (`id`)
6 | )DEFAULT CHARSET=utf8 COMMENT='学生表'
7 |
--------------------------------------------------------------------------------