1.maven pom.xml中(注意文件路徑): java
<build> <plugins> <!-- MyBatis代碼生成 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!-- 在控制檯打印執行日誌 --> <verbose>true</verbose> <!-- 重複生成時會覆蓋以前的文件--> <overwrite>false</overwrite> <configurationFile>src\main\resources\MybatisGenerator.xml</configurationFile> </configuration> </plugin> </plugins> </build>
2. MybatisGenerator.xmlmysql
<?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="jdbc.properties"/> <!--數據庫驅動包路徑 --> <classPathEntry location="${drive.class.path}"/> <context id="MySQLTables" targetRuntime="MyBatis3"> <!--關閉註釋 --> <commentGenerator> <property name="suppressDate" value="true"/> </commentGenerator> <!--數據庫鏈接信息 --> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"> </jdbcConnection> <!--生成的model 包路徑 --> <javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}"> <property name="enableSubPackages" value="ture"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成xml mapper文件 路徑 --> <sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}"> <property name="enableSubPackages" value="ture"/> </sqlMapGenerator> <!-- 生成的Dao接口 的包路徑 --> <javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}"> <property name="enableSubPackages" value="ture"/> </javaClientGenerator> <!--對應數據庫表名 --> <table tableName="info_user"></table> </context> </generatorConfiguration>
3.jdbc.propertiessql
#數據庫鏈接jar包位置 drive.class.path=C:\\Users\\Administrator\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.43\\mysql-connector-java-5.1.43.jar #數據庫基本信息 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/icity?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=abc123 #maven mybatis 逆向生成文件包路徑 model.package=com.demo.model dao.package=com.demo.dao xml.mapper.package=com.demo.dao.xml target.project=src/main/java
4.執行 run maven build數據庫
右鍵項目run --》 maven build --》在global中輸入 mybatis-generator:generator,--->run 便可mybatis