Mybatis逆向工程使用方法

使用官方网站的mapper自动生成工具mybatis-generator-core-1.3.2来生成po类和mapper映射文件。

一、mapper生成配置文件

在generatorConfig.xml中配置mapper生成的详细信息,注意以下几点:

1、添加要生成的数据库表

2、po文件所在包路径

3、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 <generatorConfiguration>
 6     <!--数据库驱动 -->
 7     <!-- 如果IDE(eclipse或者idea) 项目里导入了jar包,那么就不需要配置了jar包的绝对路径了 <classPathEntry 
 8         location="e:/project/mybatis/lib/mysql-connector-java-5.0.8-bin.jar"/> -->
 9     <context id="DB2Tables" targetRuntime="MyBatis3">
10         <commentGenerator>
11             <property name="suppressDate" value="true" />
12             <property name="suppressAllComments" value="false" />
13         </commentGenerator>
14         <!--数据库链接地址账号密码 -->
15         <jdbcConnection driverClass="com.mysql.jdbc.Driver"
16             connectionURL="jdbc:mysql://localhost/travel" userId="root"
17             password="root">
18         </jdbcConnection>
19         <javaTypeResolver>
20             <property name="forceBigDecimals" value="false" />
21         </javaTypeResolver>
22         <!--生成Model类存放位置 -->
23         <javaModelGenerator targetPackage="po"
24             targetProject="src">
25             <property name="enableSubPackages" value="false" />
26             <property name="trimStrings" value="true" />
27         </javaModelGenerator>
28         <!--生成映射文件存放位置 -->
29         <sqlMapGenerator targetPackage="mapper"
30             targetProject="src">
31             <property name="enableSubPackages" value="false" />
32         </sqlMapGenerator>
33         <!--生成Dao类存放位置 -->
34 
35         <javaClientGenerator type="XMLMAPPER"
36             targetPackage="mapper" targetProject="src">
37             <property name="enableSubPackages" value="false" />
38         </javaClientGenerator>
39         <!--生成对应表及类名 -->
40         <table tableName="cars" domainObjectName="Category"
41             enableCountByExample="false" enableUpdateByExample="false"
42             enableDeleteByExample="false" enableSelectByExample="true"
43             selectByExampleQueryId="false">
44         </table>
45         <table tableName="flights"></table>
46         <table tableName="hotels"></table>
47         <table tableName="reservations"></table>
48         <table tableName="customers"></table>
49         <!-- <table tableName="product_" domainObjectName="Product" enableCountByExample="false" 
50             enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" 
51             selectByExampleQueryId="false"></table> -->
52     </context>
53 </generatorConfiguration>
View Code

 

二、使用java类生成mapper文件

 1 package test;
 2 
 3 import java.io.File;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.mybatis.generator.api.MyBatisGenerator;
 8 import org.mybatis.generator.config.Configuration;
 9 import org.mybatis.generator.config.xml.ConfigurationParser;
10 import org.mybatis.generator.internal.DefaultShellCallback;
11 
12 public class GenerateMap {
13 
14     public void generator() throws Exception{
15 
16         List<String> warnings = new ArrayList<String>();
17         boolean overwrite = true;
18         //指定 逆向工程配置文件
19         File configFile = new File("generateConfig.xml"); 
20         ConfigurationParser cp = new ConfigurationParser(warnings);
21         Configuration config = cp.parseConfiguration(configFile);
22         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
23         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
24                 callback, warnings);
25         myBatisGenerator.generate(null);
26 
27     } 
28     public static void main(String[] args) throws Exception {
29         try {
30             GenerateMap generatorSqlmap = new GenerateMap();
31             generatorSqlmap.generator();
32         } catch (Exception e) {
33             e.printStackTrace();
34         }
35         
36     }
37 
38 }
View Code

 

运行程序后,自动生成的po类和mapper文件:

 

posted @ 2019-03-06 11:35  Practical  阅读(470)  评论(0编辑  收藏  举报