码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis --generator

时间:2015-10-23 13:43:00      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:

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

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.运行成功后生成的文件如下

技术分享

MyBatis --generator

标签:

原文地址:http://my.oschina.net/u/2312022/blog/521114

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!