MyBastis是一个很常用的Orm框架,但它不同于其他框架的是,每个model都有一个mapper.xml,还有一个mapper.xml的实现接口,如果这些都让程序员去写的话实在是太麻烦了,MyBastis插件的出现解决了这个问题,可惜MyBatis插件需要收费,这篇博客就教大家如何在IntelliJ破解使用MyBatis插件
1.下载好MyBatis的jar包
mybaits的代码由github.com管理,
地址:https://github.com/mybatis/mybatis-3/releases
核心文件mybatis3.3.1.jar
2.下载好MyBatis的插件包
http://download.csdn.net/detail/qq_32198277/9707864
Windows用户
去到C:/Users/Administrator/.IntelliJIdea2016.2/config/plugins
把解压后的mybatis_plug复制进去
Linux用户
去到/home/用户名/.IntelliJIdea2016.2/config/plugins
把解压后的mybatis_plug复制进去
Mac用户
去到/Users/用户名/Library/Application Support/IntelliJIdea2016.2
把解压后的mybatis_plug复制进去
重启Idea
3创建数据库和表
创建一个名为test的数据库
再创建一个测试用的表,我们这取名为chat_user
4.创建放置model,mapper和mapperInterface的包
我这分别取名为com.yihaomen.model,com.yihaomen.mapper和com.yihaomen.inter
5.在src目录下创建一个mbgConfiguration.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>
<!-- 配置mysql 驱动jar包路径.用了绝对路径 -->
<classPathEntry location="/home/xjk/IdeaProjects/day03_mybaits/lib/mysql-connector-java-5.1.31-bin.jar" />
<context id="yihaomen_mysql_tables" targetRuntime="MyBatis3">
<!-- 为了防止生成的代码中有很多注释,比较难看,加入下面的配置控制 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 注释控制完毕 -->
<!-- 数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 数据表对应的model 层 -->
<javaModelGenerator targetPackage="com.yihaomen.model" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sql mapper 隐射配置文件 -->
<sqlMapGenerator targetPackage="com.yihaomen.mapper" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 在ibatis2 中是dao层,但在mybatis3中,其实就是mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.yihaomen.inter" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 要对那些数据表进行生成操作,必须要有一个. -->
<table schema="test" tableName="chat_user" domainObjectName="ChatUser"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
如果你想生成 example 之类的东西,需要在<table></table>
里面去掉
example相当于把查询条件封装成一个类
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"
6.生成文件
右键mbgConfiguration.xml
点击Run As MyBastis Generator