generator自动生成mybatis配置和类信息
2015-12-26 23:06
525 查看
generator自动生成mybatis的xml配置、model、map等信息:
generator自动生成mybatis的xml配置、model、map等信息:
1、下载mybatis-generator-core-1.3.2.jar包。
网址:http://code.google.com/p/mybatis/downloads/list?can=3&q=Product%3DGenerator,下载mybatis-generator-core-1.3.2-bundle.zip,解压
找到lib下的需要jar包。
2、编写genertor的xml文件,名下:generator.xml
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="mysql-connector-java-5.1.27-bin.jar"/>
<context id="Mysql" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 指定生成的注释中是否包含生成的日期 -->
<property name="suppressDate" value="false"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test-xc" userId="root" password="123456">
</jdbcConnection>
<!-- 指定JDBC类型和Java类型如何转换 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="test.model" targetProject="D:\new-workspace\Dubbo_project\com.gh.center\src\main\java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="test.mapping" targetProject="D:\new-workspace\Dubbo_project\com.gh.center\src\main\java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator targetPackage="test.dao" targetProject="D:\new-workspace\Dubbo_project\com.gh.center\src\main\java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- schema:数据库名字 tableName:用于自动生成代码的数据库表;domainObjectName:生成对象的基本名称,对应于数据库表的javaBean类名 -->
<table schema="test-xc" tableName="user" domainObjectName="User" >
<generatedKey column="id" sqlStatement="Mysql"/>
</table>
</context>
</generatorConfiguration>
table其他属性:
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
schema即为数据库名, tableName为对应的数据库表, domainObjectName是要生成的实体类,
如果想要mapper配置文件加入sql的where条件查询, 可以将enableCountByExample等设为true,
这样就会生成一个对应domainObjectName的Example类, enableCountByExample等设为false时,
就不会生成对应的Example类了.
如果table里边不配置property,默认字段都生成为类属性。
<ignoreColumn column="FRED" />//忽略字段
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />//无论字段是什么类型,生成的类属性都是varchar。
3、运行有四种:命令生成(最简单)、Java生成、ant生成、maven生成。这里说两种,有兴趣其余的可以在mybatis官网去学习。
1)、运行-》cmd->java - jar jar包的文件路径 -configfile generator.xml的文件路径 -overwrite 命令。
如下:
Java代码
java -jar E:\Websoft\mybaits\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3.2.jar -configfile E:\WebWorkSpace\workspace_js\downAttachdemo\src\com\mochasoft\down\generator.xml -overwrite
成功时输出:MyBatis Generator finished successfully.
2)、java运行关键代码:
Java代码
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
其实Java运行,细分可以分两种,还有一种可以去官网学习。
4、生成代码之后,根据自己的实际项目架构,可以对生成的代码进行适当的修改,如把数据库管理交有spring等等。
相关文章推荐
- R的一些统计分析包工具
- codeforces 603 C. Load Balancing 贪心
- Sql使用日常总结
- iOS OC(四) 字符串NSString 可变数组NSMutableArray 不可变数组NSArray 数值对象类NSNumber NSValue 数值对象类NSNumber
- 进军服务器了,转点mysql操作命令
- 401.2 登录方法 尚未确定
- Java中的引用传递
- MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)
- Source Insight
- CSS实现鼠标聚焦时边框不变,框内图片放大
- fedora 远程windows桌面
- deb,rpm格式
- java环境变量的配置
- django 快速搭建blog
- 使用web ssh 登陆设备---gateone开发之旅
- 基于rails的schedule网站开发(17):动态流
- 【杭电】[0000]整除235
- macd综合版
- [错误整理]error LNK1123:转换到COFF期间失败 解决方案
- 宿主机为linux、windows分别实现VMware三种方式上网