sweets_wn的gravatar头像
sweets_wn 2018-08-30 11:05:22
DB系列(一)__mybatis自动生成代码

记得多年前用ibatis的时候,写配置文件还是蛮费劲的,而且容易出问题,这两天看mybatis,代码都可以自动生成了。利用mybatis-generator插件,可以自动生成对应的model、dao和mapper(复杂sql手动编写),很大程度上减少了手动编码的时间,而且还能一定程度上避免配置错误问题。下面就Maven插件的方式(还有命令行、eclipse插件两种实现方式)做详细说明,mark下,也备后来者参考。

环境

Intellij IDEA 2018

JDK1.8

Tomcat9

Maven3

一、建一个基于maven的项目(IDEA中,一个项目即一个Module),此处不做累述。下图是我用于测试的project,目录结构可供参考:

DB系列(一)__mybatis自动生成代码

二、pom.xml文件配置

<build>标签下增加插件,配置如下:

<!-- mybatis-generator自动生成代码插件 -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <configuration>
        <!-- mybatis用于生成代码的配置文件 -->
        <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>3.4.0</version>
        </dependency>
    </dependencies>
</plugin>

三、generatorConfig.xml文件配置

该文件的存放路径需与pom.xml中配置的路径保持一致,如上一步,文件路径为:/src/main/resources。文件内容如下:

<?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="application.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <!-- 生成的文件编码 -->
        <property name="javaFileEncoding" value="utf-8"/>
        <!-- 集成通用Mapper -->
        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.test.springboot.common.dao.MyMapper"/> 
        </plugin>
        <!-- 生成注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <!-- 数据库连接属性(application.properties文件中取值) -->
        <jdbcConnection driverClass="${spring.datasource.driverClassName}" connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}" password="${spring.datasource.password}"/>
        <!-- 生成实体类配置 -->
        <javaModelGenerator targetPackage="com.test.springboot.entity" targetProject="src/main/java"/>
        <!-- 生成mapper.xml配置文件 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
        <!-- 生成mapper接口文件配置 -->
        <javaClientGenerator targetPackage="com.test.springboot.mapper" targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <!-- 罗列要生成哪些表 , %表示生成全部的表 -->
        <table tableName="%">
            <!--mysql 配置 -->
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
            <!--oracle 配置-->
            <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
        </table>

        <!-- 罗列要生成的表,多个实体类复制多份该配置即可 -->
        <!--<table tableName="user_info" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>-->

    </context>
</generatorConfiguration>

注意:以上配置中的文件路径需做对应修改。MyMapper.java源码如下:

package com.test.springboot.common.dao;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
    //FIXME 特别注意,该接口不能被扫描到,否则会出错
}

四、自动生成代码

DB系列(一)__mybatis自动生成代码

如上图,第3步右击

DB系列(一)__mybatis自动生成代码

等待。运行结束后,控制台日志如下:

DB系列(一)__mybatis自动生成代码

查看自动生成的代码。

1. model,即实体类,如下图:

DB系列(一)__mybatis自动生成代码

DB系列(一)__mybatis自动生成代码

2. dao,即mapper接口

DB系列(一)__mybatis自动生成代码

DB系列(一)__mybatis自动生成代码

注意:接口名称需与xml配置文件对应。

3. mapper,即xml配置,sql写在该文件

DB系列(一)__mybatis自动生成代码

DB系列(一)__mybatis自动生成代码

至此,自动生成代码结束。


打赏
最近浏览
dingjiaqiang  LV5 2020年11月18日
fashion1314  LV5 2020年4月25日
zdm2859 2020年4月23日
暂无贡献等级
下载狂魔 2020年1月16日
暂无贡献等级
西秦月影  LV1 2019年10月24日
1248612588  LV1 2019年10月22日
惜缘尘露  LV4 2019年9月19日
759985221  LV8 2019年9月16日
xp9522  LV9 2019年8月31日
891353192  LV1 2019年7月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友