Eclipse自动生成mapper:Mybatis-Generator插件的使用

41 篇文章 0 订阅

在maven项目中使用

1.导包,版本是1.3.2,一定要对应,不然出错了,很难找到问题,我用了1.3.6的版本,出了问题找了半天

//版本是1.3.2,一定要对应,不然出错了,很难找到问题,我用了1.3.6的版本,出了问题找了半天
<mybaits.generator.version>1.3.2</mybaits.generator.version>


<dependency>
    <groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-core</artifactId>
	<version>${mybaits.generator.version}</version>
	 <scope>test</scope>
</dependency>

这是在原项目基础上建的,还需要数据库连接的jar包;

2.xml的书写

<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~ The MIT License (MIT)
  ~
  ~ Copyright (c) 2014 abel533@gmail.com
  ~
  ~ Permission is hereby granted, free of charge, to any person obtaining a copy
  ~ of this software and associated documentation files (the "Software"), to deal
  ~ in the Software without restriction, including without limitation the rights
  ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  ~ copies of the Software, and to permit persons to whom the Software is
  ~ furnished to do so, subject to the following conditions:
  ~
  ~ The above copyright notice and this permission notice shall be included in
  ~ all copies or substantial portions of the Software.
  ~
  ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  ~ THE SOFTWARE.
  -->

<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

  <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    
    <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
      <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
    </plugin>
    //第一个要修改的地方,mysql连接信息
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                    connectionURL="jdbc:mysql://192.168.10.225:3306/zhouwen"
                    userId="root"
                    password="123456">
    </jdbcConnection>
    //第2个要修改的地方,模型
    <javaModelGenerator targetPackage="com.zhouwen.project.system.dao.pojo" targetProject="src/main/java"/>
     //第3个要修改的地方 ,mapper接口
    <sqlMapGenerator targetPackage="com.zhouwen.project.system.dao.mapper"  targetProject="src/main/java"/>
    //第4个要修改的地方 ,xml
    <javaClientGenerator targetPackage="com.zhouwen.project.system.dao.mapper" targetProject="src/main/java" type="XMLMAPPER" />
   
   //第5个要修改的地方这是表名字,表id,也可以设置生成mapper的别名
    <!-- <table tableName="%" >
      <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
    </table> -->
     <table tableName="tb_sys_user" >
      <generatedKey column="user_id" sqlStatement="Mysql" identity="true"/>
    </table>
    
  </context>
</generatorConfiguration>

3.入口,运行他就可以

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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;

public class GeneratorMybatis {

	public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(new File("C:\\Users\\ss\\workspace2\\project-web\\src\\test\\resource\\generator\\generator.xml"));
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}
}

4.项目结构

 

我写的:https://pan.baidu.com/s/1hEywXjrFshePZnK0trdxvQ  

密码:p2f2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值