I have two projects (one project Referenced in the form of a jar package.jar project can be referenced by multiple projects). mybatis interface code is written in the jar project(common project). datebase properties at war project.
In the beginning, all the actual operations of the database were called in the war project, but now it is necessary to directly obtain the database data in the jar project Now I want to do query database operations in the jar project.how should i write? (Can I pass the interface class as a parameter into the jar project?)

I can only configure the database connection in the war project.What parameters should I pass to the jar project so that I can implement database query operations in the jar project.ask for advice!

20230410144912.png

Dani AI

Generated

This setup (common jar with MyBatis mapper interfaces, WAR that holds the datasource) is a normal, recommended pattern. As described, keep configuration (DB URL/username/password and the DataSource bean) in the WAR and let Spring create the connection. Do not try to "pass mapper Class objects" around as a long-term pattern — use Spring dependency injection so mapper beans or SqlSession objects are provided to components in the common jar.

Two practical approaches to implement queries from the jar:

Option A — library-style jar (preferred)

  • Put MyBatis mapper interfaces and service classes (annotated @Component/@Service) in the jar.
  • In the WAR enable mapper scanning for the jar package so MyBatis creates mapper beans. The jar services simply inject mappers:
@Service
public class CommonService {
  private final UserMapper userMapper;
  public CommonService(UserMapper userMapper) {
    this.userMapper = userMapper;
  }
}

Option B — jar provides configuration

  • Put a @Configuration in the jar that uses MyBatis mapper scanning but do not create a DataSource there. The WAR supplies the DataSource bean. The WAR either component-scans or imports that configuration.

Troubleshooting checklist (ties to 's runtime note)

  • Ensure the common jar is on the runtime classpath (Maven/Gradle dependency or in WEB-INF/lib).
  • Verify @MapperScan/component-scan packages include the jar packages.
  • Look for "No qualifying bean" or mapper-not-found errors on startup and check logs for mapper registration.
  • If you need programmatic access, inject SqlSessionTemplate and call getMapper(Class).

See the Spring Boot reference for wiring beans and the MyBatis Spring docs for mapper integration:

Recommended Answers

All 2 Replies

I'm confused now how do I go about writing these codes

Place the database jar somewhere in your CLASSPATH, andimport the classes you need in the other projects do you can use them directly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.