MyBatis自動生成Dao層
MyBatis自動生成Dao層,從數據庫的表映射到Java的數據層。包括 Mapper接口的定義,Mapper文件中的sql腳本以及接口中用到的對象
參考地址:
http://mybatis.org/generator/running/runningWithMaven.html
http://mybatis.org/generator/configreference/xmlconfig.html
新建Maven項目(我的是基於SpringBoot)
1.配置pom.xml 主要添加build那一塊
<?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 https://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.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.imodule.product</groupId> <artifactId>product</artifactId> <version>0.0.1-SNAPSHOT</version> <name>product</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <imodule-nexus.version>0.0.1-SNAPSHOT</imodule-nexus.version> <quartz.version>2.3.0</quartz.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <artifactId>jul-to-slf4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> <exclusion> <artifactId>log4j-to-slf4j</artifactId> <groupId>org.apache.logging.log4j</groupId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- mybatis-generator --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <configuration> <!-- mybatis-generator的配置文件,根據情況調整位置 --> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.4.0</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
2.生成dao的配置文件 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> <!--JDBC驅動jar包的位置--> <classPathEntry location="D:/app/maven/repository/mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar"/> <context id="default" targetRuntime="MyBatis3"> <!--創建Java類時是否取消生成注釋--> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--JDBC數據庫連接--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://10.22.33.86:4000/test1?characterEncoding=utf8" userId="root" password="111"> </jdbcConnection> <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類 targetPackage 指定生成的model生成所在的包名 targetProject 指定在該項目下所在的路徑 --> <javaModelGenerator targetPackage="com.imodule.product.generated.pojo" targetProject="./src/main/java"> <!-- 是否允許子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 是否對model添加構造函數 --> <property name="constructorBased" value="true"/> <!-- 是否對類CHAR類型的列的數據進行trim操作 --> <property name="trimStrings" value="true"/> <!-- 建立的Model對象是否 不可改變 即生成的Model對象不會有 setter方法,只有構造方法 --> <property name="immutable" value="false"/> </javaModelGenerator> <!-- mapper映射文件生成所在的目錄 為每一個數據庫的表生成對應的SqlMap文件 --> <sqlMapGenerator targetPackage="generator" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件的代碼 type="ANNOTATEDMAPPER",生成Java Model和基於注解的Mapper對象 type="MIXEDMAPPER",生成基於注解的Java Model和相應的Mapper對象 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.imodule.product.generated.mapper" targetProject="./src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--tables--> <table tableName="PRD_BASE" domainObjectName="PrdBase" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="prd_property" domainObjectName="PrdProperty" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
生成步驟:
IDEA- 點擊Maven - Plugins - 選中 Mybatis-generator 雙擊即可
控制台輸出日志:
如果出現以下類似錯誤
[WARNING] Table configuration with catalog null, schema null, and table XXX did not resolve to any tables
請注意表明的大小寫是否與數據庫一致 ,我將表名改為大寫就都歐克啦!!!
生成前的目錄結構:
生成后的目錄 (紅色框框標記處均為自動生成的)
到這里就歐克啦,后期要使用的時候 maven 打包一下即可啦,然后在別的項目直接引用即可!
這里有個不好的點是所有的表要一一配置,我再康康有沒有啥更快捷的辦法,能對整個數據庫自動生成的哈哈哈哈 找到了我再來更新~