这是mybatis3 generator 配置文件,包括了主要的配置参数,具体的配置说明请在文章结尾处下载 完整的配置应用。 应用是基于myeclipse的java应用。 文章中的MBG指的就是MybatisGenerator这个工具。 ?xml version=1.0 encoding=UTF-8 ? !DOCTYPE generatorConf
这是mybatis3 generator 配置文件,包括了主要的配置参数,具体的配置说明请在文章结尾处下载 完整的配置应用。
应用是基于myeclipse的java应用。
文章中的MBG指的就是MybatisGenerator这个工具。
userId="${userId}" password="${password}" />
因为需要 eclipse3.6以上 才能运行mybatis插件,所以使用cmd方式运行,采用了.bat方式 直接双击运行即可。
强烈建议将属性文件中各个路径些为绝对路径,但不包括包。
如果需要使用,请修改properties处
------------------------------------------------------------------------------------------------------------------------------------------------------------
上面的方式使用cmd命令来运行mybatis generator,其实apache 提供了更方便的方法。即支持java文件的main入口来读取Generator配置文件。
下面是它的代码:
package util;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class MyBatisGeneratorTool {
public static void main(String[] args) {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;//是否覆盖原来的文件
String genCfg = "/generatorConfig.xml";//配置文件的名称
File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile());//用当前类MyBatisGeneratorTool的加载器来加载 注意路径是java项目名/bin/作为根路径
System.out.println(configFile.getAbsolutePath());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}这样更加简单和方便。
发现的问题: 如果对一个表多次生成,表mapper.xml不会覆盖,而是以追加的方式。所以记住每次生成前线删除掉对应的表mapper.xml文件,其他生成的文件没有该问题.
---------------------------------------------------------------------------------------------------------------------------------------------
mybatis generator 自动生成分页:
项目里已经集成了分页插件,是在工具生成的时候,添加分页内容到各自的mapper.xml文件中,并为mapper.java追加了 selectByPage方法。
实际上比较原来的生成的结果,仅仅是在 mapper.xml文件中追加了:
<resultMap id="selectPageResult" extends="BaseResultMap" type="com.cnofe.base.vo.BaseUser" >
<!--
<association property="" column="" javaType="">
<id column="" property="" jdbcType="" />
<result column="" property="" jdbcType="" />
</association>
-->
</resultMap>
<sql id="select_by_page_outter_where_sql" >
<if test="oredCriteria.size>0"> <if test="_parameter != null" > <include refid="Example_Where_Clause"/> </if> and </if> <if test="oredCriteria.size==0"> where </if>
</sql>
<sql id="select_by_page_inner_where_and_orderby_sql" >
<if test="oredCriteria.size>0"> <if test="_parameter != null" > <include refid="Example_Where_Clause"/> </if> </if> <if test="orderByClause != null"> order by ${orderByClause} </if>
</sql>
<sql id="select_by_page_outter_orderby_sql" >
<if test="orderByClause != null"> order by ${orderByClause} </if>
</sql>
<select id="selectPage" resultMap="selectPageResult" parameterType="com.cnofe.base.vo.BaseUserExample" >
select a.* from base_user a where a.user_id in
(select top ${pageSize} user_id from base_user
<include refid="select_by_page_outter_where_sql"/>
user_id not in (select top ${skipRecordCount} user_id from base_user
<include refid="select_by_page_inner_where_and_orderby_sql"/>
)
<include refid="select_by_page_outter_orderby_sql"/>
)
</select>

mybatis生成工具 是myeclipse里的Java应用:
http://115.com/file/be93a63d 测试通过!!!。
Mybatis3 generator 官方文档
http://www.mybatis.org/generator/index.html
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号