Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
1. Introduction
Code is not just poetry and distance, but CRUD in front of you
Before graduation, I thought Coding was like this: “Today’s task is performance optimization, tomorrow’s task is JVM tuning, yesterday the leader praised me for writing good code, I am really a little genius.”
However, the fact is that these are after-the-fact remedies and should be a good habit to avoid during the programming phase, as well as the endless CRUD
For Mybatis, there is also Mybatis Generator to generate the code. For JPA or other partners, there is also Mybatis Generator to generate the code. Dao is not required, but Service, ServiceImpl, and Controller are required
In order to solve the above problem, I searched around and found EasyCode, an open source IDEA plug-in developed by Chinese people. Its underlying logic is actually very simple, which is to generate code according to templates, which means that you can adapt to any posture according to custom templates
As a programmer who wants to leave work early every day, save the CRUD time, it is not delicious, this is based on the Mybatis template of EasyCode to generate a code, take you to further understand the convenience of this plug-in
2. Use of EasyCode
The code for section 2 has been uploaded to GitHub’s personal Demo project. The code path for this section is/wzED-demo /002-tools-demo/easycode-demo
Because the EasyCode template is used, so the dependency is in accordance with the code inside the template
Dependencies and versions covered in this article
-
MySQL 5.7
-
The IDEA of 2021.2
-
SpringBoot 2.5.4
-
See pom.xml for other dependencies
2.1 Project Preparation
Build an empty project first
And then introduce the dependencies that I need, because I inherited them when I built the projectSpring - the boot - starter - parent 2.5.4
, so only Mybatis dependencies need to be explicitly specified
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- Mybatis dependency configuration -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
Copy the code
2.2 Table Structure Preparation
Universal student table 1.0, favorite to use the student table
CREATE TABLE `student` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'Increment primary key',
`name` varchar(50) NOT NULL COMMENT 'name',
`age` int(11) NOT NULL COMMENT 'age'.PRIMARY KEY (`id`)
)
Copy the code
2.3 Use of plug-ins
First, install the plugin for IDEA, which is this thing
Then add the data source for IDEA
Right click the table and open the build window
In this case, you only need to select the correct package name and disk path, select the file to be generated, click unified configuration and Disable, and press OK
Add Mybatis configuration class MybatisConfig for scanning daOs
@Configuration
@MapperScan("cn.wzed.dao")
public class MybatisConfig {}Copy the code
Add the path to scan mapper. XML and the data source to the configuration file application.yml
server:
port: 9002
mybatis:
mapper-locations: classpath:mapper/**/**.xml
The console will print SQL logs after this function is enabled
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo
username: root
password: 123456
Copy the code
Add a startup class, start the project, and test whether the generated interface works
Perfect success. I haven’t seen such a perfect example since last time
3. Glance at the template
EasyCode all configuration is integrated in other Settings, here you can set the author name, template content, value mapping relationship with the database and so on, visual operation, mom no longer need to worry about my random generation code
4. Write in the back
There are many options for code generation tools, but EasyCode’s integration with IDEA, visual manipulation, templates and variables can accommodate a variety of complex generation scenarios, and the default built-in templates can greatly improve our efficiency in validating some use cases
The so-called time is money, you read my article, I help you save time, from another point of view I this is to save your life ah! Do you care too much about it?