How to Build a SpringBoot Multi‑Module Scaffold with MyBatis and Shiro: Guide and Pitfalls
This guide walks through creating a SpringBoot multi‑module project integrated with MyBatis and Shiro, covering Maven module setup, dependency versions, bean scanning nuances, unit‑test annotations, custom Realm configuration, common pitfalls, and provides a demo repository for reference.
0. Introduction
The article describes building a lightweight scaffolding framework that emphasizes performance, learning new technologies, and rapid development of custom features, with a focus on permission control using Apache Shiro.
1. Technical Framework Integration
1.1 Maven Multi‑Module Project Setup
Multi‑module projects improve code organization, build speed, and enforce clean architecture. Reference implementations include:
zheng – https://gitee.com/shuzheng/zheng (Spring + SpringMVC + MyBatis distributed agile development system)
ES – https://github.com/zhangkaitao/es (JavaEE enterprise rapid‑development scaffold)
renren‑security – https://www.renren.io/ (lightweight permission management system)
lenos – https://gitee.com/bweird/lenosp (modular rapid‑development scaffold)
1.2 SpringBoot‑MyBatis Integration
Key references for integrating MyBatis with SpringBoot:
Spring Boot integration guide – https://blog.csdn.net/isea533/article/details/50359390
mybatis‑spring‑boot‑autoconfigure documentation – http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
1.3 SpringBoot‑Shiro Integration
Helpful articles for Shiro integration:
Elegant starter‑based Shiro integration – https://segmentfault.com/a/1190000014479154#articleHeader0
General SpringBoot‑Shiro guide (no URL provided)
Simple RBAC example with Realm – http://jinnianshilongnian.iteye.com/blog/2022468
2. Common Pitfalls
2.1 Version Compatibility
SpringBoot 2.0.3.RELEASE
JUnit 4.12
SpringBoot‑MyBatis 1.3.2
SpringBoot‑Shiro 1.4.0‑RC2
2.2 Multi‑Module Testing Considerations
When writing unit tests in a multi‑module project, specify the main application class in @SpringBootTest: @SpringBootTest(classes = {Application.class}) Bean scanning starts from the package of Application.java. Place this class in a common parent package so that beans in other modules are discovered.
MyBatis‑generator cannot reuse the main application.properties; provide a separate data‑source configuration file in the generator module.
2.3 Mapper/DAO Injection in Unit Tests
Annotate persistence interfaces with both @Mapper and @Repository to avoid IDE warnings; @Mapper alone is sufficient for injection.
@Repository
@Mapper
public interface RoleDao {
int deleteByPrimaryKey(Long id);
int insert(Role record);
int insertSelective(Role record);
int updateByPrimaryKeySelective(Role record);
int updateByPrimaryKey(Role record);
Set<Role> findAll();
Set<Role> findByUserId(Long userId);
}2.4 Custom Realm Bean Registration
Register Shiro beans in a @Configuration class. No @Component annotation is required on the custom Realm.
@Configuration
public class ShiroConfig {
@Bean
public Realm realm() {
return new MyRealm();
}
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chain = new DefaultShiroFilterChainDefinition();
// "/anon/*" works, "/anon" does not
chain.addPathDefinition("/anon/*", "anon");
chain.addPathDefinition("/authc/*", "authc");
return chain;
}
} // No @Component needed
public class MyRealm extends AuthorizingRealm {
@Autowired
private UserService userService;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// authentication logic
return null;
}
}2.5 URL Path Definition Gotchas
chain.addPathDefinition("/anon", "anon"); // ineffective
chain.addPathDefinition("/anon/*", "anon"); // effective
3. Demo Source Code
Complete demo project: https://github.com/deng-cc/baseMan-demo-init
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
