IDEA使用mybatis-generator插件

IDEA使用mybatis-generator插件

目录结构如下:
在这里插入图片描述
pom.xml添加mybatis-generator依赖

<plugin>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.3.2</version>
	<configuration>
		<verbose>true</verbose>
		<overwrite>true</overwrite>
		<configurationFile>
			<!-- 这里需要配置generatorConfig.xml的文件路径 -->
			src/main/resources/generator/generatorConfig.xml
		</configurationFile>
	</configuration>
</plugin>

导入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>

    <!--指定特定数据库的jdbc驱动jar包的位置(绝对路径)-->
    <classPathEntry location="G:\Maven\rep\com\oracle\ojdbc7\12.1.0.1.0\ojdbc7-12.1.0.1.0.jar"/>

    <context id="default" targetRuntime="MyBatis3">

        <!-- optional,旨在创建class时,对注释进行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接 -->
        <jdbcConnection
                driverClass="oracle.jdbc.driver.OracleDriver"
                connectionURL="jdbc:oracle:thin:@localhost:1521:orcl"
                userId="scott"
                password="scott">
        </jdbcConnection>
        
        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        
        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.example.MybatisGenerator.entity"
                            targetProject="src/main/java">

            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator targetPackage="com.example.MybatisGenerator.dao"
                             targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

		<!-- 数据库表与实体映射 -->
        <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>

生成代码方式

方式一:

在这里插入图片描述

方式二:

在这里插入图片描述
如果没有maven,需要自己添加,点击+号,选择maven即可,在command line 一栏中输入:mybatis-generator:generate -e 。保存即可
在这里插入图片描述
然后在maven projects中找到并运行,即可自动生成。
在这里插入图片描述
执行结果:

G:\soft\Java\jdk1.8.0_131\bin\java -Dmaven.multiModuleProjectDirectory=G:\idea_work\MybatisGenerator -Dmaven.home=G:\Maven\apache-maven-3.6.1-bin\apache-maven-3.6.1 -Dclassworlds.conf=G:\Maven\apache-maven-3.6.1-bin\apache-maven-3.6.1\bin\m2.conf -Didea.launcher.port=7533 "-Didea.launcher.bin.path=G:\soft\IntelliJ IDEA 15.0.2\bin" -Dfile.encoding=UTF-8 -classpath "G:\Maven\apache-maven-3.6.1-bin\apache-maven-3.6.1\boot\plexus-classworlds-2.6.0.jar;G:\soft\IntelliJ IDEA 15.0.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=15.0.2 -s G:\Maven\apache-maven-3.6.1-bin\apache-maven-3.6.1\conf\settings.xml -Dmaven.repo.local=G:\Maven\rep mybatis-generator:generate -e
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------< com.example:MybatisGenerator >--------------------
[INFO] Building MybatisGenerator 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ MybatisGenerator ---
[INFO] Connecting to the Database
log4j:WARN No appenders could be found for l[INFO] Introspecting table emp
ogger (org.mybatis.generator.internal.db.DatabaseIntrospector).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[INFO] Introspecting table dept
[INFO] Generating Record class for table EMP
[INFO] Generating Mapper Interface for table EMP
[INFO] Generating SQL Map for table EMP
[INFO] Generating Record class for table DEPT
[INFO] Generating Mapper Interface for table DEPT
[INFO] Generating SQL Map for table DEPT
[INFO] Saving file EmpMapper.xml
[INFO] Saving file DeptMapper.xml
[INFO] Saving file Emp.java
[INFO] Saving file EmpMapper.java
[INFO] Saving file Dept.java
[INFO] Saving file DeptMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.614 s
[INFO] Finished at: 2019-11-20T12:45:40+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

目录结果变化如下
在这里插入图片描述

如果出现以下错误,可参考我的另外一篇博客,里面有如何处理这个错误
博客地址

2019-11-19 11:35:26.106 [http-nio-18080-exec-1] ERROR o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: Error attempting to get column 'DEPTNO' from result set.  Cause: java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
; uncategorized SQLException; SQL state [99999]; error code [17056]; 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK; nested exception is java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK] with root cause
java.sql.SQLException: 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
	at oracle.sql.CharacterSetUnknown.failCharsetUnknown(CharacterSetFactoryThin.java:233)
	at oracle.sql.CharacterSetUnknown.convert(CharacterSetFactoryThin.java:194)
	...
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值