祝各位道友念头通达
GitHub Gitee 语雀 打赏

MyBatis的逆向工程

 逆向工程

 

 1.导入jar包

  

 2.generator.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>

  <classPathEntry location="F:/eclipse-p/eclipse/mysql-connector-java-5.1.45.jar" />
  
  <context id="DB2Tables" targetRuntime="MyBatis3">
  	
  	 <commentGenerator>
  	 	<property name="suppressDate" value="true"/>
  	 	<property name="suppressAllComments" value="true"/>
  	 </commentGenerator>
  	 
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/ban32"
        userId="root"
        password="199617">
    </jdbcConnection>
	
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <javaModelGenerator targetPackage="com.zhiyou.han.bean" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
	
    <sqlMapGenerator targetPackage="com.zhiyou.han.mapper"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou.han.dao"  targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <table tableName="user" domainObjectName="User"
    		 enableUpdateByExample="false" enableCountByExample="false"
    		enableSelectByExample="true" enableDeleteByExample="false"
    	>
		<!--enableUpdateByExample="false" enableCountByExample="false"
    		enableSelectByExample="false" enableDeleteByExample="false"  
设置后会生成相关的封装的方法,例如模糊查询,不需要再xml里面进行大量的环境配置
-->     
    </table>

  </context>
</generatorConfiguration>

   3.测试类函数

  

 

package com.zhiyou.han;



import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

public class GeneratorTest {
    @Test
    public void show() throws Exception{
         List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("generator.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);
           System.out.println("=============>OVER");
    }
    
}

 

posted @ 2018-10-08 21:42  韩若明瞳  阅读(183)  评论(0编辑  收藏  举报