How to use Mybatis in 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>
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>
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:
<?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>
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>
The third step
When double-clicking the following to execute,
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
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"
Then double-click to execute, success:
The generated directory is as follows:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

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

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

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 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 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

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

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.
