mybatis逆向工程

mybatis的反向生成可以有效的减少我们代码的编写量,具体生成步骤:

1、首先使用创建javaWeb项目

2、在WebRoot/WEB-INF/lib 中添加:

  ojdbc14.jar    ( build path  ----->add)

3、(以maven javaWeb项目为例)再src/main/resources中创建配置文件:generatorConfig.xml

 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     <classPathEntry  location="D:java_Demomabits_autoWebRootWEB-INFlibojdbc14.jar"/>  
 8     <context id="DB2Tables"  targetRuntime="MyBatis3">  
 9         <commentGenerator>  
10             <property name="suppressDate" value="true"/>  
11             <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
12             <property name="suppressAllComments" value="true"/>  
13         </commentGenerator>  
14         <!--数据库链接URL,用户名、密码 -->  
15         <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:***" userId="***" password="****">  
16         </jdbcConnection>  
17         <javaTypeResolver>  
18             <property name="forceBigDecimals" value="false"/>  
19         </javaTypeResolver>  
20         <!-- 生成模型的包名和位置-->  
21         <javaModelGenerator targetPackage="com.ssm.entity" targetProject="src/main/java">  
22             <property name="enableSubPackages" value="true"/>  
23             <property name="trimStrings" value="true"/>  
24         </javaModelGenerator>  
25         <!-- 生成映射文件的包名和位置-->  
26         <sqlMapGenerator targetPackage="com.ssm.mapper" targetProject="src/main/resources">  
27             <property name="enableSubPackages" value="true"/>  
28         </sqlMapGenerator>  
29         <!-- 生成DAO的包名和位置-->  
30         <javaClientGenerator type="XMLMAPPER" targetPackage="com.ssm.mapper" targetProject="src/main/java">  
31             <property name="enableSubPackages" value="true"/>  
32         </javaClientGenerator>  
33         <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->  
34         <table tableName="student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
35         <table tableName="grade" domainObjectName="Grade" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
36         
37     </context>  
38 </generatorConfiguration>  

4、写java 代码运行即可:

 1 public static void main(String[] args) throws Exception {
 2         
 3         List<String> warnings = new ArrayList<String>();
 4         boolean overwrite = true;
 5         File configFile = new File("src/main/resources/generatorConfig.xml");
 6         ConfigurationParser cp = new ConfigurationParser(warnings);
 7         Configuration config = cp.parseConfiguration(configFile);
 8         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
 9         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
10         myBatisGenerator.generate(null);
11     }

注意: 当使用逆向工程对oracle 进行操作时,当不同用户下出现表名相同时 ,会出现Table schema,(可以删除其中一个表,否则解析会出现错误),也可以设置schema属性进行,但设置属性对后期维护和再可发可能造成麻烦。mysql中不会出现此类问题。

 

原文地址:https://www.cnblogs.com/hua-show/p/7309391.html