MyBatis逆向工程基本操作及代码实例

回想一下MyBatis的基本操作,它需要实体类,自定义mapper接口以及mapper.xml配置文件。为了避免繁琐的工作量,MyBatis Generator,简称MBG,可自动生成框架所需的这些依赖,同时支持基本的CRUD操作,但是需要注意,MBG一般不要重复执行,否则会出问题。

下面是引入MBG的基本操作:

一、新建maven项目,引入依赖:


  
    org.mybatis
    mybatis
    3.5.5
  

  
    mysql
    mysql-connector-java
    8.0.20
  

  
    org.mybatis.generator
    mybatis-generator-core
    1.4.0
  

二、创建mbg配置文件:





  
    
    
    
    
    
    
    
    
    

三、运行Generator:

public class MyTest {
  public static void main(String[] args) {
    List warnings = new ArrayList<>();
    File configFile = new File(MyTest.class.getResource("generatorConfig.xml").getFile());
    ConfigurationParser configurationParser = new ConfigurationParser(warnings);
    Configuration configuration = null;
    try {
      configuration = configurationParser.parseConfiguration(configFile);
    } catch (IOException e) {
      e.printStackTrace();
    } catch (XMLParserException e) {
      e.printStackTrace();
    }
    DefaultShellCallback callback = new DefaultShellCallback(true);
    MyBatisGenerator myBatisGenerator = null;
    try {
      myBatisGenerator = new MyBatisGenerator(configuration, callback, warnings);
    } catch (InvalidConfigurationException e) {
      e.printStackTrace();
    }
    try {
      myBatisGenerator.generate(null);
    } catch (SQLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

这都是固定写法,了解一下即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(MyBatis逆向工程基本操作及代码实例)