pom.xml里追加以下依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.24</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
update project添加jar包完成。
适用mybatis generator生成entity和mapper
pom.xml里追加如下内容
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/java/com/xxx/config/generatorConfiguration.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
新建generatorConfiguration.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>
<classPathEntry location="D:\repository\mysql\mysql-connector-java\5.1.24\mysql-connector-java-5.1.24.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/rbtblog" userId="root" password="xxx">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.rabbit.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--对应的mapper.xml文件 -->
<sqlMapGenerator targetPackage="mapper.basic" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.rabbit.mapper.basic" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="rbt_article" domainObjectName="Article" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
<table tableName="rbt_article_body" domainObjectName="ArticleBody" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
<table tableName="rbt_article_tag" domainObjectName="User" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
<table tableName="rbt_category" domainObjectName="Category" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
<table tableName="rbt_comment" domainObjectName="Comment" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
<table tableName="rbt_tag" domainObjectName="Tag" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
<table tableName="sys_log" domainObjectName="SysLog" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
<table tableName="sys_user" domainObjectName="SysUser" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
<!-- <columnOverride column="article_content" javaType="java.lang.String" jdbcType="VARCHAR" />
<columnOverride column="article_content_md" javaType="java.lang.String" jdbcType="VARCHAR" /> -->
<!-- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题 -->
</table>
</context>
</generatorConfiguration>
targetProject要写相对路径,否则生成时会报错,找不到路径。
右键工程,run as,选择 run configuration
选择maven,在Goals里填入mybatis-generator:generate -e。
run执行后,刷新工程,可以看到成功生成entity和mapper
本文介绍如何使用MyBatis Generator自动生成Entity和Mapper代码,包括配置pom.xml依赖、设置generatorConfiguration.xml参数,以及运行代码生成任务。
1476

被折叠的 条评论
为什么被折叠?



