mybatis代码自动生成

1.pom.xml配置generator插件

<plugin>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-maven-plugin</artifactId>
      <version>1.3.7</version>
    <configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
    </configuration>
</plugin>

2.jdbc.propreties 配置数据源和代码生成需要的配置

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://ip\:端口/数据库?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
jdbc.username=用户名
jdbc.password=密码
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

#注意:使用最新版本的jar会有问题,要使用五点几版本的
jdbc.driverPath=D:\\Java\\IdeaProject\\mysql-connector-java-5.1.47\\mysql-connector-java-5.1.47.jar
target_package=生成代码的包位置
project=src\\main\\java

3.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" />
	  <!-- 指定数据连接驱动jar地址 -->
    <classPathEntry location="${jdbc.driverPath}" />
	<!-- 此处指定生成针对MyBatis3的DAO -->
	<context id="context" targetRuntime="MyBatis3">

		  <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>

        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>

        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

         <!--beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号;-->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

         <!-- 为生成的Java模型创建一个toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
         <!-- 添加序列号方法 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

         <!-- 重命名插件 -->
		<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<!-- 		<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> -->
<!-- 			<property name="searchString" value="Example$" /> -->
<!-- 			<property name="replaceString" value="Criteria" /> -->
<!-- 		</plugin> -->

		<!-- 去掉生成出来的代码的注解 -->
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
			<property name="suppressDate" value="true" />
		</commentGenerator>

		<!--数据库链接URL,用户名、密码  jdbc.propreties中读取-->
		<jdbcConnection driverClass="${jdbc.driverClass}"
			connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
		</jdbcConnection>

		<!-- 默认false,表示把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer; true表示把JDBC DECIMAL
			和 NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- 生成bean对象 -->
		<javaModelGenerator targetPackage="${target_package}.pojo"
			targetProject="${project}">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="true" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<!-- 生成sqlMap xml -->
		<sqlMapGenerator targetPackage="${target_package}.mapping"
			targetProject="${project}">
			<property name="enableSubPackages" value="true" />
			</sqlMapGenerator>

		<!-- 生成DAO的类文件 -->
		<javaClientGenerator targetPackage="${target_package}.dao"
			targetProject="${project}" type="XMLMAPPER">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
			
<!--		 简单生成-->
<!--		<table tableName="表名" domainObjectName="生成前缀"-->
<!--			   enableCountByExample="false" enableUpdateByExample="false"-->
<!--			   enableDeleteByExample="false" enableSelectByExample="false"-->
<!--			   selectByExampleQueryId="false">-->
<!--		</table>-->

<!--			    复杂生成-->
		 		<table tableName="表名" domainObjectName="生成前缀"/>

	</context>
</generatorConfiguration>

 4,运行方式:maven -plugins- mybatis-generator:generate 

posted @ 2022-05-25 04:43  韩伊  阅读(922)  评论(0)    收藏  举报