您的位置:首页 > 编程语言 > Java开发

SpringBoot从安装到实战(七)整合MyBatis并使用MyBatis Generator(已解决生成xml不覆盖以及生成其他数据库的同名表问题)

2020-06-05 07:42 701 查看

SpringBoot从安装到实战(七)整合MyBatis并使用MyBatis Generator(已解决生成xml不覆盖以及生成其他数据库的同名表问题)

前言

此教程会与教程第五篇 起冲突,若读过前篇的读者可创建新的项目来避免冲突。

MyBatis

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。

MyBatis Generator

MyBatis Generator (MBG) is a code generator for MyBatis MyBatis. It will generate code for all versions of MyBatis. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s). This lessens the initial nuisance of setting up objects and configuration files to interact with database tables. MBG seeks to make a major impact on the large percentage of database operations that are simple CRUD (Create, Retrieve, Update, Delete). You will still need to hand code SQL and objects for join queries, or stored procedures.

MyBatis生成器(MBG)是MyBatis MyBatis的代码生成器。它将为MyBatis的所有版本生成代码。它将对一个(或多个)数据库表进行内部检查,并将生成可用于访问表的工件。这减轻了设置对象和配置文件以与数据库表进行交互的麻烦。MBG试图对简单CRUD(创建,检索,更新,删除)的大部分数据库操作产生重大影响。您仍将需要手工编写SQL和对象代码以进行联接查询或存储过程。

MyBatis 与hibernate的区别

Hibernate和MyBatis的区别

1.添加依赖以及MyBatis Generator插件

pom.xml

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<!-- 添加一个mysql的依赖,防止等会找不到driverClass -->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<!-- mybatisGenerator 的配置 -->
<configuration>
<!-- generator 工具配置文件的位置 -->
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<!-- 是否覆盖 -->
<!-- 此处要特别注意,如果不加这个设置会导致每次运行都会在原目录再次创建-->
<overwrite>true</overwrite>
</configuration>
</plugin>

2.新建application.yml配置MyBatis

注意

mapper-locations可以指定复数存放mapper.xml位置

3.新建generatorConfig.xml配置MyBatis Generator

在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>
<context id="MySQLTables" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 重要!!! 用于设置当使用mybatis generator 时,覆盖旧有mapper.xml而不是合并 -->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!-- 给每个实体类重载tostring方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 公共设置 -->
<commentGenerator>
<!-- 是否取消自动生成时的注释 -->
<property name="suppressAllComments" value="true"/>
<!-- 是否取消在注释中加上时间 -->
<property name="suppressDate" value="true"/>
</commentGenerator>

<!-- 链接数据库的配置 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/tacocloud?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT%2B8&amp;useSSL=false" userId="root" password="852025">
<!-- 只允许生成当前数据库的表 -->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>

<!-- 关于生成实体类的设置 -->
<!-- targetPackage 生成代码的目标目录 -->
<!-- targetProject 目录所属位置 -->
<javaModelGenerator targetPackage="tacos.mbg.model" targetProject="src/main/java">
<!-- 在targetPackge的基础上根据schema再生成一层package 默认flase -->
<property name="enableSubPackages" value="true"/>
<!-- 是否在get方法中 对String类型的字段做空的判断 -->
<property name="trimStrings" value="true"/>
<!-- 是否生成一个包含所有字段的构造器 -->
<property name="constructorBased" value="false"/>

<!-- 是否创建一个不可变类-->
<property name="immutable" value="false"/>
</javaModelGenerator>

<!--关于生成映射文件的设置-->
<sqlMapGenerator targetPackage="mbg.mapper" targetProject="src/main/resources">
<!--同上-->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

<!--关于生成dao层的设置-->
<javaClientGenerator type="mapper" targetPackage="tacos.mbg.mapper" targetProject="src/main/java">
<!--同上-->
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>

<!--需要生成的代码对应的表名-->
<table tableName="ingredient"></table>
<table tableName="taco"></table>
<table tableName="taco_ingredients"></table>
<table tableName="taco_order"></table>
<!--  <table tableName="user"></table> -->
<table tableName="taco_order_tacos"></table>
</context>
</generatorConfiguration>

4.配置Mybatis

新建MybatisConfig类

package tacos;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("tacos.mbg.mapper")
public class MybatisConfig {

}

5.运行generator插件

右键项目->Run As->Maven Build

填写Goals输入框,运行。

成功

可以看到generator已经帮我们自动建好model,mapper,mapper.xml

6.测试

package tacos.mapper;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import tacos.mbg.mapper.IngredientMapper;
import tacos.mbg.model.IngredientExample;

@SpringBootTest
@ExtendWith(SpringExtension.class)
public class IngredientMapperTest {

@Autowired
private IngredientMapper ingredientMapper;
@Test
public void test() {
IngredientExample ingredientExample=new IngredientExample();

ingredientMapper.selectByExample(ingredientExample).forEach(x->System.out.println(x.toString()));
}
}


测试成功

排错

如果出现了错误的话,请按以下顺序排查

  1. pom.xml文件下mybatis.generator版本是否为1.3.7
  2. generatorConfig.xml是否存在UnmergeableXmlMappersPlugin
  3. generatorConfig.xml的jdbcConnection是否有配置nullCatalogMeansCurrent
  4. application.yml是否正确配置好mapper.xml的映射路径
  5. 配置类是否有注解@MapperScan(“tacos.mbg.mapper”)

完整目录

文章参考

MyBatis Generator 生成器把其他数据库的同名表生成下来的问题

MyBatis Generator使用过程中踩过的一个坑

SpringBoot2.0整合Mybatis之逆向工程(eclipse环境)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐