java笔记之使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件2种方法
来源:互联网 发布:苹果c语言编程软件 编辑:程序博客网 时间:2024/06/11 05:45
Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
方法一、在外面自动生成Dao,Model,Mapping文件
1、在自己数据库创建一个表名为mymessages的表
2、先下载相关文件
关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases,(版本自己选择)
由于我使用的是Mysql数据库,这里需要再准备一个连接mysql数据库的驱动jar包(如果解压没有mysql驱动包就把自己本地有的拷贝到该目录下)
1) 和Hibernate逆向生成一样,这里也需要一个配置文件generatorConfig.xml(如果没有这个直接新建一个就是。):
<?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="mysql-connector-java-5.0.8-bin.jar"/> 8 <context id="DB2Tables" targetRuntime="MyBatis3"> 9 <commentGenerator>10 <property name="suppressDate" value="true"/>11 <property name="suppressAllComments" value="true"/>12 </commentGenerator>13 <!--数据库链接地址账号密码-->14 <jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://ip:8080/mymessages(自己表名)" userId="root" password="自己数据库密码">15 </jdbcConnection>16 <javaTypeResolver>17 <property name="forceBigDecimals" value="false"/>18 </javaTypeResolver>19 <!--生成Model实体类存放位置-->20 <javaModelGenerator targetPackage="lcw.model" targetProject="src">21 <property name="enableSubPackages" value="true"/>22 <property name="trimStrings" value="true"/>23 </javaModelGenerator>24 <!--生成映射文件xml存放位置-->25 <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">//targetPackage 自己的包名<!--targetProject 该文件你想放位置(路径的不要有中文)-->26 <property name="enableSubPackages" value="true"/>27 </sqlMapGenerator>28 <!--生成Dao接口类存放位置-->29 <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">30 <property name="enableSubPackages" value="true"/>31 </javaClientGenerator>32 <!--生成对应表及类名-->33 <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>34 </context>35 </generatorConfiguration>
对上面配置xml说明:如果这2个jar包在一个路径就直接mysql驱动包的对应名。如果没有location就下入你对应包所在路径加包名前面
上面配置文件中的:
<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>tableName和domainObjectName为必选项,分别代表数据库表名和生成的实体类名,其余的可以自定义去选择(一般情况下均为false)。
2) 生成语句文件:
在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
二、安装eclipse的mybatis-generator插件,在项目里面直接生成Dao,Model,Mapping文件
1、1) 在线安装:
Help->Install New Software输入插件地址:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/,如下图所示,一路Next安装即可
2)离线安装:下载相关文件https://github.com/mybatis/generator/releases里面的
解压后,将features和plugins这2个文件拷贝到你eclipse所在路径下面,即可。
2.创建MyBatis Generator配置文档
点击项目名->New->Other...
如图所示新建MyBatis Generator配置文档,按提示创建好即可,便会在对应项目下生成generatorConfig.xml文件。
3.配置generatorConfig.xml(同第一种方法一样),注意的是,
targetProject= "路径" //这个路径(不能有中文)就是生成相关文件所要放的位置。这里已经创建在项目下面。可以直接只写项目名。前面把包名写好了就行。
4.创建数据库表(和的一种方法一样)。然后将表名写入generatorCofig.xml文件里面。
5.运行及结果展示
在该文件右键执行即可,生成相关文件,如图所示:
暂时还没有解决支持中文路径的,不知道有大神知道不,可以交流学习。
- java笔记之使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件2种方法
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 利用mmap /dev/mem 读写Linux内存
- 让你流泪的不是毕业,而是无法再重走一次的青春
- Eclipse配置相关
- Android PieChart 饼图控件
- [思考汇聚]杨小凯:后发劣势
- java笔记之使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件2种方法
- 字符串
- android:configChanges属性
- Android clipChildren用法
- 呵呵!回首已枉然
- laravel框架初次使用
- php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
- Objective-C中的各种遍历(迭代)方式
- mybatis 中的<![CDATA[ ]]>