您的位置:首页 > 编程语言 > Java开发

MyBatis Generator myeclipse 生成 mybatis

2015-12-20 17:21 405 查看
在下载好的插件包解压,找到generator-master\eclipse\UpdateSite目录下的feauresj和plugins放在myeclipse的相应目录下















以下为配置文件
<?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 >
<!-- 设置mysql驱动路径 -->
<classPathEntry location="F:/mysql-connector-java-5.1.18-bin.jar" />
<!-- 此处指定生成针对MyBatis3的DAO -->
<context id="context1"  targetRuntime="MyBatis3">
<!-- jdbc连接信息 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/msite"
userId="root" password="123456" />
<!-- 生成vo对象 -->
<javaModelGenerator targetPackage="com.hlzt.bjcl.commons.model" targetProject="aa" />
<!-- 生成用于查询的Example对象 -->
<sqlMapGenerator targetPackage="com.hlzt.bjcl.commons.model" targetProject="aa" />
<!-- 生成DAO的类文件以及配置文件 -->
<javaClientGenerator targetPackage="com.hlzt.bjcl.commons.dao" targetProject="aa" type="XMLMAPPER" />
<!-- 想要生成的数据库表,自动化工具会根据该表的结构生成相应的vo对象 -->
<table schema="" tableName="order"
enableSelectByExample="true"
enableDeleteByExample="true"
enableCountByExample="true"
enableUpdateByExample="true">
</table>
</context>
</generatorConfiguration>


从网上找的

<?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>
<!-- 引入配置文件 -->
<properties resource="init.properties"/>

<!-- 指定数据连接驱动jar地址 -->
<classPathEntry location="${classPath}" />

<!-- 一个数据库一个context -->
<context id="infoGuardian">
<!-- 注释 -->
<commentGenerator >
<property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
</commentGenerator>

<!-- jdbc连接 -->
<jdbcConnection driverClass="${jdbc_driver}"
connectionURL="${jdbc_url}" userId="${jdbc_user}"
password="${jdbc_password}" />

<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.oop.eksp.user.model"
targetProject="${project}" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="com.oop.eksp.user.data"
targetProject="${project}" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>

<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="com.oop.eksp.user.data"
targetProject="${project}" type="XMLMAPPER" >
<!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>

<!-- 配置表信息 -->
<table schema="${jdbc_user}" tableName="s_user"
domainObjectName="UserEntity" enableCountByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false">
<!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
是否生成 example类   -->

<!-- 忽略列,不生成bean 字段 -->
<ignoreColumn column="FRED" />
<!-- 指定列的java数据类型 -->
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>

</context>
</generatorConfiguration>


XML Parser Error on line 13: XML 文档结构必须从头至尾包含在同一个实体内。

貌似配置文件不能加注释!

Unexpected error while running MyBatis Generator. Exception getting JDBC Driver

查阅generator官方文档发现指定Jar包路径即可

<classPathEntry location="D:/Dev/j2eeDev/jar/mysql-connector-java-5.1.24/mysql-connector-java-5.1.24/mysql-connector-java-5.1.24-bin.jar" />

eneration Warnings Occured Table configuration with catalog null, schema null, and table T_LOGIN did not resolve to any tables

忘了建表,数据库中没表当然解析不了.........创建表,解析成功,毫无压力

增加分页支持

首先在Example对象里面加入两个整形变量start和limit,start代表起始索引,limit代表数量,然后生成对应的get/set方法,使MyBatis在执行的时候能够获取相应的值.

第二部就是修改Mapper了,在selectByExample里面加入对Example对象中start和limit的判断,如果start和limit不同时为0,则自动在sql的最后加入分页条件,以下是需要加入的xml代码,记得加在selectByExample这个sql的最后面,也就是排序的后面

<if test="start !=0 or limit!=0">

limit #{start},#{limit}

</if>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: