mybatis-generator 使用

使用mybatis-generator生成dao、mapper和model 

在src/main/resources/下面新建 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>
    <classPathEntry 
      location="D:\repo\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar" />
    <context id="MysqlTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://127.0.0.1:3306/api" userId="root"
            password="root">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="true" />
        </javaTypeResolver>
        <!-- targetProject:自动生成代码的位置 -->  
        <javaModelGenerator targetPackage="org.mybatis.generator.model"
            targetProject="src\main\java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->    
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格  -->   
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="org.mybatis.generator.dao"
            targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="org.mybatis.generator.dao" targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->  
        <table schema="dispatch" tableName="user" domainObjectName="User"
            enableCountByExample="true" enableUpdateByExample="true"
            enableDeleteByExample="true" enableSelectByExample="true"
            selectByExampleQueryId="true" enableInsert="true"
            enableDeleteByPrimaryKey="true" enableSelectByPrimaryKey="true"
            enableUpdateByPrimaryKey="true">
            <property name="useActualColumnNames" value="false" />
        </table>
        <table schema="dispatch" tableName="source" domainObjectName="Source"
            enableCountByExample="true" enableUpdateByExample="true"
            enableDeleteByExample="true" enableSelectByExample="true"
            selectByExampleQueryId="true" enableInsert="true"
            enableDeleteByPrimaryKey="true" enableSelectByPrimaryKey="true"
            enableUpdateByPrimaryKey="true">
            <property name="useActualColumnNames" value="false" />
        </table>
        <table schema="dispatch" tableName="setting" domainObjectName="Setting"
            enableCountByExample="true" enableUpdateByExample="true"
            enableDeleteByExample="true" enableSelectByExample="true"
            selectByExampleQueryId="true" enableInsert="true"
            enableDeleteByPrimaryKey="true" enableSelectByPrimaryKey="true"
            enableUpdateByPrimaryKey="true">
            <property name="useActualColumnNames" value="false" />
        </table>
        <table schema="dispatch" tableName="article" domainObjectName="Article"
            enableCountByExample="true" enableUpdateByExample="true"
            enableDeleteByExample="true" enableSelectByExample="true"
            selectByExampleQueryId="true" enableInsert="true"
            enableDeleteByPrimaryKey="true" enableSelectByPrimaryKey="true"
            enableUpdateByPrimaryKey="true">
            <property name="useActualColumnNames" value="false" />
        </table>
        <table schema="dispatch" tableName="comment" domainObjectName="Comment"
            enableCountByExample="true" enableUpdateByExample="true"
            enableDeleteByExample="true" enableSelectByExample="true"
            selectByExampleQueryId="true" enableInsert="true"
            enableDeleteByPrimaryKey="true" enableSelectByPrimaryKey="true"
            enableUpdateByPrimaryKey="true">
            <property name="useActualColumnNames" value="false" />
        </table>
        <table schema="dispatch" tableName="error" domainObjectName="Error"
            enableCountByExample="true" enableUpdateByExample="true"
            enableDeleteByExample="true" enableSelectByExample="true"
            selectByExampleQueryId="true" enableInsert="true"
            enableDeleteByPrimaryKey="true" enableSelectByPrimaryKey="true"
            enableUpdateByPrimaryKey="true">
            <property name="useActualColumnNames" value="false" />
        </table> 
    </context>
</generatorConfiguration>


如果数据库是oracle或sqlserver,需要去掉<table> 标签的schema属性,否则不会生成Java代码


使用maven插件


	<build>
		<finalName>mybatis-generator</finalName>
		<!-- 设置项目jdk版本,编码 -->
		<plugins>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<configuration>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF8</encoding>
				</configuration>
			</plugin>
		</plugins>

	</build>

然后邮件pom.xml  run as --> maven build...    在Goals中输入 

mybatis-generator:generate


mybatis-generator:generate  注意 这个命令前没有 mvn  。网上很多帖子前面都加了mvn 这样写是错的,也不知道作者验证了没有,简直害人害己


点击run就ok了maven会自动去下载依赖包

顺利的话就可以生成了,如果遇到问题一般都是maven的问题 自行处理


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MyBatis Generator是一个自动生成MyBatis代码的工具,可以根据数据库表结构自动生成对应的Java实体类、Mapper接口和XML映射文件。使用MyBatis Generator可以大大减少手写代码的工作量,提高开发效率。具体使用方法可以参考MyBatis Generator官方文档。 ### 回答2: MyBatis-Generator是一个用于自动生成MyBatis框架下的持久层代码的工具。它可以根据数据库中的表生成Entity、Mapper、XML文件等,让开发者省去了手动编写这些代码的时间和精力,极大地提高了开发效率。 MyBatis-Generator使用步骤如下: 1. 配置generatorConfig.xml文件:这个文件是MyBatis-Generator的配置文件,需要根据实际情况进行配置,包括数据库连接信息、表格配置信息、生成的文件路径和类型等。 2. 运行MyBatis-Generator:可以使用命令行工具或者在IDEA中之间运行,生成的Entity、Mapper和XML文件会在对应的路径下生成。 3. 根据需要进行修改:生成的代码只是根据数据库表自动生成的,有些情况下不一定符合实际需求,需要进行修改。 MyBatis-Generator还有一些高级功能,比如可以添加自定义的插件,可以生成基于注解的Mapper接口等,这些内容需要了解MyBatis和JDBC的原理,掌握这些知识后再使用会更加得心应手。 简而言之,MyBatis-Generator是一个非常实用的工具,在后端开发中有着广泛的应用,可以帮助开发者快速生成持久层代码,提高开发效率。同时,使用MyBatis-Generator也需要了解其使用方法和原理,只有掌握了这些知识才能最大限度地发挥它的作用。 ### 回答3: MyBatis Generator是一个自动生成MyBatis映射文件和Java Bean的工具,可以极大地简化ORM框架中的开发过程。使用MyBatis Generator可以省去手动编写XML映射文件和Java Bean等繁琐的操作,提高开发效率。 MyBatis Generator的配置文件是XML格式,可以通过命令行或者maven插件的方式来执行。其主要配置有以下几个方面: 1. 数据源配置:通过配置数据源信息,让MyBatis Generator能够访问数据库并生成相应的映射文件和Java Bean。 2. 映射文件配置:指定要生成的映射文件的类型、存放路径、文件名等信息。 3. Java Bean配置:指定要生成的Java Bean的类型、存放路径、文件名等信息。 4. 表配置:指定需要生成映射文件和Java Bean的表名、别名、主键生成策略等信息。 在上述配置完成之后,只需要执行MyBatis Generator的命令,即可自动生成映射文件和Java Bean。生成结果可以包含CRUD方法,甚至包含一些基本的查询方法,使得开发人员可以直接使用。 总之,MyBatis Generator是一个非常方便的ORM框架开发工具,可以快速生成映射文件和Java Bean,减少重复劳动,提高开发效率。不过需要注意的是,生成的代码可能存在一些问题,需要开发人员进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值