新项目要用mybatis,为了开发效率和方便开发,研究了mybatis-generate,在maven环境下,通过插件的形式配置,废话不多说。
pom文件:
<!-- MyBatis代码生成 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
<!-- 绑定到generate周期中,防止在其他生命周期中再次生成 -->
<phase>generate</phase>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqlserver4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
</dependencies>
</plugin>
由于M2e不支持这个goal,eclipse编辑器会标红报错,忽略这个错误就好了,具体原因请看:
https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html
解决办法把下面这段配置添加到与plugins平级目录中即可解决:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<versionRange>[1.3.2,)</versionRange>
<goals>
<goal>generate</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
mybatis-generator-maven-plugin会默认读取src/main/resources的generatorConfig.xml文件,
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>
<context id="sqlserver" targetRuntime="MyBatis3">
<!-- 抑制警告 -->
<property name="suppressTypeWarnings" value="true" />
<!-- generate entity时,生成hashcode和equals方法 -->
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<!-- generate entity时,生成serialVersionUID -->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<!-- 这个插件只会增加字符串字段映射到一个JDBC字符的方法 -->
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
<!-- genenat entity时,生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!-- 抑制生成代码的注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionURL="jdbc:sqlserver://localhost:1433;databaseName=spider"
userId="sa" password="123">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- generate Model -->
<javaModelGenerator targetPackage="com.spider.tickets.user.entity"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- generate xml -->
<sqlMapGenerator targetPackage="mybatis"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成dao接口的配置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.spider.tickets.user.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="ss_user" domainObjectName="User" enableCountByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" />
<table tableName="ss_task" domainObjectName="Task" enableCountByExample="false" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false" selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
通过命令:mybatis-generator:generate 即可生成我们所需的文件。
分享到:
相关推荐
可以设置开发工具的dtd配置,配置http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd指向本地项目mybatis-generator-core-1.3.2.jar里的mybatis-generator-config_1_0.dtd idea配置见附件
mybatis-generator配置详解:详细描述各XML标签的作用,便于快速上手使用。
mybatis-generator 自动生成代码配置,相关pom文件请自行下载,这里只做generator的配置,包含bean、xml、dao
包含mybatis-generator1.3.2的jar包、mysql数据库连接jar包、mybatis3.2.7.jar、自动生成代码的配置文件模板以及示例。
使用命令行直接执行,java -jar mybatis-generator-core-1.3.6....包含mybatis-generator-core-1.3.6.zip代码自动生成器官方最新版本的jar包、mysql数据库连接jar包、mybatis3.2.7.jar以及自动生成代码的配置文件模板。
利用mybatis-generator-core可以自动生成实体类、dao接口和mapping映射文件,里头也提供了使用说明: 1、进入lib文件夹里头。 2、修改generatorConfig.xml配置 主要修改数据库地址,用户名,密码,以及数据库名称,...
eclipse插件使用: 1 eclipse首先安装Mybatis Generator插件,目前最新版本为v1.3.2.201207161521 ...6 打开eclipse,在配置文件gencfg_T_XXX.xml上右键Generate Mybatis/iBatis Artifacts即可直接生成到对应的包下
把下载的jar包放在 Maven仓库的 org\mybatis\generator\mybatis-generator-core\1.3.5 下面即可,然后在generatorConfig配置文件中加上<plugin type="org.mybatis.generator.plugins.LombokPlugin" > </plugin> ...
改造过的mybatis-generator 带有中文注释,减少了配置文件的复杂度。
idea配置mybatis-generator步骤
使用Mybatis-Generator配置独立的文件,可以实现将数据库表生成对应的文件,并放在对应的包路径下,只需要运行generateSql.bat文件即可生成代码文件!
修改mybatis-generator源码实现生成后的实体类字段上方加上数据库中填写字段的注释,有使用方法,可以在配置文件generatorConfig.xml中根据自己的需要进行修改
2. GeneratorConfig:mybatis-plus-generator配置类,该类包含生成相关的绝大部分配置,例如Swagger2、二级缓存、数据源配置、策略配置、包配置等; 3. MysqlGenerator:main方法所在位置 4. TemplateEnum:模板...
附件为mybatis-generator所需要的jar包,包好配置文件generatorConfig.xml文件
* 保存数据库连接与Generator配置,每次代码生成轻松搞定 * 内置常用插件,比如分页插件 * 把数据库中表列的注释生成为Java实体的注释,生成的实体清晰明了 * 可选的去除掉对版本管理不友好的注释,这样新增或删除...
mybatis-generator-core-1.3.2自动生成数据库表中对应的dao和model以及mapper文件,灰常方便,压缩包是个Java工程,解压后可以直接导入到自己的eclipse中,然后修改一下唯一的配置文件generatorConfig.xml里面的...
利用MyBatis Generator自动生成代码,包含mybatis-generator-core的jar包和generatorConfig.xml配置样版
SpringBoot集成Mybatis及如何使用mybatis-generator插件生成代码示例,内含application.yml、generatorConfig.xml等详细配置