码迷,mamicode.com
首页 > 编程语言 > 详细

mybatis怎样自动生成java类,配置文件?

时间:2014-09-01 22:35:23      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   java   ar   for   

其实没有什么东西是可以自动生成的,只不过是别人已经写好了,你调用罢了。

所以想要mybatis自动生成java类,配置文件等,就必须要一些配置和一些jar包。当然这些配置也很简单。

为了有个初步的认识,首先我列出了所需要的文件:

bubuko.com,布布扣

其中标红的比较重要。好了,让我们开始吧

1.首先需要在数据库建好表,随便建几个就好。

2.下载mybatis-generator-core包

  下载地址:http://search.maven.org/

  然后搜索mybatis-generator-core下载即可

3.下载mysql-connector-java包

  想必大家这个包都很熟悉,因为它 就是mysql的java语言驱动包

  因为我们是根据数据库表来生产配置文件,当然需要连接到数据库了,那也就是需要驱动包了。

  下载地址:http://search.maven.org/

  然后搜索mysql-connector-java,选择需要的版本下载即可

4.generator.xml文件

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <!DOCTYPE generatorConfiguration  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 3 
 4 <generatorConfiguration>
 5  <!-- clas<font size="" color=""></font>sPathEntry:数据库的JDBC驱动 -->
 6   <classPathEntry location="D:\study\spring-mvc-mybatis-maven\mysql-connector-java-5.0.3-bin.jar" />
 7  
 8   <context id="DB2Tables" targetRuntime="MyBatis3">
 9  <!-- 去除自动生成的注释 -->
10  <commentGenerator>
11   <property name="suppressAllComments" value="true" />
12  </commentGenerator>
13      <!-- 数据库的url 用户名 密码-->
14     <jdbcConnection driverClass="com.mysql.jdbc.Driver"
15         connectionURL="jdbc:mysql://localhost:3306/sy"
16         userId="root"
17         password="mysql">
18     </jdbcConnection>
19 
20     <javaTypeResolver >
21       <property name="forceBigDecimals" value="false" />
22     </javaTypeResolver>
23  
24  <!-- 生成模型的包名和位置:自动生成代码的位置, -->
25     <javaModelGenerator targetPackage="com.sy.model" targetProject="D:\study\spring-mvc-mybatis-maven\src">
26       <property name="enableSubPackages" value="true" />
27       <property name="trimStrings" value="true" />
28     </javaModelGenerator>
29     
30  <!-- 自动生成映射文件的包名和位置 -->
31     <sqlMapGenerator targetPackage="com.sy.mapping"  targetProject="D:\study\spring-mvc-mybatis-maven\src">
32       <property name="enableSubPackages" value="true" />
33     </sqlMapGenerator>
34  <!-- 生成Dao 的包名和位置  -->
35     <javaClientGenerator type="XMLMAPPER" targetPackage="com.sy.dao"  targetProject="D:\study\spring-mvc-mybatis-maven\src">
36       <property name="enableSubPackages" value="true" />
37     </javaClientGenerator>
38  
39  <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
40  <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
41   <table tableName="tbug" domainObjectName="Bug" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
42   <table tableName="tmenu" domainObjectName="Menu" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
43   <table tableName="tonline" domainObjectName="Online" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
44   <table tableName="tresource" domainObjectName="Resource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
45   <table tableName="trole" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
46   <table tableName="trole_tresource" domainObjectName="RoleResource" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
47   <table tableName="tuser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
48   <table tableName="tuser_trole" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
49   </context>
50   
51 </generatorConfiguration>
generator.xml

  其中generator.xml文件里都有很详细的注释,大家一看就明白,我们需要做的有

  (1) 修改mysql驱动的路径,小编的路径如下:

    <classPathEntry location="D:\study\spring-mvc-mybatis-maven\mysql-connector-java-5.0.3-        bin.jar" />

  (2)修改  数据库的url 用户名 密码

  (3)修改保存自动生成文件的路径,小编的路径为 

  <javaModelGenerator targetPackage="com.sy.model" targetProject="D:\study\spring-mvc-mybatis-  maven\src">

  这里大家可以建一个src空文件夹,然后把全路径复制覆盖小编的 路径即可。

  注:这里的model,mapper,dao三个路径都需要修改,否则就找不到路径了

5.好了,准备工作都做好了,一个命令即可生成

  首先cmd进入到generator.xml这个文件的路径下  如

  cd d:

  cd study\spring-mybatis-maven

  (win 7 下有快捷方式进入:

  在generator.xml目录下,单击右键:在此处打开命令窗口(W))

  运行命令:

  java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

 

是不是成功了?快去看看刚刚新建的src文件夹吧,看是不是已经有代码了。

  

  

 

 

 

  

mybatis怎样自动生成java类,配置文件?

标签:style   blog   http   color   os   io   java   ar   for   

原文地址:http://www.cnblogs.com/ruo-/p/3950058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!