基于springboot2版本
pom基本依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 测试 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mysql 依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- springboot-web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
插件依赖
<build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <configurationFile>./src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>com.itfsw</groupId> <artifactId>mybatis-generator-plugin</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin> </plugins> </build>
在resourcu中新建这两个文件夹,和这两个文件
文件夹位置可以在pom中配置,在application.yml中配置
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="mybatis-generator/mybatisGeneratorinit.properties"/> <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 --> <!--<classPathEntry location="D:generator_mybatismysql-connector-java-5.1.24-bin.jar" /> --> <!-- 一个数据库一个context --> <!--defaultModelType="flat" 大数据字段,不分表 --> <context id="MysqlTables" targetRuntime="MyBatis3" defaultModelType="flat"> <property name="autoDelimitKeywords" value="true" /> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <property name="javaFileEncoding" value="utf-8" /> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!-- 批量插入插件 --> <plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/> <plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/> <plugin type="com.itfsw.mybatis.generator.plugins.BatchInsertPlugin"> <!-- 开启后可以实现官方插件根据属性是否为空决定是否插入该字段功能 !需开启allowMultiQueries=true多条sql提交操作,所以不建议使用!插件默认不开启 --> <property name="allowMultiQueries" value="false"/> </plugin> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 注释 --> <commentGenerator > <property name="suppressAllComments" value="true"/><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--> </commentGenerator> <!-- jdbc连接 --> <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" /> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com..model" targetProject="${project}" > <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="mappers" targetProject="${resources}" > <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com..mapper" targetProject="${project}" type="XMLMAPPER" > <property name="enableSubPackages" value="false" /> </javaClientGenerator> <table schema="%" tableName="weather" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" enableInsert="true" selectByPrimaryKeyQueryId="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"> <property name="useActualColumnNames" value="false" /> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> </context> </generatorConfiguration>
mybatisGeneratorinit.properties
#Mybatis Generator configuration project =src/main/java resources=src/main/resources jdbc_driver =com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://127.0.0.1:3306/test?useLegacyDatetimeCode=false&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=CST&tinyInt1isBit=false jdbc_user=root jdbc_password=root
applicatiom.yml
mybatis: mapper-locations: classpath*:mappers/*.xml
双击这个
支持覆盖,修改表