springboot整合mybatis以及生成entity和mapper

本文介绍如何使用MyBatis Generator自动生成Entity和Mapper代码,包括配置pom.xml依赖、设置generatorConfiguration.xml参数,以及运行代码生成任务。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值