您的位置:首页 > 其它

用Maven中实现MyBatis逆向工程(IDEA版)

2017-11-30 21:17 495 查看

用Maven中实现MyBatis逆向工程(IDEA版)

MyBatis逆向工程介绍

MyBatis逆向工程是指用数据库的表直接生成Java代码,利用MyBatis官方提供的逆向工程,可以针对单表自动生MyBatis执行所需要的代码(如pojo,mapper接口和mapper.xml)。

首先

使用idea中的Maven生成MyBatis所需要的mapper类和xml文件,非常方便。

首先在IDEA中要有MyBatis的这款插件,它为idea提供mybatis的xml的模版文件、接口类与xml文件对应导航。



安装成功后,在maven的resources文件右击使用mybatis-generator-config模版文件,



内容如下:

<?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>

<!--数据库驱动jar包的真实路径 -->
<classPathEntry
location="C:\Users\Hoictas\.m2\repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar"/>

<context id="context" targetRuntime="MyBatis3">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>

<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/db_wechat"
userId="tk"
password="tk"/>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>

<!--指定包名生成实体类 以及生成的地址 (可以自定义地址,如果路径不存在会自动创建) -->
<javaModelGenerator targetPackage="com.mybatis.demo.entity" targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>

<!-- !!!! Mapper XML Configurations !!!! -->
<sqlMapGenerator targetPackage="com.mybatis.demo.mapper.impl" targetProject=".\src\main\java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>

<!-- !!!! Mapper Interface Configurations !!!! -->
<javaClientGenerator targetPackage="com.mybatis.demo.mapper" targetProject=".\src\main\java"
type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>

<!-- 指定数据库表 -->
<table schema="db_wechat" tableName="t_commandcontent"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table schema="db_wechat" tableName="t_automessage"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
<table schema="db_wechat" tableName="t_command"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>


配置简单,没什么难度,再把我的项目结构比对上面的代码,你就知道该怎么根据自己的项目来配置文件。



最后点击右侧的maven project 找到mybatis-generator运行即可。



2017/11/30 19:41:19
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: