zoukankan      html  css  js  c++  java
  • Mybatis 自动从数据库生成entity,mapping,dao接口

    1.下载需要的jar包

      mybatis-generator-core-1.3.2.jar,mysql-connector-java-5.1.39.jar

    2.把上面的jar包放到某个目录,并在该目录下创建文件夹

      mybatisMyProjectsrc estmodel

      mybatisMyProjectsrc estmapping

      mybatisMyProjectsrc estdao

    3.在mybatis文件夹下创建configuration.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="mysql-connector-java-5.1.39.jar"/>  
        <context id="DB2Entity"  targetRuntime="MyBatis3">  
            <commentGenerator>  
                <property name="suppressDate" value="true"/>  
                <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
                <property name="suppressAllComments" value="true"/>  
            </commentGenerator>  
            <!--数据库URL,用户名、密码 -->  
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="root">  
            </jdbcConnection>  
            <javaTypeResolver>  
                <property name="forceBigDecimals" value="false"/>  
            </javaTypeResolver>  
            <!-- 生成实体的包名和位置-->  
            <javaModelGenerator targetPackage="test.model" targetProject="MyProjectsrc">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
         </javaModelGenerator>
            <!-- 生成映射文件的包名和位置-->  
            <sqlMapGenerator targetPackage="test.mapping" targetProject="MyProjectsrc">  
                <property name="enableSubPackages" value="true"/>  
            </sqlMapGenerator>  
            <!-- 生成DAO的包名和位置-->  
            <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="MyProjectsrc">  
                <property name="enableSubPackages" value="true"/>  
            </javaClientGenerator>  
            <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->  
            <table tableName="person" domainObjectName="Person" enableCountByExample="false" enableUpdateByExample="false" 
             enableDeleteByExample
    ="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>

    4.在当前目录下打开控制台,输入java -jar mybatis-generator-core-1.3.2.jar -configfile configuration.xml -overwrite  这样就可以自动生成对应的文件了

    最终的目录结构为:

    如果执行命令的时候出现The specified target project directory src does not exist,说明没有建立好对应的目录。

    如果执行报错:XML Parser Error on line 11: 注释中不允许出现字符串 "--"。那么就删掉配置文件中的注释。

    Mybatis配置的参考文档十分详细:http://www.mybatis.org/generator/configreference/javaModelGenerator.html

  • 相关阅读:
    uva558 Wormholes SPFA 求是否存在负环
    管理经验(一)——怎样当好一个管理者
    41. 百度面试题:字符串的排列(字符串)
    24L01/SI24R1调试笔记
    中英文对照 —— 学术概念
    matlab 稀疏矩阵(sparse matrix)
    matlab 稀疏矩阵(sparse matrix)
    matlab 可变参数与默认参数设置
    matlab 可变参数与默认参数设置
    卷积、卷积矩阵(Convolution matrix)与核(Kernel)
  • 原文地址:https://www.cnblogs.com/qlong8807/p/5502431.html
Copyright © 2011-2022 走看看