使用MyBatis Generator自动创建代码(dao,mapping,poji)

连接的数据库为SQL server2008,所以需要的文件为sqljdbc4.jar

使用的lib库有:

在lib库目录下新建一个src文件夹用来存放生成的文件,然后新建generatorConfig.xml

里面代码为:

 1  1 <?xml version="1.0" encoding="UTF-8"?>    
 2  2 <!DOCTYPE generatorConfiguration    
 3  3   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"    
 4  4   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">    
 5  5 <generatorConfiguration>    
 6  6 <!-- 数据库驱动-->    
 7  7 <!-- sqljdbc4.jar是SQLServer数据库连接jar包,如果要连接MySQL数据库直接把sqljdbc4.jar改成mysql-connector-java-5.1.25-bin.jar -->  
 8  8     <classPathEntry  location="sqljdbc4.jar"/>    
 9  9     <context id="DB2Tables"  targetRuntime="MyBatis3">    
10 10         <commentGenerator>    
11 11             <property name="suppressDate" value="true"/>    
12 12             <!-- 是否去除自动生成的注释 true:是 : false:否 -->    
13 13             <property name="suppressAllComments" value="true"/>    
14 14         </commentGenerator>    
15 15         <!--数据库链接URL,用户名、密码 -->    
16 16         <!--连接数据SQLServer -->    
17 17         <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
18 18 
19 19 connectionURL="jdbc:sqlserver://127.0.0.1:1433;databaseName=dbSSMTEST" userId="sa" password="123">  
20 20         <!--连接数据库MySQL -->    
21 21      <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.10:3306/ypzlmanagement" 
22 22 
23 23 userId="root" password="hewei123">  -->    
24 24         </jdbcConnection>    
25 25         <javaTypeResolver>    
26 26             <property name="forceBigDecimals" value="false"/>    
27 27         </javaTypeResolver>    
28 28         <!-- 生成模型的包名和位置-->    
29 29         <!-- targetPackage="com.ssm.pojo"是生成代码的存放路径这里根据你的路径存放 -->    
30 30         <javaModelGenerator targetPackage="com.ssm.pojo" targetProject="src">    
31 31             <property name="enableSubPackages" value="true"/>    
32 32             <property name="trimStrings" value="true"/>    
33 33         </javaModelGenerator>    
34 34         <!-- 生成映射文件的包名和位置-->    
35 35         <!-- targetPackage="com.ssm.pojo"是生成代码的存放路径这里根据你的路径存放 -->    
36 36         <sqlMapGenerator targetPackage="com.ssm.mapping" targetProject="src">    
37 37             <property name="enableSubPackages" value="true"/>    
38 38         </sqlMapGenerator>    
39 39         <!-- 生成DAO的包名和位置-->    
40 40         <!-- targetPackage="com.ssm.pojo"是生成代码的存放路径这里根据你的路径存放 -->    
41 41         <javaClientGenerator type="XMLMAPPER" targetPackage="com.ssm.dao" targetProject="src">    
42 42             <property name="enableSubPackages" value="true"/>    
43 43         </javaClientGenerator>    
44 44         <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->    
45 45         <!-- tableName是要生成数据库映射文件的表名     domainObjectName要生成代码的实体类名 根据自己需求修改 -->    
46 46         <table tableName="student" domainObjectName="student" enableCountByExample="false" enableUpdateByExample="false" 
47 47 
48 48 enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
49 49     </context>    
50 50 </generatorConfiguration>

 

 最后在cmd控制台下找到lib的根目录然后执行以下语句

 1 Java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite 

如图所示:

然后在文件夹目录下可以看见自动生成的文件

 

posted @ 2016-10-13 14:48  calmLang  阅读(669)  评论(0编辑  收藏  举报