mybatis自动生成dao, model, mapper xml文件

用mybatis的时候,手写xml或model文件是一个力气活,所以可以用mybatis-gennerator插件自动生成mybatis所需要的dao、bean、mapper xml文件

(原文地址:http://blog.csdn.net/tolcf/article/details/50835165)

附件下载地址:http://files.cnblogs.com/files/cc-robot/generator.rar

把附件解压到本地,我放在d:\web\java目录下了

只需要修改下面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:\web\java\generator\mysql-connector-java-5.1.34.jar" /> 
    <!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/dbname?characterEncoding=utf8" userId="dbuser" password="dbpass"> 
        <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.callservice.model" targetProject="D:\web\java\generator\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="com.callservice.mapping" targetProject="D:\web\java\generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.callservice.dao" targetProject="D:\web\java\generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="admin" domainObjectName="admin" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <!-- <table tableName="course_info" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="course_user_info" domainObjectName="CourseUserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> -->
    </context>
</generatorConfiguration>

复制代码

 然后windwos下打开命令行模式,切换到generator目录下,执行:

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

如图:

 出现successfully,证明生成成功了。

这时再切换到generator/src目录下,可以看到刚才生成的文件了,有三个文件夹:dao,mapping,model,把生成的文件复制到相应项目目录下就可以了。

猜你喜欢

转载自blog.csdn.net/weixin_41722928/article/details/84027693