• Mybatis自动生成实体类、dao接口和mapping映射文件


    由于Mybatis是一种半自动的ORM框架,它的工作主要是配置mapping映射文件,为了减少手动书写映射文件,可以利用mybatis生成器,自动生成实体类、dao接口以及它的映射文件,然后直接拷贝到工程中稍微修改就可以直接使用了。

    生成器目录如下:

    首先进入lib文件夹中,该目录如下:

    (图上文件下载地址:http://download.csdn.net/detail/qiwei31229/9790909

    主要修改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="mysql-connector-java-5.1.25-bin.jar"/>  
        <context id="DB2Tables"  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://127.0.0.1/test" userId="root" password="123456">  
            </jdbcConnection>  
            <javaTypeResolver>  
                <property name="forceBigDecimals" value="false"/>  
            </javaTypeResolver>  
            <!-- 生成模型的包名和位置-->    
            <javaModelGenerator targetPackage="com.shaw.cn.domain" targetProject="src">  
                <property name="enableSubPackages" value="true"/>  
                <property name="trimStrings" value="true"/>  
            </javaModelGenerator>  
            <!-- 生成映射文件的包名和位置-->    
            <sqlMapGenerator targetPackage="com.shaw.cn.mapping" targetProject="src">  
                <property name="enableSubPackages" value="true"/>  
            </sqlMapGenerator>  
         <!-- 生成DAO的包名和位置-->    
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.shaw.cn.IDao" targetProject="src">  
                <property name="enableSubPackages" value="true"/>  
            </javaClientGenerator>  
         <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->   
            <table tableName="user_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        </context>  
    </generatorConfiguration>  

    注意表要现在具体的数据库里头创建好,然后在lib文件夹中shift+鼠标右键打开控制台命令输入:

    Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

    这样就可以在src看到制定的目录的代码了。

    作者:shawWey
    出处: https://www.cnblogs.com/shawWey/
    要相信所有的事情都会过去的,只是以什么样的方式,什么样的结果结束而已,保持平常心,做你该做的。
  • 相关阅读:
    RedHat 7 安装PostgreSQL 10.5
    百万级数据库优化方案
    所有文章的测试Demo
    PostGreSql安装
    windows server 2016部署服务
    Spring MVC Hello World 404
    Unity攻略
    Unity判断用户联网状态,WiFi/移动网络/无网络
    Unity UGUI Layout自动排版组件用法介绍
    Unity中对系统类进行扩展的方法
  • 原文地址:https://www.cnblogs.com/shawWey/p/6604261.html
Copyright © 2020-2023  润新知