使用MyBatis_Generator生成Dto、Dao、Mapping

本文介绍如何使用MyBatis Generator自动生成持久层代码,包括配置文件的设置、所需jar包及执行步骤,并展示了完整的表结构到Java对象的映射过程。

1.建立表语句如下

CREATE TABLE
    tb_user
    (
        id INT NOT NULL AUTO_INCREMENT COMMENT '主键',
        userName VARCHAR(40),
        password VARCHAR(40) NOT NULL COMMENT '密码',
        email VARCHAR(40) COMMENT '邮件',
        age INT COMMENT '年龄',
        sex CHAR(2) NOT NULL COMMENT '性别',
        PRIMARY KEY (id),
        CONSTRAINT username UNIQUE (userName)
    )
    ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.新建java project项目

3.新建com.sk.mapper和com.sk.models包

4.新建source folder,名称为config

5.jar包

     mybatis-generator-core-1.3.1.jar
     mysql.jar

6.新建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="D:/WorkSpaceTest/GeneratorConfigDemo/mysql.jar" /> 
<!-- 此处指定生成针对MyBatis3的DAO -->
  <context id="context1" targetRuntime="MyBatis3">
 <!-- 去除自动生成的注释  -->
 <commentGenerator>
    <property name="suppressAllComments" value="true" />
    <property name="suppressDate" value="true" />
   </commentGenerator>
   
   <!-- jdbc连接信息 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ceshi" userId="root" password="root" /> 
    <!-- 生成vo对象 -->
    <javaModelGenerator targetPackage="com.sk.models" targetProject="./src" />
    <!-- 生成用于查询的Example对象 -->
    <sqlMapGenerator targetPackage="config/mapper" targetProject="./config" />
    <!-- 生成DAO的类文件以及配置文件 -->
    <javaClientGenerator targetPackage="com.sk.mapper" targetProject="./src" type="XMLMAPPER" />
   
    <!-- 想要生成的数据库表,自动化工具会根据该表的结构生成相应的vo对象 -->
 <table tableName="tb_user" domainObjectName="TTbUser"  enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"></table>
  </context> 
</generatorConfiguration> 
7.运行cmd,如下:

8.看到Mybatis Generator finshed successfully,表示成功,我们的项目结构如下:

 

欢迎大家给出意见!

爱生活,爱分享,爱康宝!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值