前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mybatis-Generator 代码生成器的使用

Mybatis-Generator 代码生成器的使用

作者头像
wsuo
发布2020-10-26 14:39:39
6810
发布2020-10-26 14:39:39
举报

Mybatis 官方为我们提供了持久层代码的代码生成器,可以生成 mapper.xmlMapper 接口和实体类。

环境搭建

新建一个 SpringBoot 工程。

pom.xml

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql.version}</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>${druid.version}</version>
</dependency>

设置 maven 插件

<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.4.0</version>
            <configuration>
                <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
                <overwrite>true</overwrite>
                <verbose>true</verbose>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.18</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

src/main/resources/generator/ 新建一个:

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>
    <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">

        <property name="autoDelimitKeywords" value="true"/>

        <!--    反引号:如果表名或字段名是 MySQL 的关键字就自动加上反引号    -->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 覆盖生成XML文件 -->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

        <!-- 生成的实体类添加 toString() 方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 不生成注释: 因为注释是英文 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/online_course?serverTimezone=UTC"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- domain类的位置 -->
        <javaModelGenerator targetProject="src\main\java"
                            targetPackage="com.lsu.server.domain"/>

        <!-- mapper xml的位置 -->
        <sqlMapGenerator targetProject="src\main\resources"
                         targetPackage="mapper"/>

        <!-- mapper类的位置 -->
        <javaClientGenerator targetProject="src\main\java"
                             targetPackage="com.lsu.server.mapper"
                             type="XMLMAPPER" />

<!--        <table tableName="test" domainObjectName="Test"/>-->
<!--        <table tableName="chapter" domainObjectName="Chapter"/>-->
        <table tableName="section" domainObjectName="Section"/>
<!--        <table tableName="course" domainObjectName="Course"/>-->
<!--        <table tableName="course_content" domainObjectName="CourseContent"/>-->
<!--        <table tableName="course_content_file" domainObjectName="CourseContentFile"/>-->
<!--        <table tableName="teacher" domainObjectName="Teacher"/>-->
<!--        <table tableName="file" domainObjectName="File"/>-->
<!--        <table tableName="user" domainObjectName="User"/>-->
<!--        <table tableName="resource" domainObjectName="Resource"/>-->
<!--        <table tableName="role" domainObjectName="Role"/>-->
<!--        <table tableName="role_resource" domainObjectName="RoleResource"/>-->
<!--        <table tableName="role_user" domainObjectName="RoleUser"/>-->
<!--        <table tableName="member" domainObjectName="Member"/>-->
<!--        <table tableName="sms" domainObjectName="Sms"/>-->
<!--        <table tableName="member_course" domainObjectName="MemberCourse"/>-->
    </context>
<!--    mybatis-generator:generate -e 生成代码命令-->
</generatorConfiguration>

在标签 jdbcConnection 中设置必要的连接值,其余标签注释均有详细的介绍。

table 里的属性表示要生成的表名,这里设置了很多,需要生成哪一个表打开对应的注释即可。

生成文件

输入以下命令执行任务:

mybatis-generator:generate -e

IDEA 中可以设置快捷方式:

会生成 4 个文件:

  • SectionMapper.xml
  • SectionMapper
  • Section
  • SectionExample

3 个文件就是我们平常自己写的文件,主要看一下最后一个 SectionExample

该类提供了很多方法,可以帮我们完成复杂查询,下面以一个例子说明:

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相当于:select * from user where username = 'wyw' and  username is null order by username asc,email desc

UserExample example = new UserExample();
example.or().andxxxx();

可以翻阅文档查看更多使用说明:https://mybatis.org/generator/generatedobjects/exampleClassUsage.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境搭建
  • 生成文件
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档