概述
使用IntelliJ IDEA安装MyBatis-Plugin插件。
http://myoss.github.io/2016/MyBatis-Plugin-学习使用/通过简单的配置就可以生成所需的实体类、mapper映射文件和接口。
在src/main/resources目录下新建mybatis-generator配置文件。
生成文件如下所示:
<?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>
<!-- !!!! Driver Class Path !!!! -->
<classPathEntry location=""/>
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- !!!! Database Configurations !!!! -->
<jdbcConnection driverClass="" connectionURL="" userId="" password=""/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- !!!! Model Configurations !!!! -->
<javaModelGenerator targetPackage="" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!! Mapper XML Configurations !!!! -->
<sqlMapGenerator targetPackage="" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- !!!! Mapper Interface Configurations !!!! -->
<javaClientGenerator targetPackage="" targetProject="THIS_CONFIGURATION_IS_NOT_REQUIRED" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- !!!! Table Configurations !!!! -->
<table tableName="" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
<properties url="file:///D:/N3verL4nd/Desktop/M/src/main/resources/jdbc.properties"/>
<!-- !!!! Database Configurations !!!! --> <jdbcConnection driverClass="${jdbc.driverClassName}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
可以如上加载数据库配置文件,貌似使用resource不能使用。
详细配置说明:
http://www.jianshu.com/p/e09d2370b796
http://luoxianming.cn/2016/03/15/mybatis0/
作如下配置:
<?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>
<!-- !!!! Driver Class Path !!!! -->
<classPathEntry location="D:Javagradlehomecachesmodules-2files-2.1mysqlmysql-connector-java5.1.40ef2a2ceab1735eaaae0b5d1cccf574fb7c6e1c52mysql-connector-java-5.1.40.jar "/>
<context id="context" targetRuntime="MyBatis3">
<property name="javaFileEncoding" value="UTF-8"/>
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="false"/>
</commentGenerator>
<!-- !!!! Database Configurations !!!! -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="root" password="lgh123"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- !!!! Model Configurations !!!! -->
<javaModelGenerator targetPackage="cn.bjut.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- !!!! Mapper XML Configurations !!!! -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- !!!! Mapper Interface Configurations !!!! -->
<javaClientGenerator targetPackage="cn.bjut.mapper" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- !!!! Table Configurations !!!! -->
<table tableName="persons" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
enableUpdateByExample="false" domainObjectName="Person"/>
</context>
</generatorConfiguration>
domainObjectName:生成实体类、映射的名称(此处就是Person.java、PersonMapper.java、PersonMapper.xml)
在配置该xml文件前你得先创建相应的目录结构。
在该xml文件上右键:
就会自动生成实体类、mapper映射文件和相应的接口了。
生成前后对应的目录结构:
不知道是不是该插件本身的bug还是破解导致的bug:
<sqlMapGenerator targetPackage="cn.bjut.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator>如上配置,经测试,该插件将mapper映射文件生成在src/main/java路径下,而不是在src/main/resources目录下。
后来使用官方的生成代码则没有问题,这也证实了该bug的存在。
package cn.bjut;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by N3verL4nd on 2017/7/6.
*/
public class MyBatisGene {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<>();
boolean overwrite = true;
InputStream in = MyBatisGene.class.getClassLoader().getResourceAsStream("mybatis-generator-config.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(in);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
最后
以上就是霸气金鱼为你收集整理的IntelliJ IDEA MyBatis-Plugin插件的使用的全部内容,希望文章能够帮你解决IntelliJ IDEA MyBatis-Plugin插件的使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复