Mybatis 自动生成代码配置 -Spring + Maven 环境
2018-02-27 14:18
766 查看
Spring + Maven 环境下 Mybatis 自动生成代码配置
需要对应数据库的数据库驱动jar包
本文为
放置路径为
添加 generatorConfig.xml 文件到项目中,注意路径,本文为
generatorConfig.xml 内容,涉及数据库驱动,数据库连接,Mapper,实体,xml 文件生成的配置信息,生成文件包路径可以自动创建:
在
指定了 generatorConfig.xml 的路径
运行
允许一次生成多个表的对应代码,如果使用 Spring Boot 可以直接在项目上运行 Mybatis 自动生成代码的程序
完整的命令行是
需要对应数据库的数据库驱动jar包
本文为
mysql-connector-java-5.1.7-bin.jar。
放置路径为
C:/Users/Administrator/Desktop/mybatis-generator/mysql-connector-java-5.1.7-bin.jar。
添加 generatorConfig.xml 文件到项目中,注意路径,本文为
src/main/resources/generator/generatorConfig.xml
generatorConfig.xml 内容,涉及数据库驱动,数据库连接,Mapper,实体,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> <classPathEntry location="C:/Users/Administrator/Desktop/mybatis-generator/mysql-connector-java-5.1.7-bin.jar" /> <context id="context"> <!-- 注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--> <property name="addRemarkComments" value="true"/> </commentGenerator> <!-- JDBC连接 --> <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" driverClass="com.mysql.jdbc.Driver" password="root" userId="root"></jdbcConnection> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.bestcxx.stu.springmybatis.model" targetProject="src/main/java"> <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] --> <property name="enableSubPackages" value="false" /> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources"> <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] --> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.bestcxx.stu.springmybatis.dao" targetProject="src/main/java" type="XMLMAPPER"> <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] --> <property name="enableSubPackages" value="false" /> <!--<property name="rootInterface" value="cn.acsm.farmeasy.order.core.dao.BaseDAO"/> --> </javaClientGenerator> <!-- 配置表信息,这里没生成一张表,这里需要改变一次对应表名 --> <!-- 已经生成实体类的表:--> <!-- <table tableName="表名" domainObjectName="驼峰格式实体名" module="order" enableCountByExample="false" --> <table tableName="test2" domainObjectName="Test2" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <!-- 忽略列,不生成bean 字段 <ignoreColumn column="FRED" />--> <!-- 指定列的java数据类型 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />--> </table> </context> </generatorConfiguration>
在
pom.xml中添加 Maven 插件 配置
指定了 generatorConfig.xml 的路径
<build> ... <plugins> ... <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build>
运行
mybatis-generator:generate,生成代码
允许一次生成多个表的对应代码,如果使用 Spring Boot 可以直接在项目上运行 Mybatis 自动生成代码的程序
完整的命令行是
mvn:mybatis-generator:generate,这是一个 Maven 命令,如果直接在 Eclipse 中运行,使用
mybatis-generator:generate即可。
相关文章推荐
- eclipse maven 搭建 SSM(Spring+SpringMVC+MyBatis)开发环境 和 MyBatis 自动生成的 maven 插件配置
- 在maven环境下配置mybatis自动生成代码
- Intellij IDEA springboot maven 配置mybatis-generator,自动从数据库中生成代码
- mybatis-generator-maven-plugin插件自动生成代码的配置方法
- MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子
- SpringBoot学习-如何使用mybatis-generator-maven-plugin自动生成代码
- Mybatis 优缺点,配置,使用,结合Spring使用,常见错误,代码自动生成(草稿)
- SpringBoot系列: 使用MyBatis maven插件自动生成java代码
- spring boot配置mybatis代码自动生成(mybatis通用mapper相关基础代码自动生成)
- myBatis自动生成相关代码文件配置(Maven)
- idea 搭建 springMVC+mybatis+maven 项目(三)mybatis-generater自动生成MyBatis代码
- maven + MyBatis Generator 自动生成代码配置
- Mybatis3.x与Spring4.x整合+代码自动生成
- 使用Eclipse的Generator MyBatis/iBatis Artifacts插件自动生成代码,与spring集成
- maven项目使用mybatis-generator自动生成代码
- mybatis-generator生成代码配置maven插件
- Intellij IDEA 14中maven下生成利用mybatis-generator自动生成代码
- maven 使用插件mybatis-generator生成代码配置
- Mybatis最入门---代码自动生成(generatorConfig.xml配置)