Table of Contents
springboot integrates Mybatis
Use the reverse engineering provided by Mybatis to generate entity beans, mapping files, and DAO interfaces
Home Java javaTutorial How to use Mybatis in springboot

How to use Mybatis in springboot

May 10, 2023 pm 09:10 PM
mybatis springboot

springboot integrates Mybatis

Step one:

Add Mybatis dependency

<!--mybatis整合springboot框架的起步依赖-->
<dependency>
    <groupid>org.mybatis.spring.boot</groupid>
    <artifactid>mybatis-spring-boot-starter</artifactid>
    <version>2.0.0</version>
</dependency>
Copy after login

Step two:

Add mysql driver
The reason why there is no version number is because it inherits from the parent project. Of course, you can also specify a version number yourself

<!--添加mysql驱动-->
<dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
    <!-- 指定版本号 <version>5.1.9<version> -->
</dependency>
Copy after login

How to use Mybatis in springboot

Use the reverse engineering provided by Mybatis to generate entity beans, mapping files, and DAO interfaces

Step one:

Create the GeneratorMapper.xml file in the project root directory with the following configuration:

How to use Mybatis in springboot

<?xml  version="1.0" encoding="utf-8"?>
nbsp;generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorconfiguration>
    <!--指向连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径-->
    <classpathentry></classpathentry>
    <!--配置table表信息内容体,targetRuntime 指定采用MyBatis3的版本-->
    <context>
        <commentgenerator>
            <property></property>
        </commentgenerator>
        <!--配置数据库连接信息-->
        <jdbcconnection>
        </jdbcconnection>
        <!--生成Model类,targetPackage指定model类的包名,
        targetProject指定生成的model类放在eclipse的哪个工程下边、-->
        <javamodelgenerator>
            <property></property>
            <property></property>
        </javamodelgenerator>
        <!--生成Mybatis的Mapper.xml 文件,targetPackage指定mapper.xml文件的包名,
        targetProject指定生成的mapper.xml放在eclipse的哪个工程下边-->
        <sqlmapgenerator>
            <property></property>
        </sqlmapgenerator>
        <!--生成Mybatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名,
        targetProject指定生成的Mapper接口放在eclipse的哪个工程下边-->
        <javaclientgenerator>
            <property></property>
        </javaclientgenerator>
 
        <!--数据库表名及对应的Java模型类名
        有100张表,就需要指定100个table
        tableName:数据库中表的名字;
        domainObjectName:表对应生成的实体类的名字叫什么
        -->
        <table></table>
    </context>
</generatorconfiguration>
Copy after login

The second step
is configured as follows in pom.xml:

<!--在plugins标签中,添加如下代码-->
<!--mybatis 代码自动生成插件-->
<plugin>
    <groupid>org.mybatis.generator</groupid>
    <artifactid>mybatis-generator-maven-plugin</artifactid>
    <version>1.3.7</version>
    <dependencies>
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>8.0.15</version>
        </dependency>
    </dependencies>
    <configuration>
        <!--配置文件的位置-->
        <configurationfile>GeneratorMapper.xml</configurationfile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>
Copy after login

The third step
When double-clicking the following to execute,

How to use Mybatis in springboot

There is a pit here. Mine reported an error here. The error message is as follows:

Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.7: generate (default-cli) on project sprint_boot_01: Communications link failure

How to use Mybatis in springboot

99% is because the connectionURL in the driver and configuration database information is configured incorrectly. My solution:

driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/java_pro?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false"
Copy after login

Then double-click to execute, success:

How to use Mybatis in springboot

The generated directory is as follows:

How to use Mybatis in springboot

The above is the detailed content of How to use Mybatis in springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
iBatis vs. MyBatis: Which one is better for you? iBatis vs. MyBatis: Which one is better for you? Feb 19, 2024 pm 04:38 PM

iBatis vs. MyBatis: Which should you choose? Introduction: With the rapid development of the Java language, many persistence frameworks have emerged. iBatis and MyBatis are two popular persistence frameworks, both of which provide a simple and efficient data access solution. This article will introduce the features and advantages of iBatis and MyBatis, and give some specific code examples to help you choose the appropriate framework. Introduction to iBatis: iBatis is an open source persistence framework

Comparative analysis of the functions and performance of JPA and MyBatis Comparative analysis of the functions and performance of JPA and MyBatis Feb 19, 2024 pm 05:43 PM

JPA and MyBatis: Function and Performance Comparative Analysis Introduction: In Java development, the persistence framework plays a very important role. Common persistence frameworks include JPA (JavaPersistenceAPI) and MyBatis. This article will conduct a comparative analysis of the functions and performance of the two frameworks and provide specific code examples. 1. Function comparison: JPA: JPA is part of JavaEE and provides an object-oriented data persistence solution. It is passed annotation or X

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Various ways to implement batch deletion operations in MyBatis Various ways to implement batch deletion operations in MyBatis Feb 19, 2024 pm 07:31 PM

Several ways to implement batch deletion statements in MyBatis require specific code examples. In recent years, due to the increasing amount of data, batch operations have become an important part of database operations. In actual development, we often need to delete records in the database in batches. This article will focus on several ways to implement batch delete statements in MyBatis and provide corresponding code examples. Use the foreach tag to implement batch deletion. MyBatis provides the foreach tag, which can easily traverse a set.

Detailed explanation of how to use MyBatis batch delete statements Detailed explanation of how to use MyBatis batch delete statements Feb 20, 2024 am 08:31 AM

Detailed explanation of how to use MyBatis batch delete statements requires specific code examples. Introduction: MyBatis is an excellent persistence layer framework that provides rich SQL operation functions. In actual project development, we often encounter situations where data needs to be deleted in batches. This article will introduce in detail how to use MyBatis batch delete statements, and attach specific code examples. Usage scenario: When deleting a large amount of data in the database, it is inefficient to execute the delete statements one by one. At this point, you can use the batch deletion function of MyBatis

Detailed explanation of MyBatis cache mechanism: understand the cache storage principle in one article Detailed explanation of MyBatis cache mechanism: understand the cache storage principle in one article Feb 23, 2024 pm 04:09 PM

Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When

Comparison of similarities and differences between iBatis and MyBatis: comparison of mainstream ORM frameworks Comparison of similarities and differences between iBatis and MyBatis: comparison of mainstream ORM frameworks Feb 19, 2024 pm 07:08 PM

iBatis and MyBatis are two mainstream ORM (Object-Relational Mapping) frameworks. They have many similarities in design and use, but also have some subtle differences. This article will compare the similarities and differences between iBatis and MyBatis in detail, and illustrate their characteristics through specific code examples. 1. The history and background of iBatis and MyBatis iBatis is Apache Software Foundat

MyBatis Generator configuration parameter interpretation and best practices MyBatis Generator configuration parameter interpretation and best practices Feb 23, 2024 am 09:51 AM

MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.

See all articles