mybatis-generator-core 自动生成实体和Mapper

 所谓mybatis-generator-core就是利用mybatis-generator-core.jar自动生成数据库对应的实体和映射文件。首先要下载mybatis-generator-core-1.3.2.zip

1.然后创建一个文件夹我们定义为mybatis。在mybatis里面创建lib和src两个文件夹。同时将mybatis-generator-core-1.3.2.zip解压后的jar文件放到mybatis文件夹中。

2.将mysql的jar包拷贝到lib目录下,mysql-connector-java-5.1.43.jar。注意不要用6.x版本。可能会出问题。

3.然后就是在mybatis里面创建xml文件generator.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:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
    <classPathEntry location=".\lib\mysql-connector-java-5.1.43.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sy" userId="sypro" password="sypro"> -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
            connectionURL="jdbc:mysql://172.23.88.107:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC" 
            userId="root" 
            password="zdsoft">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="cn.duanjt.ssm.entity" targetProject=".\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="cn.duanjt.ssm.mapper" targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="cn.duanjt.ssm.dao" targetProject=".\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 
        要生成那些表(更改tableName和domainObjectName就可以) 
        domainObjectName表示实体对象的名称,不指定将自动生成
        -->
        <table tableName="Student" domainObjectName="" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="stat_app" domainObjectName="" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
 
    </context>
</generatorConfiguration>

 然后就是在mybatis目录下打开dos程序,然后执行代码:

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

 出现如下界面表示成功:

说明:

1.src目录必须存在,否则要抛异常

2.mysql的jar包版本不要用6.x。否则也会出现异常,暂时未找到原因。

猜你喜欢

转载自www.cnblogs.com/duanjt/p/9390548.html