MyBatis --generator
MyBatis --generator
求是科技 发表于2年前
MyBatis --generator
  • 发表于 2年前
  • 阅读 339
  • 收藏 7
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 最近做工程时,用到了代码生成器 generator,它可以很方便的生成xx.mapper、xx.mappering、xx.bean,即连接数据的接口层、接口实现层、bean对象。

使用代码生成器的步骤如下:

1.创建生成数据的数据源,即数据库表;我们需要对某一张表或者多张表生成xx.mapper、xx.mappering、xx.bean文件。如下是我在navicat中创建好的部分表。

2.在pom.xml中添加mybatis-generator-core


3.在pom.xml中添加连接mysql的驱动jar包

4.maven依赖包显示如下


5.创建配置文件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="C:\Users\Berlython\.m2\repository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar" />
        
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://192.168.x.xx:3306/caren_db" userId="root"
            password="123456" />
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成实体类的包名和位置,这里配置将生成的实体类放在me.gacl.domain这个包下 -->
        <javaModelGenerator targetPackage="com.zhiji.caren.model"
            targetProject="E:\Workspace\w4sts2\caren\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在me.gacl.mapping这个包下 -->
        <sqlMapGenerator targetPackage="com.zhiji.caren.mapping"
            targetProject="E:\Workspace\w4sts2\caren\src\main\java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置,这里配置将生成的dao类放在me.gacl.dao这个包下 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.zhiji.caren.mapper" targetProject="E:\Workspace\w4sts2\caren\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="t_advertisement" domainObjectName="Advertisement"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />

        <table tableName="t_editor" domainObjectName="Editor"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_edu_requirement" domainObjectName="EduRequirement"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_enterprise" domainObjectName="Enterprise"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_enterprise_type" domainObjectName="EnterpriseType"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_feedback" domainObjectName="Feedback"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_hr" domainObjectName="Hr"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_job" domainObjectName="Job"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_job_category" domainObjectName="JobCategory"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_job_tags" domainObjectName="JobTags"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_job_tags_rel" domainObjectName="JobTagsRel"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_job_type" domainObjectName="JobType"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_location" domainObjectName="Location"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_message" domainObjectName="Message"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_person" domainObjectName="Person"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_person_audit_result" domainObjectName="PersonAuditResult"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_person_link" domainObjectName="PersonLink"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_person_link_audit_result" domainObjectName="PersonLinkAuditResult"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_person_recommoned" domainObjectName="PersonRecommoned"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_resume" domainObjectName="Resume"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_resume_submission" domainObjectName="ResumeSubmission"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_salary" domainObjectName="Salary"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_share_record" domainObjectName="ShareRecord"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_sms_info" domainObjectName="SMSInfo"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_sys_info" domainObjectName="SysInfo"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_third_user_info" domainObjectName="ThirdUserInfo"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user" domainObjectName="User"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_account_application" domainObjectName="UserAccountAppliction"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_enterprise_cmmt" domainObjectName="UserEnterpriseCmmt"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_enterprise_focus" domainObjectName="UserEnterpriseFocus"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_enterprise_oper" domainObjectName="UserEnterpiseOper"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_follow" domainObjectName="UserFollow"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_hr_cmmt" domainObjectName="UserHrCmmt"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_hr_focus" domainObjectName="UserHrFocus"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_info" domainObjectName="UserInfo"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_job_filtered" domainObjectName="UserJobFiltered"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_job_focus" domainObjectName="UserJobFocus"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_job_interested_info" domainObjectName="UserJobInterestedInfo"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_person_cmmt" domainObjectName="PersonCmmt"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_person_focus" domainObjectName="UserPersonFocus"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_user_person_oper" domainObjectName="UserPersonOper"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_words" domainObjectName="Words"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_work_experience" domainObjectName="WorkExperience"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />
            
        <table tableName="t_work_years" domainObjectName="WorkYears"
            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" />


    </context>
</generatorConfiguration>

6.备注

需要注意的有数据库驱动、数据库URL、用户名、密码、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最后需要生成的表名和对应的类名。

7.运行

需要通过CMD命令行方式来运行,首先可以先准备一个运行的脚本,这里使用的脚本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

需要注意的是:mybatis-generator-core-1.3.2.jar为下载的对应版本的jar,generatorConfig.xml 为配置文件名,如果不为这个可以在这里进行修改。

启动cmd进入到“F:\soft\mybatis-generator-core-1.3.2\lib”这个目录下,如图:

8.运行成功后生成的文件如下

共有 人打赏支持
粉丝 87
博文 438
码字总数 222864
×
求是科技
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: