Maven环境下MyBatisGenerator 配置
- 插件导入; 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); } }