Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate common mapper from Mybatis Generator in springboot Development of single web shop

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to generate common mapper from Mybatis Generator in springboot development monomer web shop, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Mybatis Generator tool

After we start the development of a new project, we usually have to write a lot of entity/pojo/dto/mapper/dao...,. Most of our R & D brothers will complain, "Why should I repeat CRUD?" In order to avoid writing some unnecessary repetitive code. Let me introduce you to the use of an open source tool to help us save from this simple and boring coding. Solemnly welcome: MyBatis General Mapper4 > General Mapper can greatly facilitate developers. You can choose a general method according to your own needs at will, and you can also easily develop your own general method. It is extremely convenient to use the addition, deletion, modification and query of MyBatis single table. Single table operations are supported, but general multi-table federated queries are not supported. General Mapper supports Mybatis-3.2.4 and above. Tips: technical colleagues must be aware of the version.

Create mybatis-generator-tool Module

Add dependency

Expensive-shop com.life-runner 1.0-SNAPSHOT 4.0.0 mybatis-generator-tool UTF-8 org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis- Generator-maven-plugin 1.3.6 ${basedir} / src/main/resources/generator/generatorConfig.xml true true Mysql mysql-connector-java 8.0.16 tk.mybatis Mapper 4.1.5

To write the configuration file according to the path we specified in the pom file: ${basedir} / src/main/resources/generator/generatorConfig.xml, we need to create a generator folder under the project src= > main= > resource directory and a file generatorConfig.xml under the folder, as follows:

We can see a line of configuration content: here is to externalize our database connection, account and other information. The configuration content is as follows:

Jdbc.driverClass = com.mysql.cj.jdbc.Driverjdbc.url = jdbc:mysql://localhost:3306/expensiveshop?characterEncoding=UTF-8&useSSL\ = false&useUnicode=true&serverTimezone=UTCjdbc.user = rootjdbc.password = 12345678

You can see that the content set here is used in the subordinate code.

......

For configuration information, please refer to: portal

Use maven test generation to execute the following commands:

Mybatis-generator-tool > mvn mybatis-generator:generate [INFO] Scanning for projects... [INFO] [INFO]-

< com.life-runner:mybatis-generator-tool >

-[INFO] Building mybatis-generator-tool 1.0-SNAPSHOT [INFO]-- [jar]-- [INFO] [INFO]-mybatis-generator-maven-plugin:1 .3.6: generate (default-cli) @ mybatis-generator-tool-[INFO] Connecting to the Database [INFO] Introspecting table carousel [INFO] Introspecting table category... [INFO] Generating Record class for table carousel [INFO] Generating Mapper Interface for table carousel [INFO] Generating SQL Map for table carousel... [INFO] Saving file CarouselMapper.xml... [INFO] Saving file Carousel.java [INFO] Saving file Users.java... [WARNING] Table configuration with catalog null Schema null And table shop_users did not resolve to any tables [INFO]-[INFO] BUILD SUCCESS [INFO]- -[INFO] Total time: 1.374 s [INFO] Finished at: 2019-11-05T15:40:07+08:00 [INFO]-- -

You can see that the execution was successful, although it was successful here, but when we open the file, we will find:

Package com.liferunner.pojo;import java.util.Date;import javax.persistence.*;import lombok.Getter;import lombok.Setter;import lombok.ToString;@Getter@Setter@ToString@Table (name = "Carousel") public class Carousel {/ * / / Id private String id; / @ Id private String id; / * / / private String imageUrl;.}

There is a garbled problem here, what is going on here? It's okay, let's bing.... Portal, you can see that there are 265000 results, which means that too many people have encountered our problem. Click one at random: you can see that the content in the red box is missing. Add it to\ expensive-shop\ mybatis-generator-tool\ src\ main\ resources\ generator\ generatorConfig.xml, re-execute the generation command, and you can see that our garbled code is gone.

@ Getter@Setter@ToString@Table (name = "`carousel`") public class Carousel {/ * primary key * / @ Id @ Column (name = "`id`") private String id; / * picture address * / @ Column (name = "`carousel`") private String imageUrl;...

> Tips: > play through a bug first in this section, otherwise I am worried that when you encounter it later, because it is really related to Common Mapper generation.

When we click on the generated Users.java, we can see the following:

@ Getter@Setter@ToString@Table (name = "users") public class Users {@ Column (name = "USER") private String user; @ Column (name = "CURRENT_CONNECTIONS") private Long currentConnections; @ Column (name = "TOTAL_CONNECTIONS") private Long totalConnections;}

But our Users watch is not like this, what's going on? Let's analyze: 1. Since we didn't use our own Users table, but we did generate it through the generator, it's clear that it's the table in the Mysql database, that's for sure. two。 So the question is, where did it come from? Find it, disk it. 3. Which database is it from? Sys? Information_schema? Performance_schema? 4. Inquire one by one, and sure enough:

As you can see, there is a users table in the performance_schema database, so did we generate it or not? Execute SHOW CREATE TABLE users, the result is like the figure above, the fields are consistent with the generated ones! 5. Caught it, how to disk it? > it's simple to modify jdbc:mysql://localhost:3306/expensiveshop?nullCatalogMeansCurrent=true&characterEncoding=UTF-8&useSSL.

= false&useUnicode=true&serverTimezone=UTC, just add the bold part.

The literal meaning of nullCatalogMeansCurrent is very simple, that is, if it is null catalog, I will choose current. Because mysql does not support catalog, we need to inform mybatis of this feature and set it to true. > according to the interpretation of the SQL standard, both Catalog and Schema are abstract concepts in SQL environment, which are mainly used to solve naming conflicts. Conceptually, a database system contains multiple Catalog, each Catalog contains multiple Schema, and each Schema contains multiple database objects (tables, views, sequences, etc.). In turn, a database object must belong to a Schema, and the Schema must belong to a Catalog, so that we can get the fully qualified name of the database object and solve the problem of naming conflicts. Various database systems have different support and implementation methods for Catalog and Schema. To solve specific problems, you need to refer to the specific product specification. The relatively simple and common way of implementation is to use the database name as the Catalog name and the Oracle user name as the Schema name.

We explained how to generate the simple, important and repetitive work we want. We can do it through tools, and next time we will start coding the actual business. Gogogo. The above is how to generate common mapper from Mybatis Generator in a single web shop developed by springboot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report