在网上查了下如何自动实现mybatis的中mapper以及映射接口和bean,感觉不是挺适合新手的,而且说的模棱两可,介于此,我就自己写一篇记录,日后忘记了便于翻阅。
我们都知道,mybatis要手动配置那些文件,有时弄不好会出错,我刚开始学的时候深有体会,如今春天来了,废话不多说,入正题。这个是lib里面的结构
这里最重要的是是generatorConfig.xml,
<?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>
<!-- 数据库驱动-->
<!-- 可以根据自己的情况而补加架包,这个架包是放在lib目录下的。-->
<classPathEntry location="ojdbc5.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="scott" password="tiger">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<!-- 根据自己的情况而生成包名-->
<javaModelGenerator targetPackage="com.shandian.bean" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.shandian.mapping" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.shandian.Dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!--一张表对应一个table-->
<table tableName="Emp" domainObjectName="Emp" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="Dept" domainObjectName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
首先你们到我的度盘中吧这个下下来解压放到你喜欢的盘下,链接: https://pan.baidu.com/s/1o8wLXwe 密码: iz4t,
然后通过doc命令进入lib目录下,然后将这个命令拷贝到如下。java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
回车,最后你会看到
此时说明你成功了,接下来进入到lib下面的src目录下看有没有你所需要的mapper以及接口映射和bean
现在你可以去嗨皮你的mybatis了。