mybatisplus https://github.com/lyloou/spring-master/tree/master/spring-mybatisplus
自动生成,包括 Controller、Mapper、Entity、Service、Mapper.xml
mybatis 自动生成工具
下载mybatis-generator-core-1.3.7.jar
1 2 https://github.com/mybatis/generator/releases https://github.com/mybatis/generator/releases/download/mybatis-generator-1.3.7/mybatis-generator-core-1.3.7.zip
1 2 https://jdbc.postgresql.org/download.html https://jdbc.postgresql.org/download/postgresql-42.2.2.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration > <classPathEntry location ="D:/c/mybatis/mybatis-generator-core-1.3.7/lib/postgresql-42.2.2.jar" /> <context id ="MysqlContext" targetRuntime ="MyBatis3Simple" > <plugin type ="org.mybatis.generator.plugins.SerializablePlugin" > </plugin > <jdbcConnection driverClass ="org.postgresql.Driver" connectionURL ="jdbc:postgresql://192.168.1.123:5432/users?currentSchema=hello& tcpKeepAlive=true" userId ="hello" password ="hello" /> <javaTypeResolver > <property name ="forceBigDecimals" value ="false" /> </javaTypeResolver > <javaModelGenerator targetPackage ="com.example.model.original" targetProject ="./src/main/java" > <property name ="enableSubPackages" value ="true" /> <property name ="trimStrings" value ="true" /> </javaModelGenerator > <javaClientGenerator type ="ANNOTATEDMAPPER" targetPackage ="com.example.repository.mapper" targetProject ="./src/main/java" > <property name ="rootInterface" value ="com.example.repository.mark.IMarkMapper" /> </javaClientGenerator > <commentGenerator > <property name ="suppressDate" value ="true" /> <property name ="suppressAllComments" value ="false" /> </commentGenerator > <table schema ="hello" tableName ="app_verify_info" /> <table schema ="hello" tableName ="api_interface_info" /> <table schema ="hello" tableName ="api_interface_access" /> <table schema ="hello" tableName ="app_code" /> </context > </generatorConfiguration >
1 2 3 @echo off java -jar mybatis-generator-core-1 .3 .2 .jar -configfile generatorConfig.xml pause
参考资料:
1 2 mysql-connector-java-5.1.37.jar
根据数据库表生成对应实体类(Lombok 的注解实现) 记得 IDEA 安装好插件:Lombok Plugin
文件pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <mybatis.generator.version > 1.3.7</mybatis.generator.version > <mysql.connector.java > 5.1.46</mysql.connector.java > <mybatis.generator.lombok.plugin > 1.0</mybatis.generator.lombok.plugin > </properties > <dependencies > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > 1.16.16</version > <type > jar</type > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > ${mysql.connector.java}</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.mybatis.generator</groupId > <artifactId > mybatis-generator-maven-plugin</artifactId > <version > ${mybatis.generator.version}</version > <configuration > <overwrite > true</overwrite > <configurationFile > src/main/resources/mybatis/generatorConfig.xml</configurationFile > </configuration > <dependencies > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > ${mysql.connector.java}</version > </dependency > <dependency > <groupId > com.softwareloop</groupId > <artifactId > mybatis-generator-lombok-plugin</artifactId > <version > ${mybatis.generator.lombok.plugin}</version > </dependency > </dependencies > </plugin > </plugins > </build >
文件src/main/resources/mybatis/generatorConfig.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration > <context id ="context1" targetRuntime ="MyBatis3Simple" defaultModelType ="flat" > <plugin type ="com.softwareloop.mybatis.generator.plugins.LombokPlugin" > <property name ="builder" value ="true" /> <property name ="builder.fluent" value ="true" /> <property name ="builder.builderMethodName" value ="myBuilder" /> <property name ="accessors" value ="true" /> <property name ="accessors.prefix" value ="m_, _" /> <property name ="allArgsConstructor" value ="false" /> </plugin > <commentGenerator > <property name ="suppressAllComments" value ="true" /> </commentGenerator > <jdbcConnection driverClass ="com.mysql.jdbc.Driver" connectionURL ="jdbc:mysql://127.0.0.1/lyloou?useUnicode=true& characterEncoding=utf8" userId ="root" password ="root" /> <javaModelGenerator targetPackage ="com.lyloou.orders.entity.task" targetProject ="src/main/java" > <property name ="trimStrings" value ="true" /> </javaModelGenerator > <table tableName ="task_bonus" domainObjectName ="TaskBonus" /> <table tableName ="task_bonus_coupon" domainObjectName ="TaskBonusCoupon" /> <table tableName ="task_bonus_user" domainObjectName ="TaskBonusUser" /> </context > </generatorConfiguration >
如下图所示点击生成 entity
如果数据库中的时间用的是 timestamp
,为了不损失精度,做一个【在文件夹中替换】的操作。 选中刚才的 task 文件夹,键入 Ctrl + Shift + R
替换 private Date
为 private java.sql.Timestamp