我在使用mybatis写类与数据表中的映射时,由于表的列名太多,出现的问题也很多,列名与类对象属性的对应要一个一个写的话很比较复杂。
为了方便和减少失误,可以使用 MyBatis 提供的 code generator 自动生成 mybatis 的 xml 映射文件、Model 、 Map 等信息,大家可以到
MyBatis 官网下载一个 mybatis-generator-core-1.3.2-bundle ,在压缩包中找到 lib 下的 jar 包。然后编写 mybatis.xml ,并执行即可
 
<generatorConfiguration>
	    <!-- 这里是加载JDBC驱动的jar包地址-->
        <classPathEntry location="C:\Users\Administrator.WJ-20131202XSPS\Desktop\mybatis-generator-core-1.3.2\lib\mysql-connector-java-5.1.7-bin.jar" />  
      
        <context id="DB2Tables" targetRuntime="MyBatis3">  
      
	     <!-- 是否去除自动生成的注释 true:是,false:否--> 
            <commentGenerator>  
                <property name="suppressAllComments" value="true" />  
            </commentGenerator>  
      
	     <!-- 数据库连接的信息:驱动类、连接地址、用户名、密码--> 
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
                connectionURL="jdbc:mysql://localhost/workloads" userId="root" password="mysql106366">  
            </jdbcConnection>  
         
		 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer  true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal-->  
            <javaTypeResolver>  
                <property name="forceBigDecimals" value="false" />  
            </javaTypeResolver>  
    
            <!--应用实体的生成信息-->
            <javaModelGenerator targetPackage="com.tly.entity"  
                targetProject="C:\table">  
                <property name="enableSubPackages" value="true" />  
                <property name="trimStrings" value="true" />  
            </javaModelGenerator>  
			
             <!--sqlMapper XML文件的生成信息,包括生成路径等    targetProject表示生成文件存放的位置-->
            <sqlMapGenerator targetPackage="com.tly.mapping"  targetProject="C:\table">  
                <property name="enableSubPackages" value="true" />  
            </sqlMapGenerator>  
       
            <javaClientGenerator type="XMLMAPPER"  
                targetPackage="com.tly.dao" targetProject="C:\table">  
                <property name="enableSubPackages" value="true" />  
            </javaClientGenerator> 
	         <!--数据库中表和程序里实体的对应关系-->
            <table tableName="project" domainObjectName="ClassRecord" enableCountByExample="false" enableUpdateByExample="false" 
			enableDeleteByExample="false"                enableSelectByExample="false" selectByExampleQueryId="false"  >  
            </table> 
            <table tableName="project_score" domainObjectName="Parameter" enableCountByExample="false" enableUpdateByExample="false" 
			enableDeleteByExample="false"                enableSelectByExample="false" selectByExampleQueryId="false"  >  
            </table> 	
        </context>  
    </generatorConfiguration>
在 cmd 中输入 java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml –overwrite ,这里的路径都是绝对的,为了方便,我们可以在 mybatis-generator-core-1.3.2 所在的文件夹下建一个 bat 文件,输入
最后我们可以按住shift建点击右键,直接在该文件夹处打开命令窗口,直接运行 bat 文件即可
generator自动生成mybatis配置和类信息,布布扣,bubuko.com
原文:http://www.cnblogs.com/John-Lyn/p/3830242.html