MyBatis 使用Generator自动生成Model , Dao, mapper

最近   我新建了一 个maven 项目,使用的是spring + springmvc + mybatis框架。

听说Mybatis可以自动生成model和mapper以及dao层,我就从网上查了查资料,自己试着做一下,其中遇到一 个很麻烦的问题,

就是用dos 命令执行  java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite  时 报出  “前言中不允许有内容

这是因为 用记事本打开xml文件转化为UTF-8会有一个BOM头,所以java在读取时就会报以上错误

解决方法:用notepad++打开,在格式选择“以UTF-8无BOM格式编码”,然后保存, 就可以了。

从网上下载这两个jar 包,

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:javageneratormysql-connector-java-5.1.32.jar" /> 
    
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
    
         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/smart4j" userId="root" password="root"> 
        
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
    
        <javaModelGenerator targetPackage="com.telecom.myshiro.model" targetProject="D:javageneratorsrc">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
    
        <sqlMapGenerator targetPackage="com.telecom.myshiro.mapping" targetProject="D:javageneratorsrc">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
    
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.telecom.myshiro.dao" targetProject="D:javageneratorsrc">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
    
        <table tableName="userinfo" domainObjectName="UserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="s_user" domainObjectName="CourseInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

然后在目录里执行dos命令就可以了。 

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

原文地址:https://www.cnblogs.com/murong/p/5057753.html