- 外掛匯入; pom檔案引入mybatis-generator-maven-plugin
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!-- generator 工具配置檔案的位置 --> <configurationFile>${basedir}/src/main/resources/mybatisGenerator/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.16</version> </dependency> </dependencies> </plugin>
- 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="mybatisGenerator/mybatisGenerator.properties"/> <context id="lego" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <!-- 註釋 --> <commentGenerator > <!-- 是否取消註釋 --> <property name="suppressAllComments" value="false"/> <!-- 是否生成註釋代時間戳--> <property name="suppressDate" value="true" /> </commentGenerator> <!--資料庫連線資訊--> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <!--型別轉換--> <javaTypeResolver > <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成實體類地址 --> <javaModelGenerator targetPackage="${package.do}" targetProject="src/main/java"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--生成mapper--> <sqlMapGenerator targetPackage="${package.mapper}" targetProject="src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!--dao介面--> <javaClientGenerator type="XMLMAPPER" targetPackage="${package.dao}" targetProject="src/main/java"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table tableName="lego_user" domainObjectName="UserDO" > <!-- 資料庫表主鍵 --> <generatedKey column="id" sqlStatement="select last_insert_id()" identity="true" /> </table> </context> </generatorConfiguration>
- generatorConfig.properties 配置(解耦和 ,可忽略)
#mysql jdbc.driverClass=com.mysql.jdbc.Driver #lego jdbc.connectionURL=jdbc:mysql://localhost:3306/lego?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8 jdbc.userId=root jdbc.password=123456 #package package.do=com.zhangweinan.dao.objects package.dao=com.zhangweinan.dao package.mapper=mapper
SpringBoot環境下對映關係配置
- application.properties檔案新增配置
# mybatis mybatis.mapper-locations=classpath:mapper/*.xml
- SpringBoot Starter啟動類新增註解MapperScan
@SpringBootApplication @MapperScan("com.zhangweinan.dao") public class Starter { public static void main(String[] args) { SpringApplication.run(Starter.class, args); } }