在编写java代码时,我们需要使用大量的时间来编写dao层文件,浪费时间也使工作变得枯燥乏味。mybatis generator可以使我们自动生成dao层的文件,这里说一下我使用的配置文件的写法。
准备工作
1.由于需要进行数据库连接,所以我们需要准备一个jdbc的jar包:
2.然后创建一个配置文件generatorConfig.xml,如果需要生成多个表,可添加多个<table>标签:
<?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="ojdbc-5.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection
driverClass="oracle.jdbc.driver.OracleDriver"
connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
userId="****" password="****">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="cn.com.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="cn.com.mapping" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.com.dao" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName:是数据库中的表名或视图名 domainObjectName:是实体类名 schema="数据库用户名" schema 如果加上,则生成的mapping文件会有数据库前缀 -->
<table tableName="***" domainObjectName="***" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="***" domainObjectName="***" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
3.所依赖的jar包也不能少:
资源地址:https://download.csdn.net/download/github_39557053/15866602
4.因为指定路径在src下,所以我们需要再在配置文件同级目录下创建一个src文件夹,最后生成的文件会在此文件夹下。
5.创建一个run-mybatis-generator.bat文件,把如下代码粘贴进去:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
pause
开始运行
1.双击打开最后创建的.bat文件,会显示如下图,然后按照提示,随便输入一个字符,即可完成生成,可在scr文件下查看生成的对应文件。