还是为了提高开发效率,其实就是懒。在实际模块开发中,每一个curd操作我们都要写对应的service、实体类、dao层以及MyBatis的mapper(sql映射),最然简单但是工作量不少,所以最近用到了mybatis的代码生成插件generator跟大家分享下。
1.下载插件:网盘链接 链接:http://pan.baidu.com/s/1pLyicpt 密码:n8ag
解压后分别打开并拷贝都你的eclipse对应目录下
2.配置generator
重启eclipse 在你使用mybatis的项目上 new>orthers
ok 你会发现你的项目src根目录多了一个配置文件 你可以在这个文件利配置你的生成规则
比如要生成代码的项目、 生成的包路径、 以及数据库连接信息
比如我的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动-->
<classPathEntry location="F:\Eclipseworkstation\MyBatis Generator 1.3.2\mysql-connector-java-5.1.25-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1/test" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.dd.entity" targetProject="ddkjSsm">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.dd.mapper" targetProject="ddkjSsm">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dd.dao" targetProject="ddkjSsm">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="candidate" domainObjectName="Candidate" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
其中需要制定你的数据库连接驱动(我用的是mysql所以是mysql-connector-java-5.1.25-bin.jar)
还有就是数据库表明 实体类名称 以及生成的位置 (自己看很清楚)
配置好了之后 右键你的配置文件 选择如下
你会发现代码已经生成 当然这里生成的sql肯定不能满足你开发中的需求、但是已经为你节省了大量的时间,并且已经生成的sql有很大的复用性,对于其他的业务需求sql自己添加修改就好了。。。