Idea + mybatis Generator + Oracle reverse code generation

Mybatis generator can automatically generate dao, entity and mapper files corresponding to database tables. Here, take oracle as an example

My project directory is as follows:

Create dao and entity files under demo and mapper under resource, and the automatically generated files will be in these three folders.

I. create a new generator.properties code as follows

``

jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.connectionURL=jdbc:oracle:thin:@x.x.x.x:1521:orcl
jdbc.username=User name
jdbc.password=Password

II. generatorConfig.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>
    <!--Import property configuration-->
    <properties resource="mybatis-generator/generator.properties"></properties>  
    <context id="default" targetRuntime="MyBatis3">

        <!-- optional,Aimed at creating class Control comments when -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
        <!-- Remove automatically generated comments or not true: Yes, false:no -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc Database connection for -->
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.username}"
                password="${jdbc.password}">
        </jdbcConnection>


        <!-- Not required, type processor, in database type and java Conversion control between types-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model Model builder,Used to generate a primary key key Class, record class and query of Example class
            targetPackage     Specify generated model Package name where the build is located
            targetProject     Specify the path under the project
        -->
        <javaModelGenerator targetPackage="com.example.demo.entity"
                            targetProject="./src/main/java">

            <!-- Whether subpackages are allowed, i.e targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- Is it right? model Add constructor -->
            <property name="constructorBased" value="true"/>
            <!-- Are classes right? CHAR Type of column trim operation -->
            <property name="trimStrings" value="true"/>
            <!-- Established Model Whether the object is immutable or not Model Object will not have setter Method, only construction method -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--Mapper The directory where the mapping file is generated generates the corresponding SqlMap file -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="./src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- Client code, easy to use for Model Object and XML Code for profile
                type="ANNOTATEDMAPPER",generate Java Model And annotation based Mapper object
                type="MIXEDMAPPER",Generate annotation based Java Model And corresponding Mapper object
                type="XMLMAPPER",generate SQLMap XML Documentation and independent Mapper Interface
        -->
        <javaClientGenerator targetPackage="com.example.demo.dao"
                             targetProject="./src/main/java" type="XMLMAPPER">
            <!-- enableSubPackages:Whether to let schema As suffix of package -->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- Table to generate tableName Is the table or view name in the database domainObjectName Is the entity class name mapperName yes Dao name-->
        <table tableName="Database table name" domainObjectName="generate entity name"  mapperName="generate dao name"
         enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
         enableSelectByExample="false" selectByExampleQueryId="false">
           </table>
    </context>
</generatorConfiguration>

resource = "mybatis-generator/generator.properties" write the relative path here

tableName = "database table name" domainObjectName = "generate entity name" mapername = "generate dao name"
tableName is the name of the file to be generated in the database, domainObjectName is the name of the entity class generated under entity, mapperName is the name of the interface generated under dao

III. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.6</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.2.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!--Add to mybatis generator maven Plug-in unit-->
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.6</version>
				<configuration>
					<!--generatorConfig.xml position-->
					<configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<phase>generate-sources</phase>
					</execution>
				</executions>
				<!--Must be added here oracle Driving package-->
				<dependencies>
					<dependency>
						<groupId>com.oracle</groupId>
						<artifactId>ojdbc6</artifactId>
						<version>11.2.0.2.0</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>
</project>

For my convenience, I post all of them directly, but in fact, I only need mybatis generator core, spring boot starter JDBC, ojdbc6
And mybatis generator Maven plugin. The version of mybatis generator is 1.3.6, and 1.3.7 has bug s

4. Double click mybatis generator: generate to run


The following message indicates that the file was generated successfully

ok, here's mybatis generator reverse engineering!

Keywords: Mybatis JDBC Spring xml

Added by Diego17 on Sun, 10 Nov 2019 23:00:47 +0200