MyBatis generator (postgres)-----生成Dto、Dao、Mapping

本文介绍如何在Eclipse中安装MyBatis Generator插件,并通过配置generatorConfig.xml来自动生成Dto、Dao及Mapping文件。适用于使用PostgreSQL数据库的项目。

一.eclipse Mybatis generator 插件安裝
二.MyBatis generator (postgres)—–生成Dto、Dao、Mapping

一.eclipse Mybatis generator 插件安裝
1.自動安裝
—-打开eclipse,点击Help>Software Update
—-选择 “Available Software” 标签,点击 “Add Site” 按钮
—-输入以下信息:
Location:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
—-点击ok,自动进入 “mybatis generator Feature”
—-点击“install”按钮进行安装。。。。mybatis generator 插件安装完成

2.手動安裝
—-教程http://jingyan.baidu.com/article/9faa7231506ed8473c28cbee.html
—-插件下載http://jingyan.baidu.com/article/9faa7231506ed8473c28cbee.html

二.MyBatis generator (postgres)—–生成Dto、Dao、Mapping
由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件.

—–創建表
CREATE TABLE worker (
id varchar(50) NOT NULL,
username varchar(18) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
password varchar(18) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;

—–配置generator config
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="/home/peng.zhang/zpeng/sources/myjars/postgresql-9.4-1201.jdbc4.jar" />
    <context id="context1">
        <jdbcConnection driverClass="org.postgresql.Driver"
            connectionURL="jdbc:postgresql://127.0.0.1:5432/postgres" userId="postgres"
            password="123456" />
        <!-- 生成模型的包名和位置--> 
        <javaModelGenerator targetPackage="com.travelzen.entity"
            targetProject="MybatisTOPostgresql" />
        <!-- 生成映射文件的包名和位置--> 
        <sqlMapGenerator targetPackage="mybatis"
            targetProject="MybatisTOPostgresql" />
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator
            targetPackage="com.travel.dao"
            targetProject="MybatisTOPostgresql"
            type="XMLMAPPER" />
        <!-- 要生成哪些表-->  
        <table tableName="worker" domainObjectName="WorkerDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
        <!-- <table schema="zpeng" tableName="stu" domainObjectName="Student"
            enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true"
            enableDeleteByPrimaryKey="true" enableSelectByExample="false"
            enableDeleteByExample="false" enableCountByExample="false"
            enableUpdateByExample="false">
            <columnOverride column="" property="" />
        </table> -->
    </context>
</generatorConfiguration>

—–生成(兩種方式)
1.右键配置文件运行:

这里写图片描述

2.命令:java -jar /Users/liqiu/git/study/web/soft/mybatis-generator-core-1.3.2/lib/mybatis-generator-core-1.3.2.jar -configfile /Users/liqiu/git/study/web/soft/mybatisGeneratorConfig.xml -overwrite

*無法正確生成看這裏:
http://www.cnblogs.com/AloneSword/p/3412236.html

1.XML Parser Error on line 13: XML 文档结构必须从头至尾包含在同一个实体内。
貌似配置文件不能加注释!

2.Unexpected error while running MyBatis Generator. Exception getting JDBC Driver
查阅generator官方文档发现指定Jar包路径即可

3.eneration Warnings Occured Table configuration with catalog null, schema null, and table T_LOGIN did not resolve to any tables
忘了建表,数据库中没表当然解析不了………创建表,解析成功,毫无压力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值