使用mybatis逆向工程生成对应的bean、dao以及mapper

http://www.mybatis.org/generator/quickstart.html

1.引入jar包(在pom.xml中引入)
  

     

2.在工程下创建MBG配置文件

数据库中表:

 

mbg.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>

  <context id="DB2Tables" targetRuntime="MyBatis3">
  
      <!-- 逆向生成时,没有注释 -->
    <commentGenerator>
      <property name="suppressAllComments" value="true" />
    </commentGenerator>
    
      <!-- 配置数据库连接信息 -->
    <jdbcConnection 
        driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
        userId="root"
        password="123">
    </jdbcConnection>
    
    <!-- java类型解析 -->
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    
    <!-- 指定javaBean生成的位置
        targetPackage:生成在哪个包下
        targetProject:生成在哪个工程下
     -->
    <javaModelGenerator 
        targetPackage="com.atguigu.crud.bean" 
        targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    
    <!-- 指定sql映射文件生成的位置 -->
    <sqlMapGenerator 
        targetPackage="mapper"  
        targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!-- 指定dao接口生成的位置,就是mapper接口生成的位置 -->
    <javaClientGenerator type="XMLMAPPER" 
        targetPackage="com.atguigu.crud.dao"  
        targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <!-- table 指定每个表的生成策略 
        tableName 表示连向数据后逆向生成哪张表
        domainObjectName 表示表对应的Javabean的类名
    -->
    <table tableName="tbl_emp" domainObjectName="Employee"></table>
    <table tableName="tbl_dept" domainObjectName="Department"></table>
    
  </context>
</generatorConfiguration>

 

 

3.生成:

  

package com.atguigu.crud.test;

import java.io.File;
import java.io.IOException;
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.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class MBGtest {
    
    public static void main(String[] args) throws Exception {
         List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("mbg.xml");
           ConfigurationParser cp = new ConfigurationParser(warnings);
           Configuration config = cp.parseConfiguration(configFile);
           DefaultShellCallback callback = new DefaultShellCallback(overwrite);
           MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
           myBatisGenerator.generate(null);
    }
}

 

posted @ 2018-10-22 11:29  一纸流年  阅读(3929)  评论(0编辑  收藏  举报