Mybatis的逆向工程

Mybatis的逆向工程能夠對數據庫進行單表操做,自動生成相應的mapper.java,mapper.xml,相應實體類。java

須要導入相應的jar包mysql

 

先要配置相應的xml文件sql

<?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/student"
        userId="root"
        password="123">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
        
        <!-- 指定javabean生成的位置 -->
    <javaModelGenerator 
            targetPackage="com.student.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.student.dao"  targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

        <!-- 指定每一個表的 生成策略-->
    <table tableName="usertable" domainObjectName="User" >
      
    </table>
  </context>
</generatorConfiguration>

根據java代碼自動生成數據庫

package com.student.test;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import java.util.*;

import javax.security.auth.login.Configuration;

import org.mybatis.generator.api.MyBatisGenerator;
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 Generator {
     public void generator() throws Exception{

            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            //指定 逆向工程配置文件
            File configFile = new File("generatorConfig.xml"); 
            ConfigurationParser cp = new ConfigurationParser(warnings);
           org.mybatis.generator.config.Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                    callback, warnings);
            myBatisGenerator.generate(null);

        } 
        public static void main(String[] args) throws Exception {
            try {
                Generator generatorSqlmap = new Generator();
                generatorSqlmap.generator();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
}

生成bean包,dao包及mapper文件夾下的文件api

 

相關文章
相關標籤/搜索