Demo:
http://pan.baidu.com/s/1pLeyVv9
1.pom.xml
1 <dependencies> 2 <!-- 用于生成日志 --> 3 <dependency> 4 <groupId>log4j</groupId> 5 <artifactId>log4j</artifactId> 6 <version>1.2.17</version> 7 </dependency> 8 <!-- 用于连接oracle数据库 --> 9 <dependency> 10 <groupId>ojdbc</groupId> 11 <artifactId>ojdbc</artifactId> 12 <version>14-10gR3</version> 13 </dependency> 14 <!-- 用于连接mysql数据库 --> 15 <dependency> 16 <groupId>mysql</groupId> 17 <artifactId>mysql-connector-java</artifactId> 18 <version>5.1.38</version> 19 </dependency> 20 <dependency> 21 <groupId>org.mybatis</groupId> 22 <artifactId>mybatis</artifactId> 23 <version>3.3.0</version> 24 </dependency> 25 <!-- mybatis-generator-core核心jar包 --> 26 <dependency> 27 <groupId>org.mybatis.generator</groupId> 28 <artifactId>mybatis-generator-core</artifactId> 29 <version>1.3.2</version> 30 </dependency> 31 </dependencies>
2.mybatis-generator.xml(根目录下新建资源文件夹resources,放里面就行,以后需要读取)
这是用于生成pojo和mapper的配置文件,是独立的,这里面定义了各种映射规则,如生成的位置,生成哪张表甚至可以指定字段等等,具体如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 5 6 <generatorConfiguration> 7 <context id="caigouTables" targetRuntime="MyBatis3"> 8 <commentGenerator> 9 <!-- 是否去除自动生成的注释 true:是 : false:否 --> 10 <property name="suppressAllComments" value="true" /> 11 </commentGenerator> 12 <!--数据库连接的信息:驱动类、连接地址、用户名、密码 --> 13 <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" 14 connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" 15 password="mysql"> 16 </jdbcConnection> --> 17 <jdbcConnection 18 driverClass="oracle.jdbc.OracleDriver" 19 connectionURL="jdbc:oracle:thin:@192.168.44.128:1521:orcl" 20 userId="scott" 21 password="orcl"> 22 </jdbcConnection> 23 24 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 25 NUMERIC 类型解析为java.math.BigDecimal --> 26 <javaTypeResolver> 27 <property name="forceBigDecimals" value="false" /> 28 </javaTypeResolver> 29 30 <!-- targetProject:生成PO类的位置 --> 31 <javaModelGenerator targetPackage="com.yan.pojo" 32 targetProject=".srcmainjava"> 33 <!-- enableSubPackages:是否让schema作为包的后缀 --> 34 <property name="enableSubPackages" value="false" /> 35 <!-- 从数据库返回的值被清理前后的空格 --> 36 <property name="trimStrings" value="true" /> 37 </javaModelGenerator> 38 <!-- targetPackage:mapper映射文件生成的位置 --> 39 <sqlMapGenerator targetPackage="com.yan.dao.mapper" 40 targetProject=".srcmainjava"> 41 <property name="enableSubPackages" value="false" /> 42 </sqlMapGenerator> 43 <!-- targetPackage:mapper接口的生成位置 --> 44 <javaClientGenerator type="XMLMAPPER" 45 targetPackage="com.yan.dao.mapper" 46 targetProject=".srcmainjava"> 47 <property name="enableSubPackages" value="false" /> 48 </javaClientGenerator> 49 50 <!-- 指定表 emp 51 这里不指定schema,逆向工程会查询sysuser都有哪些schema,对每个schema生成对象--> 52 <table schema="scott" tableName="emp"> 53 <!-- <columnOverride column=""></columnOverride> --> 54 </table> 55 <table schema="scott" tableName="dept"/> 56 57 </context> 58 </generatorConfiguration>
3.创建执行类GeneratorExecution,用来读取上述配置文件,并且根据配置文件执行(直接Run as Java Application)就大功告成了,哦对了,记得刷新一下项目,不然看不到哦^_^
1 public class GeneratorExecution { 2 public static void generator(){ 3 4 List<String> warnings=new ArrayList<String>(); 5 try { 6 // 导入配置表mybatis-generator.xml 7 File configFile=new File("resources/mybatis-generator.xml"); 8 // 解析 9 ConfigurationParser cp=new ConfigurationParser(warnings); 10 Configuration config=cp.parseConfiguration(configFile); 11 // 是否覆盖 12 DefaultShellCallback dsc=new DefaultShellCallback(true); 13 MyBatisGenerator mg=new MyBatisGenerator(config, dsc, warnings); 14 mg.generate(null); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 } 18 19 } 20 21 /** 22 * @param args 23 */ 24 public static void main(String[] args) { 25 GeneratorExecution.generator(); 26 System.out.println("done!"); 27 } 28 29 }