Springboot結合mybatis和mybatis generator的外掛,利用tk.mybatis的通用mapper實現逆向工程。
阿新 • • 發佈:2018-12-20
SpringBoot結合Mybatis的generator外掛以及tk.mybatis的通用mapper實現逆向工程的生成 1.首先在資料庫中建好表。 2.然後在pom中增加mybatis和通用mapper的依賴,以及generator的外掛。 3.需要自己在utils中的MyMapper中繼承tk.mybatis中的通用模板。 4.然後編輯generator.xml檔案,注意和properties中配置的結合,設定好mapper生成的檔案的具體位置。具體可以看帖的程式碼的註釋。 5.然後在IDEA中點選右側的maven 外掛,選擇generate即可。 6.生成mapper檔案後,會發現mapper繼承了我們的MyMapper。 下圖所示的XXXXMapper和POJO中的Entity類和resource中的mapper.xml就是逆向生成的產物。
現將pom.xml檔案和generatorConfig.xml以及properties檔案附上。
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> <groupId>com.martina</groupId> <artifactId>o2o</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>o2o</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <druid.version>1.1.10</druid.version> <mysql.version>5.1.46</mysql.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--SpringBoot test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--Mysql連線--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>${mysql.version}</version> </dependency> <!--引入阿里巴巴的資料來源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--通用mapper,使用mapper模板進行--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <!--這個是用於display生成generator.xml檔案的時候用的,這裡我們用外掛生成,所以註釋掉不影響--> <!-- <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> <scope>compile</scope> <optional>true</optional> </dependency>--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <!-- fork=true,用於明確表示編譯版本配置的可用 --> </configuration> </plugin> <!-- mybatis逆向工程外掛 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <!--允許移動生成的檔案 --> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <!--下面這兩個依賴不引入是不會逆向生成的--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.3</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
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> <!-- 引入 application.properties --> <properties resource="application.properties" /> <!-- MyBatis3Simple:不生成 Example相關類及方法 defaultModelType="flat" --> <context id="MysqlContext" targetRuntime="MyBatis3Simple" > <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- 指定生成 Mapper 的繼承模板 --> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="${generator.mappers}"/> </plugin> <!--注意context內的檔案要按序放--> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的註釋 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- jdbc 連線配置 --> <jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"> </jdbcConnection> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成實體類的包名和位置 ,targetPackage指的是包名,targetProject值得是路徑位置--> <!-- 對於生成的pojo所在包,pojo其實就是domain Entity--> <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}" targetProject="${generator.targetProject}"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 對於生成的mapper.xml所在目錄 --> <sqlMapGenerator targetPackage="${generator.sqlMap-targetPackage}" targetProject="src/main/resources"/> <!-- 配置mapper對應的java對映 也可以叫dao層 --> <javaClientGenerator targetPackage="${generator.javaClient-targetPackage}" targetProject="${generator.targetProject}" type="XMLMAPPER"/> <table tableName="tb_area"></table> </context> </generatorConfiguration>
配置檔案中的配置:
# Generator
generator.targetProject=src/main/java
generator.mappers=com.martina.o2o.utils.MyMapper
generator.javaModel-targetPackage=com.martina.o2o.pojo
generator.sqlMap-targetPackage=mapper
generator.javaClient-targetPackage=com.martina.o2o.mapper