mybatis genrator的配置详解

xiaoxiao2021-02-28  100

最近刚学习了mybatis generator,不断的尝试,终于配置成功了,先将我配置写下来以供参考:

1. 首先创建maven项目,工程目录如下:

2.pom.xml配置,其中红色代码部分是引入驱动,其实这个也可以不用这段,但是从插件引入,后面写genratorConfig.xml文件时,就不需要配置它驱动的绝对路径了。

<build> <finalName>myMaven</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>

3..创建jdbc.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mymvn username=root password=root initialSize=0 maxActive=20 maxIdle=20 minIdle=20 maxWait=60000 4.创建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> <!-- 引入配置文件 --> <properties resource="jdbc.properties"/> <!-- 数据库驱动 --> <classPathEntry  location="C:/Users/Administrator/.m2/repository/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"> </jdbcConnection> <javaTypeResolver> <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 false:默认, scale>0;length>18:使用BigDecimal; scale=0;length[10,18]:使用Long; scale=0;length[5,9]:使用Integer; scale=0;length<5:使用Short; --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.cn.su.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="com.cn.su.mapping" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.cn.su.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成哪些表--> <table tableName="student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration> 5.启动方式: 点中项目-------》右键--------》Run As,弹出下面选项框: 在标红的选项框里输入: mybatis-generator:generate 点击ok就会执行,第一次执行会下载一些文件,等下载完后会继续执行!
转载请注明原文地址: https://www.6miu.com/read-81902.html

最新回复(0)