1.创建springboot +maven项目
pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.generator</groupId> <artifactId>generator-demo</artifactId> <version>1.0-SNAPSHOT</version> <name>generator-demo</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId> mysql</groupId> <artifactId> mysql-connector-java</artifactId> <version> 5.1.35</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <finalName>xxx</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> </project>
2.generactor.perproties文件:
jdbc.driverLocation=D:\\gaozhuan\\tools\\maven-lib\\mysql\\mysql-connector-java\\5.1.35\\mysql-connector-java-5.1.35.jar jdbc.driverClass=com.mysql.jdbc.Driver jdbc.connectionURL=jdbc:mysql://localhost:3306/user?characterEncoding=UTF-8&useUnicode=true&useSSL=false jdbc.userId=root jdbc.password=root
3.generatorConfig.xml:
<?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> <properties resource="generator.properties" /> <!--<classPathEntry location="D:\gaozhuan\tools\maven-lib\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" />--> <classPathEntry location="${jdbc.driverLocation}"/> <context id="Tables" targetRuntime="MyBatis3"> <!-- 注释 --> <commentGenerator> <!-- 是否生成注释代时间戳 --> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- JDBC连接 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <!--<jdbcConnection--> <!--driverClass="${spring.datasource.driver-class-name}"--> <!--connectionURL="jdbc:mysql://localhost:3306/user?characterEncoding=UTF-8&useUnicode=true&useSSL=false"--> <!--userId="root"--> <!--password="root">--> <!--</jdbcConnection>--> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.generator.pojo" targetProject="src/main/java"> <!-- 从数据库返回的值被清理前后的空格 --> <property name="trimStrings" value="true" /> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaModelGenerator> <!-- 生成mapper xml文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapper xml对应Client--> <javaClientGenerator targetPackage="com.generator.dao" targetProject="src/main/java" type="XMLMAPPER"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表信息 --> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <table schema="user" tableName="tb_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> </table> <!--<table schema="blog" tableName="article"--> <!--domainObjectName="Article" enableCountByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--enableUpdateByExample="false">--> <!--</table>--> <!--<table schema="blog" tableName="document"--> <!--domainObjectName="Document" enableCountByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--enableUpdateByExample="false">--> <!--</table>--> <!--<table schema="blog" tableName="tag"--> <!--domainObjectName="Tag" enableCountByExample="false"--> <!--enableDeleteByExample="false" enableSelectByExample="false"--> <!--enableUpdateByExample="false">--> <!--</table>--> </context> </generatorConfiguration>
4:目录结构:
5:运行步骤:
Run>Edit configreactions >
>
结果: