ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot | 集成mybatis代码生成器

2022-05-07 13:01:40  阅读:149  来源: 互联网

标签:代码生成 SpringBoot generator int Demo record demo mybatis import


image

首先,创建一个数据库表demo,添加一些数据。

drop table if exists `demo`;
create table `demo` (
    `id` bigint not null comment 'id',
    `name` varchar(50)comment'名称',
    primary key (`id`)
)engine=innodb default charset=utf8mb4 comment='测试';

insert into `demo`(id,name)values(1,'测试');

分析:自动生成器会根据生成对应的实体类mapper接口mapper映射文件

1.添加插件

<!-- mybatis generator 自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <configurationFile>src/main/resources/generator/generator-config.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.22</version>
                    </dependency>
                </dependencies>
            </plugin>

2.创建文件 generator-config.xml

src/main/resources/generator下创建generator-config.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"/>
        <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/wiki?characterEncoding=UTF8&amp;autoReconnect=true&amp;serverTimezone=UTC"
                        userId="wiki"
                        password="wiki">
        </jdbcConnection>

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

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

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

        <!--<table tableName="demo" domainObjectName="Demo"/>-->
        <!--<table tableName="ebook"/>-->
        <!--<table tableName="category"/>-->
        <!--<table tableName="doc"/>-->
        <!--<table tableName="content"/>-->
        <!--<table tableName="user"/>-->
       <table tableName="demo" domainObjectName="Demo" />
    </context>
</generatorConfiguration>

其中,这两处的包名(targetPackage)根据实际情况修改:

<!-- domain类的位置 -->
<javaModelGenerator targetProject="src\main\java"
					targetPackage="com.nathan.wiki.domain"/>
<!-- mapper类的位置 -->
<javaClientGenerator targetProject="src\main\java"
					 targetPackage="com.nathan.wiki.mapper"
					 type="XMLMAPPER"/>

其中的connectionURLuserIdwiki也根据实际情况修改:

     <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/wiki?characterEncoding=UTF8&amp;autoReconnect=true&amp;serverTimezone=UTC"
                        userId="wiki"
                        password="wiki">
        </jdbcConnection>

上述代码会将数据库表生成对应的四个文件:实体类、案例类、mapper接口、mapper映射。

image

3.编译运行

1、配置maven命令。

image

2、编译生成。

启动命令,将会自动生成上述四个文件。
image

4.案例测试

案例-使用generator生成的文件,获取数据库表(demo)的所有信息。

1、创建service接口。

package com.nathan.wiki.service;

import com.nathan.wiki.domain.Demo;
import com.nathan.wiki.mapper.DemoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DemoService {

    @Resource
    private DemoMapper demoMapper;

    public List<Demo> list() {
        //  selectByExample 是 自动生成的Mapper接口中的方法、
        return demoMapper.selectByExample(null);

    }
}

分析: selectByExample对应sql语句位于自动生成的DemoMapper.xml中。

  <select id="selectByExample" parameterType="com.nathan.wiki.domain.DemoExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from demo
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>

更多方法如下。

long countByExample(DemoExample example);

int deleteByExample(DemoExample example);

int deleteByPrimaryKey(Long id);

int insert(Demo record);

int insertSelective(Demo record);

List<Demo> selectByExample(DemoExample example);

Demo selectByPrimaryKey(Long id);

int updateByExampleSelective(@Param("record") Demo record, @Param("example") DemoExample example);

int updateByExample(@Param("record") Demo record, @Param("example") DemoExample example);

int updateByPrimaryKeySelective(Demo record);

int updateByPrimaryKey(Demo record);

2、创建Controller。

package com.nathan.wiki.controller;

import com.nathan.wiki.domain.Demo;
import com.nathan.wiki.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

//使用restController
@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource
    private DemoService demoService;

    @GetMapping("/list")
    public List<Demo> list(){
        return demoService.list();
    }

}

3、http测试

demo.http中。

GET http://localhost:8080/demo/list

结果:
image

标签:代码生成,SpringBoot,generator,int,Demo,record,demo,mybatis,import
来源: https://www.cnblogs.com/martin-1/p/16242039.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有