- 引入maven依赖
<?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.myutil.test</groupId> <artifactId>MyUtils</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.16</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.3</version> </dependency> </dependencies> <build> <resources> <resource> <directory>${basedir}/src/main/resources</directory> </resource> <resource> <directory>${basedir}/src/main/java/</directory> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <argLine>-Dfile.encoding=UTF-8</argLine> <skipTests>true</skipTests> <excludes> </excludes> </configuration> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <skip>false</skip> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> </plugins> </build> </project>
- 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> <!-- Class Driver Path --> <classPathEntry location="C:\Users\Administrator.USER-20170829XY\.m2\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/> <context id="context" targetRuntime="MyBatis3"> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="autoDelimitKeywords" value="true"/> <!--使生成的Model实现Serializable接口--> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <commentGenerator> <!-- This property is used to specify whether MBG will include any coments in the generated code --> <property name="suppressAllComments" value="false"/> <!-- This property is used to specify whether MBG will include the generation timestamp in the generated comments --> <property name="suppressDate" value="true"/> <!--model类加上数据库字段的注释--> <property name="addRemarkComments" value="true"/> </commentGenerator> <!--这个是数据库的连接url和用户名密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://xxx.xxx.xxxx:3306/sp2p?characterEncoding=UTF-8" userId="xxx" password="xxx"/> <javaTypeResolver> <!-- This property is used to specify whether MyBatis Generator should force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, rather than substituting integral types when possible --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--只需要修改 targetPackage java model定义的包名儿 --> <javaModelGenerator targetPackage="com.myutil.mybatis.model" targetProject="src/main/java"> <!-- This property is used to select whether MyBatis Generator will generate different Java packages for the objects based on the catalog and schema of the introspected table --> <property name="enableSubPackages" value="false"/> <!-- This property is used to select whether MyBatis Generator adds code to trim the white space from character fields returned from the database --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--只需要修改 targetPackage java sqlMap定义的包名儿 --> <sqlMapGenerator targetPackage="com.myutil.mybatis.mapper.xml" targetProject="src/main/java"> <!-- This property is used to select whether MyBatis Generator will generate different Java packages for the objects based on the catalog and schema of the introspected table --> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!--只需要修改 targetPackage 这个就是那个interface 包名儿 --> <!-- type字段是 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator targetPackage="com.myutil.mybatis.mapper" targetProject="src/main/java" type="MIXEDMAPPER"> <!-- This property is used to select whether MyBatis Generator will generate different Java packages for the objects based on the catalog and schema of the introspected table --> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--具体的表 domainObjectName 是你自己定义的文件名--> <table tableName="t_channel" domainObjectName="Channel" enableCountByExample="false" enableDeleteByPrimaryKey="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" modelType="flat"> <property name="useActualColumnNames" value="true"/> <property name="useCompoundPropertyNames" value="true"/> </table> </context> </generatorConfiguration>
- 运行
之后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate -e”
这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题。