maven java project configuration dual database (multi database configuration)

Keywords: Java Database Maven

preface

Why configure dual or multiple databases.
When you need to obtain or insert data from two or more databases, the database driver, url path, username and password of each database are different. If you follow the configuration of a single database, you can only obtain the content of one database.

explain

According to my method, it is configured in the case of pure java class projects (involving maven)

How to configure

Step 1: import Pom.xml dependency

<!--       Create database xml Package required for configuration file -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>

<!-- 	Import the corresponding driver package according to the database type. The package is hana Database-->
        <dependency>
            <groupId>com.sap.cloud.db.jdbc</groupId>
            <artifactId>ngdbc</artifactId>
            <version>2.7.15</version>
        </dependency>

<!--	The package is mysql Database-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>      
If you encounter a place marked with red, Baidu can search the corresponding red code to find the dependency import, which is only given here
 The most important dependence does not mean that these are necessarily enough

Step 2: create the corresponding project name

Describe with my project:
api: function implementation
mapper: implementation method of writing database
model: entity class
utils: tool class
Resource: for resource configuration, the main configuration of dual databases is here. If you want to configure multiple databases, you can add them up in turn

Take my example:
Create a mapper folder in the resource folder
In the mapper folder, create two mapper files for database addition, deletion, modification and query.

Create two database configuration files under resource

The structure is as follows:
–resource
-----mapper
---------------mapper_hana.xml
---------------mapper_sql.xml

–mapper-config-hana.xml

–mapper-config-sql.xml

Step 3: write mapper addition, deletion, modification and query files

Among them. mapper_hana.xml and mapper_ The sql.xml file code is as follows:

mapper_hana.xml code
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.db1.AssistantMapper_hana">

<!--    Find sender-->
    <select id="findSendUser" resultType="com.model.AssistantEngineer">

    </select>

</mapper>
Among them, namespace Add, delete, change and query the implementation path for the mapped database,
<select id="" resultType=""></select>
<select></select>Query representing database
id Find the corresponding content for the identified 
resultType Is the mapped entity class
 For more specific instructions, please study springboot Relevant knowledge
mapper_sql.xml code
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.db2.AssistantMapper_sql">

<!--    Find sender-->
    <select id="findSendUser" resultType="com.model.AssistantEngineer">

    </select>

</mapper>
Among them, namespace Add, delete, change and query the implementation path for the mapped database,
<select id="" resultType=""></select>
<select></select>Query representing database
id Find the corresponding content for the identified 
resultType Is the mapped entity class
 For more specific instructions, please study springboot Relevant knowledge

Step 4: write database configuration file

mapper-config-hana.xml The code is as follows:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        <package name="com.model"/>
    </typeAliases>

    <environments default="development">
        <environment id="hr">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.sap.db.jdbc.Driver"/>
                <property name="url" value="url value"/>
                <property name="username" value="user name"/>
                <property name="password" value="password"/>
            </dataSource>
        </environment>

    </environments>
    <mappers>
        <mapper resource="mapper/AssistantMapper_hana.xml"/>
    </mappers>
</configuration>
Among them,<TypeAliases>Is the mapped entity class
<dataSource>Database configured for
<mapper resource/>Add, delete, change and query files for the mapped database
mapper-config-sql.xml The code is as follows:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        <package name="com.model"/>
    </typeAliases>

    <environments default="development">
        <environment id="mes">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="url value"/>
                <property name="username" value="user name"/>
                <property name="password" value="password"/>
            </dataSource>
        </environment>

    </environments>
    <mappers>
        <mapper resource="mapper/AssistantMapper_sql.xml"/>
    </mappers>
</configuration>
Among them,<TypeAliases>Is the mapped entity class
<dataSource>Database configured for
<mapper resource/>Add, delete, change and query files for the mapped database

Step 5: create an entity class

In my project, the model folder is used as the path to store entity classes

The structure is as follows:

Among them. The content of assistanteengineer.java is the entity corresponding to the database field.
Code examples are as follows:

@Data
public class AssistantEngineer {
    private String name;
    private int age;
}

Among them,@Data Annotations need to be imported lombok,It mainly explains how to configure dual databases, so it is omitted

The sixth step is to write the implementation of database addition, deletion, modification and query

Using mapper as the implementation, my project structure is as follows (as long as the mapped path corresponds, it can be created no matter how)

Among them, assistant mapper_ Hana code is as follows:

public interface AssistantMapper_hana {
// Use multiple lists based on query < >
// Otherwise, you can use assistant engineer findsenduser();
    List<AssistantEngineer> findSendUser();
}

summary

The above is the specific way to configure multiple databases. Welcome to learn from it

Posted by Comtemplative on Wed, 10 Nov 2021 19:45:40 -0800