Java mybatis Connect Oracle database automatic generation of code tools

Source: Internet
Author: User
Tags generator

1. First put the top 4 packages under the Lib folder

2. Configure XML, placed in the SRC directory


<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis generator Configuration" "1.0//en
  G/dtd/mybatis-generator-config_1_0.dtd "> <generatorConfiguration> <!--Configure the Oracle driver jar package path. Using a relative path--> <classpathentry location= "Webroot/web-inf/lib/ojdbc7.jar"/> <context id= "Yihaomen_mysql_tables" Targetruntime= "MyBatis3" > <!--to prevent a lot of comments in the generated code, it's ugly to add the following configuration control--> <commentGenerator> <p Roperty name= "Suppressallcomments" value= "true"/> <property "name=" Suppressdate "true" value=/> ommentgenerator> <!--Note Control--> <!--database connection,--> <jdbcconnection driverclass= "Oracle.jdbc . Driver. Oracledriver "connectionurl=" jdbc:oracle:thin:@152.19.5.181:1521/gldbpdb1 "userid=" Mybatis "Passwo" rd= "Mylove" > </jdbcConnection> <javatyperesolver > <property name= "Forcebigdecimals "value=" false "/> </javaTypeResolver> <!--data table corresponding to the model layer, Targetproject for the automatically generated file Put the path--> <javamodelgenerator targetpackage= "src. Model "targetproject=" src "> <property name=" enablesubpackages "value=" false "/> <property" tr Imstrings "value=" true "/> </javaModelGenerator> <!--SQL Mapper insinuate configuration file--> Rator targetpackage= "src. 
    
    Map "targetproject=" src "> <property name=" enablesubpackages "value=" false "/> </sqlMapGenerator> <!--is a DAO layer in Ibatis2, but in Mybatis3, it is mapper interface--> <javaclientgenerator type= "Xmlmapper" Targetpacka Ge= "src. Client "targetproject=" src "> <property name=" enablesubpackages "value=" false "/> </javaclientgenera Tor> <!--You must have one for those data tables to be generated. --> <table schema= "MyBatis tablename=" category "Domainobjectname=" category "Enablecountbyexample=" Fals E "EnableUpdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid = "false" > </table> </context> </generatorConfiguration>

3. Write Execution code

Package com.yihaomen.test;
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 Genmain {public static void main (string[] args) {list<string> warnings = new ARRAYLIST&LT;STRING&G
		t; ();
		Boolean overwrite = true;
		String gencfg = "/mbgconfiguration.xml";
		File ConfigFile = new file (GenMain.class.getResource (gencfg). GetFile ());
		Configurationparser cp = new Configurationparser (warnings);
		Configuration config = null;
		try {config = cp.parseconfiguration (configfile);
		catch (IOException e) {e.printstacktrace (); catch (XMLParserexception e) {e.printstacktrace ();
		} Defaultshellcallback callback = new Defaultshellcallback (overwrite);
		Mybatisgenerator mybatisgenerator = null;
		try {mybatisgenerator = new Mybatisgenerator (config, 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 ();
 }
	}
}
4. Execute the main method to automatically generate the required files


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.