使用Ecplise 用Mybatis generatorConfig 生成代码

配置文件

在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件

<plugin>  
	<groupId>org.mybatis.generator</groupId>  
	<artifactId>mybatis-generator-maven-plugin</artifactId>  
	<version>1.3.2</version> 
<dependencies>
    
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.5</version>
	</dependency>
			
	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.16</version>
	</dependency>
	
</dependencies> 
</plugin> 

**建立 File db.properties **

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo1
username=root
password=123456

mode.package=com.openlab.pojo //
dao.package=com.openlab.mapper
xml.mapper.package=com.openlab.mapper
target.project=MavenDemo

创建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>
    <!--导入数据连接及属性配置等-->
    <properties resource="jdbc.properties"></properties>
    <!--基础配置在context标签
        id 可以随意起
        targetRuntime 运行环境
    -->
    <context id="context" targetRuntime="MyBatis3">
        <!--对注释的控制-->
        <commentGenerator>
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!--我们要自动生成对应的代码,自然要获取数据库的连接-->
        <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"></jdbcConnection>


        <!--Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.openlab.pojo" targetProject="src/main/java"></javaModelGenerator>

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

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

        <!--配置需要映射的表-->
        <table tableName="user" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
               enableCountByExample="false" selectByExampleQueryId="false">

        </table>
        <table tableName="emp" enableSelectByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
               enableCountByExample="false" selectByExampleQueryId="false">
        </table>



    </context>

以上三个文件都存放在main\java\resourses
否则会报错 找不到相关文件

**相关文件配置完成后 右键点击项目工程-----> run as ----->Run Configurations 得到下图在这里插入图片描述
**
Run之后refresh项目 相应的 包以及代码会在generatorConfig的指定目录出现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值