Teach you how to integrate Springboot+Maven+Mybatis with IDEA

IDEA teaches you how to integrate Springboot+mybatis+maven

Recently, I encountered a series of problems when trying to write an introductory springboot project with IDEA. I read a lot of articles on the Internet, but I didn't find a perfect solution, so I plan to write a little white tutorial and take this opportunity to consolidate it.

I Project creation

  1. File->new ->project

  2. Click Spring Initializr in the pop-up interface, and then you can modify the name of the project according to your own needs. The SDK and java versions are based on your own actual development environment. Here I use jdk1 8. The default packaging method is jar package. Click next

  3. Check JDBC,Mybatis and Mysql under Spring Web Template, thymeleaf and SQL

  4. Click finish to complete the creation of the project. The project directory is shown below

  5. Apply The property suffix is changed to YML and becomes application yml

  6. application. Content of YML

  7. Create new mapper and generator folders under the resources folder. The former is used to store the generated by Mybatis xml file, which is put into the configuration file of Mybatis

  8. Create a new generatorconfig. In the generator folder xml, the code of this xml file is shown at the end of the article

  9. In POM Add a plugin tag into XML, as shown in the figure, and the code is as follows

 <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.1</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
 </plugin>

It should be noted that the configurationFile here corresponds to the location of the newly created xml file. If your configuration file is not in this directory, you need to modify it

  1. Finally, for the above generatorconfig XML, the first is its code content
<?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>
<!-- Database driven:Select the database driver package on your local hard disk-->
<classPathEntry  location="D:\Program Files\maven-repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar"/>
<context id="DB2Tables"  targetRuntime="MyBatis3">
   <commentGenerator>
       <property name="suppressDate" value="true"/>
       <!-- Remove automatically generated comments true: Yes: false:no -->
       <property name="suppressAllComments" value="false"/>
   </commentGenerator>
   <!--Database connection driver class,URL,User name and password -->
   <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/demo "  userId="root" password="123456">
       <property name="nullCatalogMeansCurrent" value="true"></property>
   </jdbcConnection>
   <javaTypeResolver>
       <property name="forceBigDecimals" value="false"/>
   </javaTypeResolver>
   <!-- generate(entity)Package name and location of the model-->
   <javaModelGenerator targetPackage="com.example.demo.entity" targetProject="src">
       <property name="enableSubPackages" value="true"/>
       <property name="trimStrings" value="true"/>
   </javaModelGenerator>
   <!-- generate XML Package name and location of the mapping file-->
   <sqlMapGenerator targetPackage="main.resources.mapper" targetProject="src">
       <property name="enableSubPackages" value="true"/>
   </sqlMapGenerator>
   <!-- generate DAO Package name and location of the interface-->
   <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="src">
       <property name="enableSubPackages" value="true"/>
   </javaClientGenerator>
   <!-- Table to generate tableName Is the name of a table or view in the database domainObjectName Is the name of the entity class-->
   <table tableName="article" domainObjectName="Article" ></table>
   <table tableName="essay" domainObjectName="Essay" ></table>
   <table tableName="message" domainObjectName="Message" ></table>
   <table tableName="user" domainObjectName="User" ></table>
</context>
</generatorConfiguration>

There are several places in the code that need to be changed by yourself:
(1) Enter the address of the local driver package here, and you can usually find it in maven warehouse

<classPathEntry  location="D:\Program Files\maven-repository\mysql\mysql-connector-java\8.0.23\mysql-connector-java-8.0.23.jar"/>

(2) The demo here is the name of my database, followed by the corresponding user name and password

<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/demo "  userId="root" password="123456">
    <property name="nullCatalogMeansCurrent" value="true"></property>
</jdbcConnection>
```

(3)com. example. When the demo is actually open, it should be changed to the real project name

II How to run mybatis

1. Click Edit Configurations as shown in the figure

2. Click the plus sign as shown in the figure and select Maven


3. Fill in mybatis generator: generate - E in the Command field, and write mybatis (or any other Name) in the Name field, and then click OK

4. Select mybatis at the position shown in the figure and click Run

Then the corresponding entity class and mapper file will be generated

5. The project directory at this time is shown in the figure

You will find that the mapper file under resources is generated correctly, but there is nothing under the entity and dao folders. At this time, it is on COM example. Create a new dao and entity package under demo, and move the entity class and mapper generated above to the entity and dao package

Note: in actual operation, if generatorconfig Com. In XML example. demo. Change entity to main java. com. example. demo. Dao can be directly generated to the final location, but there will be a problem
The package in the automatically generated code is main java. com. example. demo. Entity needs to be changed manually, so it's better to generate it according to the above method and transfer it directly
My arm is tired. If you are interested in this part, try it yourself

The first blog post will inevitably make mistakes and omissions. Criticism and correction are welcome

Keywords: Java Maven Mybatis

Added by genius_supreme on Sat, 05 Mar 2022 03:49:56 +0200