逆向工程字面意思就是反向生成工程,和hibernate一样mybatis也有自己的逆向工程工具,hibernate的逆向生成我没有做过,不过我猜大概都已样,再说,hibernate的现在使用很少了,到了使用的时候再去用吧,使用逆向工程时,需要注意的是表之间的关系无法映射出来!也就是说mybatis的逆向工程生成的都是单表操作,
1:mybatis逆向工程开发文档:
http://www.mybatis.org/generator/configreference/xmlconfig.html
2:使用逆向工程生成代码有好几种方式,这里就介绍一种最简单的,java程序生成:,解释在配置中
2.1准备逆向工程配置文件genreatorConfig.xml,名字无所谓,只要在java程序中作为file传入就好:
<?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="F:/cache/mysql-connector-java-5.1.28-bin.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.leige.domain" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.leige.domain" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.leige.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="student"
domainObjectName="Student"
></table>
<table tableName="teacher"
domainObjectName="Teacher"
></table>
</context>
</generatorConfiguration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
2.2下载jar包,大家如果找不到,可以去maven repository去下载:
mybatis-generator-core-1.3.2.jar
数据库驱动:mysql驱动,获取其他数据库驱动
2.3准备java程序,在开发文档首页,粘贴一下就好了,没必要记住:
package com.leige.test
import java.awt.geom.GeneralPath
import java.awt.im.InputContext
import java.io.File
import java.io.InputStream
import java.util.ArrayList
import java.util.List
import org.apache.ibatis.io.Resources
import org.apache.log4j.chainsaw.Main
import org.apache.log4j.lf5.util.Resource
import org.mybatis.generator.api.MyBatisGenerator
import org.mybatis.generator.config.Configuration
import org.mybatis.generator.config.xml.ConfigurationParser
import org.mybatis.generator.internal.DefaultShellCallback
public class MybatisGen {
public static void generator() throws Exception{
List<String> warnings = new ArrayList<String>()
boolean overwrite = true
//项目根路径不要有中文,我的有中文,所以使用绝对路径
File configFile = new File("F:/cache/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)
}
public static void main(String[] args) {
try {
generator()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
2.4运行java程序,就会发现生成的代码:
***Mapper.java文件是操作接口,mybatis会自动根据*Mapper.xml文件生成实现代理,我们只需要获取接口,调用接口就可以
Example.java文件是综合查询时所需要的参数类,(我看了代码,猜的)*
好了在介绍下,eclipse下可以安装的mybatis-gen…的逆向工程插件,和其他插件安装是一样的步骤:
在线安装地址为:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
转载:http://blog.youkuaiyun.com/do_bset_yourself/article/details/51276517