欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用mybatis-generator自动生成代码

程序员文章站 2022-05-06 18:09:56
...

使用mybatis-generator自动生成代码

学生表

使用mybatis-generator自动生成代码

配置环境(generator.properties)

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/studentboot?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

配置代码生成规则(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="generator.properties"/>

    <!--defaultModelType="flat" 大数据字段,不分表 -->
    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="autoDelimitKeywords" value="true" />
        <property name="beginningDelimiter" value="`" />
        <property name="endingDelimiter" value="`" />
        <property name="javaFileEncoding" value="utf-8" />
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 注释 -->
        <commentGenerator >
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.yangzc.studentboot.student.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis/student" >
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成mapxml对应client,也就是接口dao -->
        <javaClientGenerator targetPackage="com.yangzc.studentboot.student.dao" targetProject="src/main/java" type="XMLMAPPER" >
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <table tableName="stu_student" domainObjectName="StudentDO" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
            <generatedKey column="sno" sqlStatement="Mysql" identity="true" />
        </table>

    </context>
</generatorConfiguration>

引入插件

<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>
   <dependencies>
       <dependency>
           <groupId>org.mybatis.generator</groupId>
           <artifactId>mybatis-generator-core</artifactId>
           <version>1.3.7</version>
       </dependency>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>${mysql.version}</version>
       </dependency>
   </dependencies>
</plugin>

使用generator生成代码

使用mybatis-generator自动生成代码

自动生成的代码文件

使用mybatis-generator自动生成代码使用mybatis-generator自动生成代码

学生接口代码

package com.yangzc.studentboot.student.controller;

import com.yangzc.studentboot.common.domain.ActionResult;
import com.yangzc.studentboot.student.dao.StudentDOMapper;
import com.yangzc.studentboot.student.domain.StudentDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yangzc
 * @Description:
 * @Date: Created on 7:59 2019/12/24
 * @Modified By:
 */

@Controller
@RequestMapping("/stu/list")
public class StudentController {
    private String prefix = "student";

    @Autowired
    StudentDOMapper studentDOMapper;

    @GetMapping()
    @RequiresPermissions("stu:list")
    String student() {
        return prefix + "/list";
    }

    @GetMapping(value = "/welcome")
    @RequiresPermissions("stu:list")
    @ResponseBody
    public ActionResult getStudents() {
        List<StudentDO> students = studentDOMapper.selectAll();
        ActionResult result = new ActionResult(students);
        return result;
    }
}

github项目地址

https://github.com/yangzc23/studentboot

参考资料

[01] mybatis-generator:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver
[02] Spring Boot(七)MyBatis代码自动生成和辅助插件
[03] IDEA集成MyBatis Generator 插件 详解
[04] generatorConfig.xml详解
[05] generator-plugin错误汇总

微信扫一扫关注公众号
使用mybatis-generator自动生成代码
点击链接加入群聊

https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105