MyBatis-Plus_04 code generator, multiple data sources (master-slave), MyBatisX plug-in

Directory

①. Code generator

②. Multiple data sources (master-slave)

③. MyBatisX

①. Code generator

  • Add code generator dependencies
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.1</version>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version>
</dependency>
  • quick build
//mybatis-plus code generator
public class MyBatisPlusGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/mybatis_plus?characterEncoding=utf-8 & amp;userSSL=false", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("songqiao") // set the author
                            //.enableSwagger() // Enable swagger mode
                            .fileOverride() // Overwrite generated files
                            .outputDir("C://mybatis_plus"); // specify the output directory
                })
                .packageConfig(builder -> {
                    builder.parent("com.songqiao") // set parent package name
                            .moduleName("mybatisplus") // Set the parent package module name
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "C://mybatis_plus"));
                    // Set mapperXml generation path
                })
                .strategyConfig(builder -> {
                    builder.addInclude("t_student") // Set the table name that needs to be generated
                            .addTablePrefix("t_", "c_"); // Set filter table prefix
                })
                .templateEngine(new FreemarkerTemplateEngine()) // Use the Freemarker engine template, the default is the Velocity engine
                .execute();
    }
}

②. Multiple data sources (master-slave)

Applicable to a variety of scenarios: pure multi-library, read-write separation, one master and multiple slaves, mixed mode, etc. Now we will simulate a pure multi-library scenario, and other scenarios are similar. Scenario description:

We create two libraries, namely: mybatis_plus (the previous library does not move) and mybatis_plus_1 (new), and move the product table of the mybatis_plus library to the mybatis_plus_1 library, so that each library has a table and obtains user data through a test case With commodity data, if it is obtained, it means that the multi-library simulation is successful.

  • ①. Introduce dependencies
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.5.0</version>
</dependency>
  • ②. Configure multiple data sources
spring:
# Configure data source information
    datasource:
        dynamic:
        # Set the default data source or data source group, the default value is master
        primary: master
        # Strictly match the data source, the default is false. true throws an exception when the specified data source is not matched, and false uses the default data source
        strict: false
        datasource:
            master:
                url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-
8 &useSSL=false
                driver-class-name: com.mysql.cj.jdbc.Driver
                username: root
                password: 123456
            slave_1:
                url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-
8 &useSSL=false
                driver-class-name: com.mysql.cj.jdbc.Driver
                username: root
                password: 123456
  • ③. Create User Service
public interface UserService extends IService<User> {
}

@DS("master") //Specify the data source to be operated
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements
UserService {
}
  • ④. Create product service
public interface ProductService extends IService<Product> {
}

@DS("slave_1")
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
implements ProductService {
}
  • ⑤. Test
@Autowired
private UserService userService;
@Autowired
private ProductService productService;
@Test
public void testDynamicDataSource(){
    System.out.println(userService.getById(1L));
    System.out.println(productService.getById(1L));
}

result:

  1. If the object can be successfully obtained, the test is successful
  2. If we achieve read-write separation, add the write operation method to the master database data source, and the read operation method to the slave database data source, and automatically switch, will it be possible to achieve read-write separation?

③.MyBatisX

MyBatis-Plus provides us with powerful mapper and service templates, which can greatly improve development efficiency

But in the real development process, MyBatis-Plus can not solve all problems for us, such as some complex SQL, multi-table

For joint investigation, we need to write code and SQL statements by ourselves. How can we quickly solve this problem? At this time, we can

to use the MyBatisX plugin

MyBatisX is an IDEA-based rapid development plug-in, born for efficiency.

  • ①. Download the plug-in

Installation method: Open IDEA, go to File -> Settings -> Plugins -> Browse Repositories, enter mybatisx to search and install.

7ce37d65c82b40b9923fa4629490d387.png

  • ②. Add data source inside IDEA

28502daedbc0494fb4ab6632031220a6.png

  • ③. Code generation

522989192ac858e25bad2f5defb5a1b3.gif

  • ④. JPA prompt: generate new

eaf03150ba6cdf583b6e437cadac8769.gif

  • ⑤. Generate query

628a4c619e08947bd95aeea46fdd3236.gif

  • ⑥. Generate modification

d369f2a14203f0fae5105b55a2074e6b.gif

  • ⑦. Generate delete

10ffcf8d9acb3c3204a3b5c05e70ceb9.gif