├── .gitattributes ├── .gitignore ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── lance │ │ └── code │ │ └── generation │ │ ├── common │ │ ├── ConfigConstants.java │ │ ├── FileUtils.java │ │ ├── InterfaceMethod.java │ │ ├── JavaBeanHandler.java │ │ ├── KeyWords.java │ │ └── XMLMethod.java │ │ ├── domain │ │ ├── ColumnInfo.java │ │ └── TableInfo.java │ │ ├── mapper │ │ └── TablesMapper.java │ │ ├── service │ │ ├── TableService.java │ │ └── TableServiceImpl.java │ │ └── web │ │ └── SimpleApplication.java ├── resources │ ├── application.properties │ └── mapper │ │ └── tablesMapper.xml └── webapp │ ├── WEB-INF │ └── web.xml │ └── index.jsp └── test └── java └── com └── lance └── code └── generation └── service └── MainTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | 49 | .settings/ 50 | target/ 51 | .project 52 | .classpath 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # code-generation 2 | Mybatis Auto Code Generation Mapper/Service/Domain/SQL.xml, 主要是针对某个数据库所有表[参考配置文件ConfigConstants], 统一生成Domain/Service/Mapper,以及SQL.xml文件, 3 | 如:针对t_customer, 通过该应用生成CustonerInfo.java, CustomerSerivce.java/CustomerServiceImpl.java, CustomerMapper.java 以及 Customer-mapper.xml文件. 小工具类 4 | 5 | Project run MainTest 6 | ```java 7 | @RunWith(SpringRunner.class) 8 | @SpringBootTest(classes=SimpleApplication.class) 9 | public class MainTest { 10 | Logger logger = LoggerFactory.getLogger(getClass()); 11 | @Autowired 12 | private TableService tableService; 13 | 14 | @Test 15 | public void main() { 16 | long startTime = System.currentTimeMillis(); 17 | logger.info("...........start application........."); 18 | 19 | tableService.run(); 20 | 21 | logger.info("...end application...Time: {}",(System.currentTimeMillis()-startTime)); 22 | } 23 | } 24 | ``` 25 | 26 | Generate Code Config 27 | ```java 28 | public interface ConfigConstants { 29 | /**定义Schema*/ 30 | String SCHEMA = "longchou-loan"; 31 | 32 | /**移除表前缀*/ 33 | String REMOVE_TABLE_PREFIX = "t_"; 34 | 35 | /**Domain后缀, CustomerInfo*/ 36 | String DOMAIN_SUFFIX = ""; 37 | 38 | /**生成文件路径*/ 39 | String FILE_PATH = "E:\\gitwork\\code-generation\\src\\main\\java"; 40 | 41 | /**MyBatis SQL生成文件路径*/ 42 | String SQL_PATH = "E:\\gitwork\\code-generation\\src\\main\\resources\\mappers"; 43 | 44 | /**生成Mapper.xml后缀名字, EX: customer-mapper.xml*/ 45 | String SQL_MAPPER_SUFFIX = "-mapper"; 46 | 47 | /**生成包名称*/ 48 | String ROOT_PACKAGE = "com.lance.code"; 49 | 50 | /**JavaBean包名称*/ 51 | String DOMAIN_PACKAGE = "domain"; 52 | 53 | /**service包名称*/ 54 | String SERVICE_PACKAGE = "service"; 55 | 56 | /**serviceImpl包名称*/ 57 | String SERVICE_impl_PACKAGE = "serviceImpl"; 58 | 59 | /**mapper包名称*/ 60 | String MAPPER_PACKAGE = "mapper"; 61 | } 62 | ``` 63 | 64 | Add dependencies to pom 65 | ```xml 66 | 67 | org.springframework.boot 68 | spring-boot-starter-parent 69 | 1.4.0.RELEASE 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-starter-web 74 | 75 | 76 | 77 | org.mybatis.spring.boot 78 | mybatis-spring-boot-starter 79 | 1.1.1 80 | 81 | 82 | 83 | mysql 84 | mysql-connector-java 85 | 86 | 87 | com.alibaba 88 | fastjson 89 | 1.2.12 90 | 91 | 92 | org.apache.commons 93 | commons-lang3 94 | 3.4 95 | 96 | 97 | 98 | org.springframework.boot 99 | spring-boot-starter-test 100 | test 101 | 102 | 103 | junit 104 | junit 105 | test 106 | 107 | ``` 108 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.code 5 | generation 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 1.4.0.RELEASE 11 | 12 | 13 | war 14 | 1.0 15 | Auto Code Generation 16 | http://maven.apache.org 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | org.mybatis.spring.boot 26 | mybatis-spring-boot-starter 27 | 1.1.1 28 | 29 | 30 | 31 | mysql 32 | mysql-connector-java 33 | 34 | 35 | com.alibaba 36 | fastjson 37 | 1.2.12 38 | 39 | 40 | org.apache.commons 41 | commons-lang3 42 | 3.4 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-test 48 | test 49 | 50 | 51 | junit 52 | junit 53 | test 54 | 55 | 56 | 57 | 58 | code-generation 59 | 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-maven-plugin 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/common/ConfigConstants.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.common; 2 | 3 | public interface ConfigConstants { 4 | /**定义Schema*/ 5 | String SCHEMA = "web-system"; 6 | 7 | /**移除表前缀*/ 8 | String REMOVE_TABLE_PREFIX = "t_"; 9 | 10 | /**Domain后缀, CustomerInfo*/ 11 | String DOMAIN_SUFFIX = ""; 12 | 13 | /**生成文件路径*/ 14 | String FILE_PATH = "E:\\gitwork\\code-generation\\src\\main\\java"; 15 | 16 | /**MyBatis SQL生成文件路径*/ 17 | String SQL_PATH = "E:\\gitwork\\code-generation\\src\\main\\resources\\mappers"; 18 | 19 | /**生成Mapper.xml后缀名字, EX: customer-mapper.xml*/ 20 | String SQL_MAPPER_SUFFIX = "-mapper"; 21 | 22 | /**生成包名称*/ 23 | String ROOT_PACKAGE = "com.lance"; 24 | 25 | /**JavaBean包名称*/ 26 | String DOMAIN_PACKAGE = "domain"; 27 | 28 | /**service包名称*/ 29 | String SERVICE_PACKAGE = "service"; 30 | 31 | /**serviceImpl包名称*/ 32 | String SERVICE_impl_PACKAGE = "serviceImpl"; 33 | 34 | /**mapper包名称*/ 35 | String MAPPER_PACKAGE = "mapper"; 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/common/FileUtils.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.common; 2 | 3 | public class FileUtils { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/common/InterfaceMethod.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.common; 2 | 3 | import java.util.Date; 4 | 5 | import org.apache.commons.lang3.time.DateFormatUtils; 6 | 7 | import com.lance.code.generation.domain.TableInfo; 8 | 9 | public final class InterfaceMethod { 10 | 11 | /** 12 | * Save Method 13 | * @return 14 | * 2016年8月24日上午10:00:59 15 | */ 16 | public static String mapperSave(TableInfo info) { 17 | StringBuilder builder = new StringBuilder(); 18 | //实体类注释 19 | builder.append(KeyWords.Tab).append("/**") 20 | .append(KeyWords.NEWLINE) 21 | .append(KeyWords.Tab).append("* 保存对象") 22 | .append(KeyWords.NEWLINE) 23 | .append(KeyWords.Tab).append("* ").append("@author Lance") 24 | .append(KeyWords.NEWLINE) 25 | .append(KeyWords.Tab).append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 26 | .append(KeyWords.NEWLINE) 27 | .append(KeyWords.Tab).append("*/") 28 | .append(KeyWords.NEWLINE) 29 | .append(KeyWords.Tab).append("int save(") 30 | .append(JavaBeanHandler.domainClassName(info.getTableName())) 31 | .append(" info);") 32 | .append(KeyWords.NEWLINE); 33 | 34 | return builder.toString(); 35 | } 36 | 37 | /** 38 | * Update Method 39 | * @return 40 | * 2016年8月24日上午10:00:59 41 | */ 42 | public static String mapperUpdate(TableInfo info) { 43 | StringBuilder builder = new StringBuilder(); 44 | //实体类注释 45 | builder.append(KeyWords.Tab).append("/**") 46 | .append(KeyWords.NEWLINE) 47 | .append(KeyWords.Tab).append("* 修改对象") 48 | .append(KeyWords.NEWLINE) 49 | .append(KeyWords.Tab).append("* ").append("@author Lance") 50 | .append(KeyWords.NEWLINE) 51 | .append(KeyWords.Tab).append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 52 | .append(KeyWords.NEWLINE) 53 | .append(KeyWords.Tab).append("*/") 54 | .append(KeyWords.NEWLINE) 55 | .append(KeyWords.Tab).append("int update(") 56 | .append(JavaBeanHandler.domainClassName(info.getTableName())) 57 | .append(" info);") 58 | .append(KeyWords.NEWLINE); 59 | 60 | return builder.toString(); 61 | } 62 | 63 | /** 64 | * Delete Method 65 | * @return 66 | * 2016年8月24日上午10:00:59 67 | */ 68 | public static String mapperDelete() { 69 | StringBuilder builder = new StringBuilder(); 70 | //实体类注释 71 | builder.append(KeyWords.Tab).append("/**") 72 | .append(KeyWords.NEWLINE) 73 | .append(KeyWords.Tab).append("* 删除对象") 74 | .append(KeyWords.NEWLINE) 75 | .append(KeyWords.Tab).append("* ").append("@author Lance") 76 | .append(KeyWords.NEWLINE) 77 | .append(KeyWords.Tab).append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 78 | .append(KeyWords.NEWLINE) 79 | .append(KeyWords.Tab).append("*/") 80 | .append(KeyWords.NEWLINE) 81 | .append(KeyWords.Tab).append("int delete(int id);") 82 | .append(KeyWords.NEWLINE); 83 | 84 | return builder.toString(); 85 | } 86 | 87 | /** 88 | * Delete Method 89 | * @return 90 | * 2016年8月24日上午10:00:59 91 | */ 92 | public static String mapperFindOne(TableInfo info) { 93 | StringBuilder builder = new StringBuilder(); 94 | //实体类注释 95 | builder.append(KeyWords.Tab).append("/**") 96 | .append(KeyWords.NEWLINE) 97 | .append(KeyWords.Tab).append("* 根据ID查询对象") 98 | .append(KeyWords.NEWLINE) 99 | .append(KeyWords.Tab).append("* ").append("@author Lance") 100 | .append(KeyWords.NEWLINE) 101 | .append(KeyWords.Tab).append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 102 | .append(KeyWords.NEWLINE) 103 | .append(KeyWords.Tab).append("*/") 104 | .append(KeyWords.NEWLINE) 105 | .append(KeyWords.Tab).append(JavaBeanHandler.domainClassName(info.getTableName())).append(" findOne(int id);") 106 | .append(KeyWords.NEWLINE); 107 | 108 | return builder.toString(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/common/JavaBeanHandler.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.common; 2 | 3 | import java.util.Date; 4 | import java.util.HashSet; 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | import org.apache.commons.lang3.RandomUtils; 9 | import org.apache.commons.lang3.StringUtils; 10 | import org.apache.commons.lang3.time.DateFormatUtils; 11 | 12 | import com.lance.code.generation.domain.ColumnInfo; 13 | import com.lance.code.generation.domain.TableInfo; 14 | 15 | public class JavaBeanHandler { 16 | 17 | public static String domainPath(){ 18 | StringBuilder builder = new StringBuilder(ConfigConstants.FILE_PATH); 19 | return builder.append("\\").append(domainPackagePath()).toString(); 20 | } 21 | 22 | public static String domainPackage(){ 23 | StringBuilder builder = new StringBuilder(ConfigConstants.ROOT_PACKAGE); 24 | return builder.append(KeyWords.DOT).append(ConfigConstants.DOMAIN_PACKAGE).toString(); 25 | } 26 | 27 | public static String domainPackagePath(){ 28 | String path = domainPackage(); 29 | return StringUtils.replace(path, ".", "\\"); 30 | } 31 | 32 | /** 33 | * 生成包文件头 Ex:package com.lance.code.generation.common 34 | * 2016年8月16日下午2:18:16 35 | */ 36 | public static String domainPackageHeader(){ 37 | StringBuilder builder = new StringBuilder(KeyWords.PACKAGE); 38 | return builder.append(KeyWords.SPACE).append(domainPackage()).toString(); 39 | } 40 | 41 | /** 42 | * 创建Domain 43 | * @param info 44 | * @param columns 45 | * 2016年8月16日下午2:46:00 46 | */ 47 | public static String createDomain(TableInfo info, List columns) { 48 | StringBuilder builder = new StringBuilder(domainPackageHeader()); 49 | builder.append(KeyWords.SEMICOLON) 50 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 51 | 52 | //导入包 53 | .append(KeyWords.IMPORT).append(KeyWords.SPACE).append("java.io.Serializable").append(KeyWords.SEMICOLON) 54 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE); 55 | 56 | //导入资源包 57 | Set set = importPackage(columns); 58 | if(!set.isEmpty()) { 59 | for(String p: set) { 60 | builder.append(KeyWords.IMPORT).append(KeyWords.SPACE).append(p).append(KeyWords.SEMICOLON) 61 | .append(KeyWords.NEWLINE); 62 | } 63 | } 64 | builder.append(KeyWords.NEWLINE); 65 | 66 | //实体类注释 67 | builder.append("/**") 68 | .append(KeyWords.NEWLINE) 69 | .append("* ").append(info.getTableComment()) 70 | .append(KeyWords.NEWLINE) 71 | .append("* ").append("@author Lance") 72 | .append(KeyWords.NEWLINE) 73 | .append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 74 | .append(KeyWords.NEWLINE) 75 | .append("*/") 76 | .append(KeyWords.NEWLINE) 77 | 78 | //生成public class A implements b 79 | .append(KeyWords.PUBLIC).append(KeyWords.SPACE).append(KeyWords.CLASS) 80 | .append(KeyWords.SPACE).append(domainClassName(info.getTableName())) 81 | .append(KeyWords.SPACE).append(KeyWords.IMPLEMENTS).append(KeyWords.SPACE) 82 | .append(KeyWords.Serial).append(KeyWords.SPACE) 83 | .append("{").append(KeyWords.NEWLINE) 84 | 85 | //serialVersionUID 86 | .append(KeyWords.Tab) 87 | .append("private static final long serialVersionUID = ") 88 | .append(RandomUtils.nextInt(1, Integer.MAX_VALUE)).append("L") 89 | .append(KeyWords.SEMICOLON) 90 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE); 91 | 92 | //属性 93 | for(ColumnInfo c: columns) { 94 | builder.append(KeyWords.Tab) 95 | .append("/**").append(c.getColumnComment()).append("*/") 96 | .append(KeyWords.NEWLINE).append(KeyWords.Tab) 97 | .append(KeyWords.PRIVATE).append(KeyWords.SPACE) 98 | .append(changeType(c.getDataType())).append(KeyWords.SPACE) 99 | .append(attrName(c.getColumnName(), false)).append(KeyWords.SEMICOLON) 100 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE); 101 | } 102 | 103 | //GET/SET 104 | for(ColumnInfo c: columns) { 105 | builder.append(KeyWords.Tab) 106 | .append(KeyWords.PUBLIC).append(KeyWords.SPACE) 107 | .append(changeType(c.getDataType())).append(KeyWords.SPACE) 108 | .append(KeyWords.GET).append(attrName(c.getColumnName(), true)).append("() {") 109 | .append(KeyWords.NEWLINE).append(KeyWords.Tab).append(KeyWords.Tab) 110 | .append(KeyWords.RETURN).append(attrName(c.getColumnName(), false)).append(KeyWords.SEMICOLON) 111 | .append(KeyWords.NEWLINE) 112 | .append(KeyWords.Tab).append("}") 113 | .append(KeyWords.NEWLINE) 114 | .append(KeyWords.NEWLINE); 115 | 116 | builder.append(KeyWords.Tab) 117 | .append(KeyWords.PUBLIC).append(KeyWords.SPACE).append(KeyWords.VOID) 118 | .append(KeyWords.SET).append(attrName(c.getColumnName(), true)).append("(") 119 | .append(changeType(c.getDataType())).append(KeyWords.SPACE).append(attrName(c.getColumnName(), false)).append(") {") 120 | .append(KeyWords.NEWLINE).append(KeyWords.Tab).append(KeyWords.Tab) 121 | .append(KeyWords.THIS).append(KeyWords.DOT).append(attrName(c.getColumnName(), false)).append(KeyWords.EQUAL) 122 | .append(attrName(c.getColumnName(), false)).append(KeyWords.SEMICOLON) 123 | .append(KeyWords.NEWLINE) 124 | .append(KeyWords.Tab).append("}") 125 | .append(KeyWords.NEWLINE) 126 | .append(KeyWords.NEWLINE); 127 | } 128 | 129 | builder.append("}"); 130 | 131 | return builder.toString(); 132 | } 133 | 134 | //------------------------------------生成Mapper----------------------------------------- 135 | public static String mapperPackage(){ 136 | StringBuilder builder = new StringBuilder(ConfigConstants.ROOT_PACKAGE); 137 | return builder.append(KeyWords.DOT).append(ConfigConstants.MAPPER_PACKAGE).toString(); 138 | } 139 | 140 | public static String mapperPackagePath(){ 141 | String path = mapperPackage(); 142 | return StringUtils.replace(path, ".", "\\"); 143 | } 144 | 145 | public static String mapperPath(){ 146 | StringBuilder builder = new StringBuilder(ConfigConstants.FILE_PATH); 147 | return builder.append("\\").append(mapperPackagePath()).toString(); 148 | } 149 | 150 | public static String mapperPackageHeader(){ 151 | StringBuilder builder = new StringBuilder(KeyWords.PACKAGE); 152 | return builder.append(KeyWords.SPACE).append(mapperPackage()).toString(); 153 | } 154 | 155 | /** 156 | * 创建Mapper 157 | * @param info 158 | * 2016年8月16日下午2:46:00 159 | */ 160 | public static String createMapper(TableInfo info) { 161 | StringBuilder builder = new StringBuilder(mapperPackageHeader()); 162 | builder.append(KeyWords.SEMICOLON) 163 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 164 | .append(KeyWords.IMPORT).append(KeyWords.SPACE) 165 | .append(domainPackage()).append(KeyWords.DOT).append(domainClassName(info.getTableName())) 166 | .append(KeyWords.SEMICOLON) 167 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 168 | 169 | //实体类注释 170 | .append("/**") 171 | .append(KeyWords.NEWLINE) 172 | .append("* ").append(info.getTableComment()) 173 | .append(KeyWords.NEWLINE) 174 | .append("* ").append("@author Lance") 175 | .append(KeyWords.NEWLINE) 176 | .append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 177 | .append(KeyWords.NEWLINE) 178 | .append("*/") 179 | .append(KeyWords.NEWLINE) 180 | 181 | //生成public interface AMapper 182 | .append(KeyWords.PUBLIC).append(KeyWords.SPACE).append(KeyWords.INTERFACE) 183 | .append(KeyWords.SPACE).append(className(info.getTableName(), ConfigConstants.MAPPER_PACKAGE)) 184 | .append(KeyWords.SPACE).append("{") 185 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 186 | //保存方法 187 | .append(InterfaceMethod.mapperSave(info)) 188 | .append(KeyWords.NEWLINE) 189 | //修改对象 190 | .append(InterfaceMethod.mapperUpdate(info)) 191 | .append(KeyWords.NEWLINE) 192 | //删除对象 193 | .append(InterfaceMethod.mapperDelete()) 194 | .append(KeyWords.NEWLINE) 195 | //查询对象根据ID 196 | .append(InterfaceMethod.mapperFindOne(info)) 197 | 198 | .append("}"); 199 | return builder.toString(); 200 | } 201 | 202 | //------------------------------------生成XML----------------------------------------- 203 | public static String xmlPath(){ 204 | return ConfigConstants.SQL_PATH; 205 | } 206 | 207 | public static String createXMLMapper(TableInfo info, List columns) { 208 | StringBuilder builder = new StringBuilder(""); 209 | builder.append(KeyWords.NEWLINE) 210 | .append("") 211 | .append(KeyWords.NEWLINE) 212 | .append("") 214 | .append(KeyWords.NEWLINE) 215 | //加入EhcacheCache缓存, 可以后期替换掉RedisCache 216 | .append(KeyWords.Tab).append("") 217 | .append(KeyWords.NEWLINE) 218 | .append(KeyWords.Tab).append("") 219 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 220 | 221 | //Save对象 222 | .append(XMLMethod.xmlSave(info, columns)) 223 | .append(KeyWords.NEWLINE) 224 | 225 | //Update对象 226 | .append(XMLMethod.xmlUpdate(info, columns)) 227 | .append(KeyWords.NEWLINE) 228 | 229 | //删除对象 230 | .append(XMLMethod.xmlDelete(info)) 231 | .append(KeyWords.NEWLINE) 232 | 233 | //查询对象根据ID 234 | .append(XMLMethod.xmlFindOne(info)) 235 | .append(""); 236 | return builder.toString(); 237 | } 238 | 239 | //------------------------------------生成Service----------------------------------------- 240 | public static String servicePackage(){ 241 | StringBuilder builder = new StringBuilder(ConfigConstants.ROOT_PACKAGE); 242 | return builder.append(KeyWords.DOT).append(ConfigConstants.SERVICE_PACKAGE).toString(); 243 | } 244 | 245 | public static String servicePackagePath(){ 246 | String path = servicePackage(); 247 | return StringUtils.replace(path, ".", "\\"); 248 | } 249 | 250 | public static String servicePath(){ 251 | StringBuilder builder = new StringBuilder(ConfigConstants.FILE_PATH); 252 | return builder.append("\\").append(servicePackagePath()).toString(); 253 | } 254 | 255 | public static String servicePackageHeader(){ 256 | StringBuilder builder = new StringBuilder(KeyWords.PACKAGE); 257 | return builder.append(KeyWords.SPACE).append(servicePackage()).toString(); 258 | } 259 | 260 | /** 261 | * 创建Service 262 | * @param info 263 | * 2016年8月16日下午2:46:00 264 | */ 265 | public static String createService(TableInfo info) { 266 | StringBuilder builder = new StringBuilder(servicePackageHeader()); 267 | builder.append(KeyWords.SEMICOLON) 268 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 269 | 270 | //实体类注释 271 | .append("/**") 272 | .append(KeyWords.NEWLINE) 273 | .append("* ").append(info.getTableComment()) 274 | .append(KeyWords.NEWLINE) 275 | .append("* ").append("@author Lance") 276 | .append(KeyWords.NEWLINE) 277 | .append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 278 | .append(KeyWords.NEWLINE) 279 | .append("*/") 280 | .append(KeyWords.NEWLINE) 281 | 282 | //生成public interface AService 283 | .append(KeyWords.PUBLIC).append(KeyWords.SPACE).append(KeyWords.INTERFACE) 284 | .append(KeyWords.SPACE).append(className(info.getTableName(), ConfigConstants.SERVICE_PACKAGE)) 285 | .append(KeyWords.SPACE).append("{") 286 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 287 | 288 | .append("}"); 289 | return builder.toString(); 290 | } 291 | 292 | /** 293 | * 创建ServiceImpl 294 | * @param info 295 | * 2016年8月16日下午2:46:00 296 | */ 297 | public static String createServiceImpl(TableInfo info) { 298 | //Service导入的Mapper接口类 299 | String importMapper = className(info.getTableName(), ConfigConstants.MAPPER_PACKAGE); 300 | 301 | StringBuilder builder = new StringBuilder(servicePackageHeader()); 302 | builder.append(KeyWords.SEMICOLON) 303 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 304 | 305 | //导入包 306 | .append(KeyWords.IMPORT).append(KeyWords.SPACE).append("org.springframework.beans.factory.annotation.Autowired").append(KeyWords.SEMICOLON) 307 | .append(KeyWords.NEWLINE) 308 | .append(KeyWords.IMPORT).append(KeyWords.SPACE).append("org.springframework.stereotype.Service").append(KeyWords.SEMICOLON) 309 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 310 | 311 | .append(KeyWords.IMPORT).append(KeyWords.SPACE).append(mapperPackage()) 312 | .append(KeyWords.DOT).append(importMapper) 313 | .append(KeyWords.SEMICOLON) 314 | .append(KeyWords.NEWLINE).append(KeyWords.NEWLINE) 315 | 316 | //实体类注释 317 | .append("/**") 318 | .append(KeyWords.NEWLINE) 319 | .append("* ").append(info.getTableComment()) 320 | .append(KeyWords.NEWLINE) 321 | .append("* ").append("@author Lance") 322 | .append(KeyWords.NEWLINE) 323 | .append("* ").append("@since ").append(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")) 324 | .append(KeyWords.NEWLINE) 325 | .append("*/") 326 | .append(KeyWords.NEWLINE) 327 | .append("@Service") 328 | .append(KeyWords.NEWLINE) 329 | 330 | //生成public class AServiceImpl 331 | .append(KeyWords.PUBLIC).append(KeyWords.SPACE).append(KeyWords.CLASS) 332 | .append(KeyWords.SPACE).append(className(info.getTableName(), ConfigConstants.SERVICE_impl_PACKAGE)) 333 | .append(KeyWords.SPACE).append(KeyWords.IMPLEMENTS).append(KeyWords.SPACE) 334 | .append(className(info.getTableName(), ConfigConstants.SERVICE_PACKAGE)).append("{") 335 | .append(KeyWords.NEWLINE) 336 | .append(KeyWords.Tab) 337 | .append("@Autowired") 338 | .append(KeyWords.NEWLINE) 339 | .append(KeyWords.Tab) 340 | .append(KeyWords.PRIVATE).append(KeyWords.SPACE).append(importMapper) 341 | .append(KeyWords.SPACE).append(StringUtils.uncapitalize(importMapper)).append(KeyWords.SEMICOLON) 342 | .append(KeyWords.NEWLINE) 343 | .append(KeyWords.NEWLINE) 344 | .append("}"); 345 | return builder.toString(); 346 | } 347 | 348 | 349 | /**------------------------------------------------------------------------------ 350 | * 处理类名称 351 | * @param className 352 | * @return 353 | * 2016年8月16日下午2:42:20 354 | */ 355 | public static String domainClassName(String className){ 356 | className = StringUtils.substringAfter(className, ConfigConstants.REMOVE_TABLE_PREFIX); 357 | String[] _names = StringUtils.split(className, "_"); 358 | 359 | StringBuilder builder = new StringBuilder(); 360 | for(String name: _names){ 361 | builder.append(StringUtils.capitalize(name)); 362 | } 363 | return builder.append(StringUtils.capitalize(ConfigConstants.DOMAIN_SUFFIX)).toString(); 364 | } 365 | 366 | /** 367 | * 处理类名称 368 | * @param className 369 | * @param suffix 370 | * 2016年8月16日下午5:44:13 371 | */ 372 | public static String className(String className, String suffix){ 373 | className = StringUtils.substringAfter(className, ConfigConstants.REMOVE_TABLE_PREFIX); 374 | String[] _names = StringUtils.split(className, "_"); 375 | 376 | StringBuilder builder = new StringBuilder(); 377 | for(String name: _names){ 378 | builder.append(StringUtils.capitalize(name)); 379 | } 380 | return builder.append(StringUtils.capitalize(suffix)).toString(); 381 | } 382 | 383 | /** 384 | * 处理属性名字 385 | * @param attr 386 | * 2016年8月16日下午4:15:12 387 | */ 388 | public static String attrName(String attr, boolean isCapitalize){ 389 | String[] _attrs = StringUtils.split(attr, "_"); 390 | 391 | StringBuilder builder = new StringBuilder(); 392 | for(int i=0; i<_attrs.length; i++){ 393 | if(i == 0 && !isCapitalize) { 394 | builder.append(_attrs[0]); 395 | }else { 396 | builder.append(StringUtils.capitalize(_attrs[i])); 397 | } 398 | } 399 | return builder.toString(); 400 | } 401 | 402 | /** 403 | * 转化类型 404 | * @param type 405 | * @return 406 | * 2016年8月16日下午4:29:29 407 | */ 408 | public static String changeType(String type){ 409 | switch (type.toLowerCase()) { 410 | case "date": ; 411 | case "timestamp": ; 412 | case "datetime": return "Date"; 413 | case "decimal": return "BigDecimal"; 414 | case "text": ; 415 | case "varchar": return "String"; 416 | case "tinyint": return "int"; 417 | } 418 | return type; 419 | } 420 | 421 | public static Set importPackage(List columns){ 422 | Set set = new HashSet<>(); 423 | for(ColumnInfo c: columns) { 424 | switch (c.getDataType().toLowerCase()) { 425 | case "datetime": set.add("java.util.Date"); break; 426 | case "decimal": set.add("java.math.BigDecimal"); break; 427 | } 428 | } 429 | return set; 430 | } 431 | } 432 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/common/KeyWords.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.common; 2 | 3 | public interface KeyWords { 4 | /**package关键字*/ 5 | String PACKAGE = "package"; 6 | 7 | /**interface关键字*/ 8 | String INTERFACE = "interface"; 9 | 10 | /**class关键字*/ 11 | String CLASS = "class"; 12 | 13 | /**public关键字*/ 14 | String PUBLIC = "public"; 15 | 16 | /**private关键字*/ 17 | String PRIVATE = "private"; 18 | 19 | /**implements关键字*/ 20 | String IMPLEMENTS = "implements"; 21 | 22 | /**Serializable关键字*/ 23 | String Serial = "Serializable"; 24 | 25 | /**import关键字*/ 26 | String IMPORT = "import"; 27 | 28 | /**get关键字*/ 29 | String GET = "get"; 30 | 31 | /**set关键字*/ 32 | String SET = "set"; 33 | 34 | /**return关键字*/ 35 | String RETURN = "return "; 36 | 37 | /**void关键字*/ 38 | String VOID = "void "; 39 | 40 | /**this关键字*/ 41 | String THIS = "this"; 42 | 43 | /**=关键字*/ 44 | String EQUAL = " = "; 45 | 46 | /**空格*/ 47 | String SPACE = " "; 48 | 49 | /**点号*/ 50 | String DOT = "."; 51 | 52 | /**分号*/ 53 | String SEMICOLON = ";"; 54 | 55 | /**换行*/ 56 | String NEWLINE = "\r\n"; 57 | 58 | /**Tab符号*/ 59 | String Tab = "\t"; 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/common/XMLMethod.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.common; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | import com.lance.code.generation.domain.ColumnInfo; 8 | import com.lance.code.generation.domain.TableInfo; 9 | 10 | public final class XMLMethod { 11 | 12 | /** 13 | * XML Save 14 | * @param info 15 | * @return 16 | * 2016年8月24日上午10:43:56 17 | */ 18 | public static String xmlFindOne(TableInfo info) { 19 | StringBuilder builder = new StringBuilder(); 20 | builder.append(KeyWords.Tab).append("").append(KeyWords.NEWLINE) 21 | .append(KeyWords.Tab).append("") 31 | .append(KeyWords.NEWLINE); 32 | return builder.toString(); 33 | } 34 | 35 | /** 36 | * XML Delete 37 | * @param info 38 | * @return 39 | * 2016年8月24日上午10:43:56 40 | */ 41 | public static String xmlDelete(TableInfo info) { 42 | StringBuilder builder = new StringBuilder(); 43 | builder.append(KeyWords.Tab).append("").append(KeyWords.NEWLINE) 44 | .append(KeyWords.Tab).append("") 45 | .append(KeyWords.NEWLINE) 46 | .append(KeyWords.Tab).append(KeyWords.Tab) 47 | .append("delete from ") 48 | .append(info.getTableName()) 49 | .append(" where id=#{value}") 50 | .append(KeyWords.NEWLINE) 51 | .append(KeyWords.Tab).append("") 52 | .append(KeyWords.NEWLINE); 53 | return builder.toString(); 54 | } 55 | 56 | /** 57 | * XML save 58 | * @param info 59 | * @return 60 | * 2016年8月24日上午10:43:56 61 | */ 62 | public static String xmlSave(TableInfo info, List columns) { 63 | StringBuilder builder = new StringBuilder(); 64 | builder.append(KeyWords.Tab).append("").append(KeyWords.NEWLINE) 65 | .append(KeyWords.Tab).append("") 68 | .append(KeyWords.NEWLINE) 69 | .append(KeyWords.Tab).append(KeyWords.Tab) 70 | .append("insert into ") 71 | .append(info.getTableName()) 72 | .append("("); 73 | for(ColumnInfo column: columns) { 74 | if(!column.isPrimaryKey()) { 75 | builder.append(column.getColumnName()).append(","); 76 | } 77 | } 78 | builder.append(")") 79 | .append(KeyWords.NEWLINE) 80 | .append(KeyWords.Tab).append(KeyWords.Tab) 81 | .append("values(").append(KeyWords.NEWLINE) 82 | .append(KeyWords.Tab).append(KeyWords.Tab); 83 | for(ColumnInfo column: columns) { 84 | if(!column.isPrimaryKey()) { 85 | builder.append("#{").append(JavaBeanHandler.attrName(column.getColumnName(), false)).append("},"); 86 | } 87 | } 88 | 89 | builder.append(")").append(KeyWords.NEWLINE) 90 | .append(KeyWords.Tab).append("") 91 | .append(KeyWords.NEWLINE); 92 | return StringUtils.replace(builder.toString(), ",)", ")"); 93 | } 94 | 95 | /** 96 | * 获取主键 97 | * @param columns 98 | * @since 2016年11月2日下午1:41:43 99 | */ 100 | private static String getPrimaryKey(List columns){ 101 | for(ColumnInfo column: columns) { 102 | if(column.isPrimaryKey()) { 103 | return "useGeneratedKeys=\"true\" keyProperty=\""+JavaBeanHandler.attrName(column.getColumnName(), false)+"\""; 104 | } 105 | } 106 | return ""; 107 | } 108 | 109 | /** 110 | * XML update.sql 111 | * @param info 112 | * @param columns 113 | * @return 114 | * 2016年8月24日下午1:12:49 115 | */ 116 | public static String xmlUpdate(TableInfo info, List columns) { 117 | StringBuilder builder = new StringBuilder(); 118 | builder.append(KeyWords.Tab).append("").append(KeyWords.NEWLINE) 119 | .append(KeyWords.Tab).append("") 122 | .append(KeyWords.NEWLINE) 123 | .append(KeyWords.Tab).append(KeyWords.Tab) 124 | .append("update ").append(info.getTableName()).append(" set "); 125 | 126 | for(ColumnInfo column: columns) { 127 | if(!StringUtils.equalsIgnoreCase(column.getColumnName(), "id")) { 128 | builder.append(KeyWords.NEWLINE).append(KeyWords.Tab).append(KeyWords.Tab).append(column.getColumnName()).append(" = ") 129 | .append("#{").append(JavaBeanHandler.attrName(column.getColumnName(), false)).append("},"); 130 | } 131 | } 132 | 133 | builder.append(" where id=#{id}") 134 | .append(KeyWords.NEWLINE) 135 | .append(KeyWords.Tab).append("") 136 | .append(KeyWords.NEWLINE); 137 | return StringUtils.replace(builder.toString(), ", where", " where"); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/domain/ColumnInfo.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | /** 8 | * 表列字段名称 9 | * @author Administrator 10 | */ 11 | public class ColumnInfo implements Serializable{ 12 | private static final long serialVersionUID = -80438187908217823L; 13 | 14 | private String columnName; 15 | private String columnComment; 16 | private String dataType; 17 | /**PRI 主键*/ 18 | private String columnKey; 19 | 20 | private boolean isPrimaryKey = false; 21 | 22 | public String getColumnName() { 23 | return columnName; 24 | } 25 | public void setColumnName(String columnName) { 26 | this.columnName = columnName; 27 | } 28 | public String getColumnComment() { 29 | return columnComment; 30 | } 31 | public void setColumnComment(String columnComment) { 32 | this.columnComment = columnComment; 33 | } 34 | public String getDataType() { 35 | return dataType; 36 | } 37 | public void setDataType(String dataType) { 38 | this.dataType = dataType; 39 | } 40 | public String getColumnKey() { 41 | return columnKey; 42 | } 43 | public void setColumnKey(String columnKey) { 44 | this.columnKey = columnKey; 45 | } 46 | public boolean isPrimaryKey() { 47 | if(StringUtils.equalsIgnoreCase(columnKey, "PRI")){ 48 | isPrimaryKey = true; 49 | } 50 | return isPrimaryKey; 51 | } 52 | public void setPrimaryKey(boolean isPrimaryKey) { 53 | this.isPrimaryKey = isPrimaryKey; 54 | } 55 | } -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/domain/TableInfo.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | public class TableInfo implements Serializable{ 6 | private static final long serialVersionUID = -7812734394886606427L; 7 | 8 | private String tableName; 9 | private String tableComment; 10 | 11 | public String getTableName() { 12 | return tableName; 13 | } 14 | public void setTableName(String tableName) { 15 | this.tableName = tableName; 16 | } 17 | public String getTableComment() { 18 | return tableComment; 19 | } 20 | public void setTableComment(String tableComment) { 21 | this.tableComment = tableComment; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/mapper/TablesMapper.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.mapper; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | 7 | import com.lance.code.generation.domain.ColumnInfo; 8 | import com.lance.code.generation.domain.TableInfo; 9 | 10 | public interface TablesMapper { 11 | 12 | /** 13 | * 根据Schema查询所有表信息 14 | * @param schema 15 | * 2016年8月16日上午9:44:53 16 | */ 17 | List findAll(String schema); 18 | 19 | /** 20 | * 根据表获取表字段 21 | * @param tableName 22 | * 2016年8月16日下午1:44:29 23 | */ 24 | List findColumn(@Param("tableName")String tableName, @Param("schemaName")String schemaName); 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/service/TableService.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.service; 2 | 3 | import java.util.List; 4 | 5 | import com.lance.code.generation.domain.TableInfo; 6 | 7 | public interface TableService { 8 | 9 | /** 10 | * 根据Schema查询所有表信息 11 | * @param schema 12 | * @return 13 | * 2016年8月16日上午9:44:53 14 | */ 15 | List findAll(String schema); 16 | 17 | void run(); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/service/TableServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.service; 2 | 3 | import java.io.BufferedWriter; 4 | import java.io.File; 5 | import java.io.IOException; 6 | import java.nio.charset.Charset; 7 | import java.nio.file.Files; 8 | import java.nio.file.Path; 9 | import java.nio.file.Paths; 10 | import java.util.List; 11 | 12 | import org.slf4j.Logger; 13 | import org.slf4j.LoggerFactory; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.stereotype.Service; 16 | 17 | import com.lance.code.generation.common.ConfigConstants; 18 | import com.lance.code.generation.common.JavaBeanHandler; 19 | import com.lance.code.generation.domain.ColumnInfo; 20 | import com.lance.code.generation.domain.TableInfo; 21 | import com.lance.code.generation.mapper.TablesMapper; 22 | 23 | @Service 24 | public class TableServiceImpl implements TableService { 25 | Logger logger = LoggerFactory.getLogger(getClass()); 26 | @Autowired 27 | private TablesMapper tablesMapper; 28 | 29 | @Override 30 | public List findAll(String schema) { 31 | return tablesMapper.findAll(schema); 32 | } 33 | 34 | public List findColumn(String tableName, String schema) { 35 | return tablesMapper.findColumn(tableName, schema); 36 | } 37 | 38 | public void run() { 39 | List list = findAll(ConfigConstants.SCHEMA); 40 | for(TableInfo info: list) { 41 | logger.info("tableName: {}, tableComment:{}", info.getTableName(), info.getTableComment()); 42 | handlerTableColumn(info); 43 | } 44 | } 45 | 46 | /** 47 | * 处理表字段 48 | * @param info 49 | * 2016年8月16日下午1:47:28 50 | */ 51 | private void handlerTableColumn(TableInfo info) { 52 | List columns = findColumn(info.getTableName(), ConfigConstants.SCHEMA); 53 | 54 | //创建Domain 55 | String domain = JavaBeanHandler.createDomain(info, columns); 56 | writeFile(JavaBeanHandler.domainPath(), JavaBeanHandler.domainClassName(info.getTableName())+".java", domain); 57 | 58 | //创建Mapper 59 | String mapper = JavaBeanHandler.createMapper(info); 60 | writeFile(JavaBeanHandler.mapperPath(), JavaBeanHandler.className(info.getTableName(), ConfigConstants.MAPPER_PACKAGE)+".java", mapper); 61 | 62 | //创建Mapper.xml 63 | String xmlMapper = JavaBeanHandler.createXMLMapper(info, columns); 64 | writeFile(JavaBeanHandler.xmlPath(), JavaBeanHandler.className(info.getTableName(), ConfigConstants.SQL_MAPPER_SUFFIX)+".xml", xmlMapper); 65 | 66 | //创建接口service 67 | String service = JavaBeanHandler.createService(info); 68 | writeFile(JavaBeanHandler.servicePath(), JavaBeanHandler.className(info.getTableName(), ConfigConstants.SERVICE_PACKAGE)+".java", service); 69 | 70 | //创建接口serviceImpl 71 | String serviceImpl = JavaBeanHandler.createServiceImpl(info); 72 | writeFile(JavaBeanHandler.servicePath(), JavaBeanHandler.className(info.getTableName(), ConfigConstants.SERVICE_impl_PACKAGE)+".java", serviceImpl); 73 | } 74 | 75 | /** 76 | * 写文件 77 | * @param dir 78 | * @param fileName 79 | * @param content 80 | * 2016年8月16日下午2:50:55 81 | */ 82 | private void writeFile(String dir, String fileName, String content){ 83 | File dic = new File(dir); 84 | if(!dic.exists()){ 85 | dic.mkdirs(); 86 | } 87 | 88 | try { 89 | Path file = Paths.get(dir+"/"+fileName); 90 | Files.deleteIfExists(file); 91 | file = Files.createFile(file); 92 | 93 | BufferedWriter writer = Files.newBufferedWriter(file, Charset.forName("utf-8")); 94 | writer.write(content); 95 | writer.flush(); 96 | } catch (IOException e) { 97 | e.printStackTrace(); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/com/lance/code/generation/web/SimpleApplication.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.web; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication(scanBasePackages="com.lance.code.generation") 8 | @MapperScan("com.lance.code.generation.mapper") 9 | public class SimpleApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(SimpleApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # IDENTITY (ContextIdApplicationContextInitializer) 2 | spring.application.index=Code.v1.1 3 | spring.application.name=Code Boot 4 | 5 | #MYBATIS 6 | mybatis.type-aliases-package=com.lance.code.generation.domain 7 | mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml 8 | mybatis.configuration.map-underscore-to-camel-case=true 9 | mybatis.configuration.use-generated-keys=true 10 | mybatis.configuration.default-fetch-size=100 11 | mybatis.configuration.default-statement-timeout=30 12 | 13 | spring.datasource.url=jdbc:mysql://localhost:3306/web-system 14 | spring.datasource.username=root 15 | spring.datasource.password=123456 16 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /src/main/resources/mapper/tablesMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 16 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /src/test/java/com/lance/code/generation/service/MainTest.java: -------------------------------------------------------------------------------- 1 | package com.lance.code.generation.service; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | 11 | import com.lance.code.generation.web.SimpleApplication; 12 | 13 | @RunWith(SpringRunner.class) 14 | @SpringBootTest(classes=SimpleApplication.class) 15 | public class MainTest { 16 | Logger logger = LoggerFactory.getLogger(getClass()); 17 | @Autowired 18 | private TableService tableService; 19 | 20 | @Test 21 | public void main() { 22 | long startTime = System.currentTimeMillis(); 23 | logger.info("...........start application........."); 24 | 25 | tableService.run(); 26 | 27 | logger.info("...end application...Time: {}",(System.currentTimeMillis()-startTime)); 28 | } 29 | } 30 | --------------------------------------------------------------------------------