Myeclipse配置Mybatis_generator生成mapper,xml,entity(结合maven)

在使用mybatis对接数据的时候,手动写mapper,model,entity会比较费时间,可以试用mybatis_gererator来生成难度比较大的mybatis配置文件。先创建项目,项目结构如下图:
这里写图片描述

然后配置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:\maven\mavenJars\repository\mysql\mysql-connector-java\5.1.18\mysql-connector-java-5.1.18.jar" />  
    <context id="Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/dev_test" userId="root"
            password="root">
            <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" 
                userId="msa" password="msa"> -->
        </jdbcConnection>
        <javaTypeResolver>
            <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer, 为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
            <property name="forceBigDecimals" value="true" />
        </javaTypeResolver>
        <!-- 生成实体类的包名和位置,这里配置将生成的实体类放在com.loan.entity这个包下 -->
        <javaModelGenerator targetPackage="com.loan.entity"
            targetProject="Test/src/main/java/">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在com.loan.dao.xml这个包下 -->
        <sqlMapGenerator targetPackage="com.loan.dao.xml"
            targetProject="Test/src/main/java/">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置,这里配置将生成的dao类放在com.loan.dao.mapper这个包下 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.loan.dao.mapper" targetProject="Test/src/main/java/">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="sys_resource" domainObjectName="Resource"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
        <table tableName="sys_role" domainObjectName="Role"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
        <table tableName="sys_role_resource" domainObjectName="RoleResource"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
        <table tableName="sys_user" domainObjectName="User"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

然后到github上下载mybatis/generator插件,git clone https://github.com/mybatis/generator.git.并且把目录
generator/eclipse/UpdateSite下面的features和plugins拷贝到myeclipse相应的目录下面.
这样就可以右击在myeclipse中的generatorConfig.xml,找到生成mybatis artifacts选项,就可以
生成mapper,model,entity
如果生成的过程中报如此错误:Generation Warnings Occured Project . does not exist ,则说明targetProject属性值配置错误,找不到项目,刚开始targetProject的值我配置为“.\src\main\java”,就提示这个错误,后改成 targetProject=”Test/src/main/java/”,终于生成成功!

猜你喜欢

转载自blog.csdn.net/shenxiaomo1688/article/details/80915044