mybatis-generator 使用

本文介绍如何使用MyBatis Generator自动生成DAO、Mapper和Model层代码,包括配置generatorConfig.xml文件及通过Maven插件运行生成命令。

使用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的问题 自行处理


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值