首先根据业务拆分成 Permission 和 Club 两个模块。然后在父工程中添加依赖:
# 1. SpringWeb 开发
# 1.1 添加依赖
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-webmvc</artifactId> |
| <version>5.2.22.RELEASE</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-test</artifactId> |
| <version>${spring.version}</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-jdbc</artifactId> |
| <version>${spring.version}</version> |
| </dependency> |
# 1.2 添加容器启动配置类
| public class PermissionWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { |
| @Override |
| protected Class<?>[] getRootConfigClasses() { |
| return new Class[]{SpringMvcConfig.class}; |
| } |
| |
| @Override |
| protected Class<?>[] getServletConfigClasses() { |
| return new Class[]{}; |
| } |
| |
| @Override |
| protected String[] getServletMappings() { |
| return new String[]{"/"}; |
| } |
| } |
《Spring 实战》
AbstractAnnotationConfigDispatcherServletInitializer 剖析
如果你坚持要了解更多细节的话,那就看这里吧。在 Servlet 3.0 环境中,容器会在类路径中查找实
现 javax.servlet.servletContainerInitializer 接口的类,如果能发现的话,就会用它来配置 Servet 容器。
Spring 提供了这个接口的实现,名为 SpringServletContainerInitializer,这个类反过来又会查找实现 webApplicationInitializer 的类并将配置的任务交给它们来完成。Spring 3.2 引入了一个便利的 WiebApplicationInitializer 基础实现,也就是 AbstractAnnotationConfigDispatcherServletInitializer。因为我们的 Spittr-WebAppInitializer 扩展了 AbstractAnnotationConfig DispatcherServlet-Initializer (同时也就实现了 webApplicationInitializer),因此当部署到 Servlet 3.0 容器中的时候,容器会自动发现它,并用它来配置 Servlet 上下文。
尽管它的名字很长,但是 AbstractAnnotationConfigDispatcherServlet-Initializer 使用起来很简便。在程序清单 5.1 中,SpittrwebAppInitializer 重写了三个方法。
# 2. Lombok
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <version>1.18.24</version> |
| </dependency> |
# 3. MyBatis
# 3.1 依赖导入
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis</artifactId> |
| <version>3.5.11</version> |
| </dependency> |
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis-spring</artifactId> |
| <version>3.0.1</version> |
| </dependency> |
| <dependency> |
| <groupId>mysql</groupId> |
| <artifactId>mysql-connector-java</artifactId> |
| <version>8.0.31</version> |
| </dependency> |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>druid</artifactId> |
| <version>1.2.12</version> |
| </dependency> |
# 3.2 新增 properties 配置文件
| url=jdbc:mysql://localhost:3306/club_manager_server |
| driverClassName=com.mysql.cj.jdbc.Driver |
| username= |
| password= |
# 3.3 配置 MyBatisConfig
| @Configuration |
| public class MybatisConfig { |
| |
| @Autowired |
| private DataSourceProperties dataSourceProperties; |
| |
| |
| * @return 配置并返回数据源 |
| */ |
| @Bean |
| public DataSource dataSource() { |
| DruidDataSource dataSource = new DruidDataSource(); |
| dataSource.setUrl(dataSourceProperties.getUrl()); |
| dataSource.setDriverClassName(dataSourceProperties.getDriverClassName()); |
| dataSource.setUsername(dataSourceProperties.getUsername()); |
| dataSource.setPassword(dataSourceProperties.getPassword()); |
| return dataSource; |
| } |
| |
| |
| * |
| * @return 配置 SqlSessionFactory 对象 |
| * @throws Exception |
| */ |
| @Bean |
| public SqlSessionFactory sqlSessionFactory() throws Exception { |
| SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); |
| sessionFactory.setDataSource(dataSource()); |
| sessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml")); |
| return sessionFactory.getObject(); |
| } |
| |
| |
| * |
| * @param dataSource 数据源对象 |
| * @return 返回事务管理器 |
| */ |
| @Bean |
| public PlatformTransactionManager transactionManager(DataSource dataSource) { |
| return new DataSourceTransactionManager(dataSource); |
| |
| } |
| |
| @Bean |
| public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { |
| return new PropertySourcesPlaceholderConfigurer(); |
| } |
| } |
# 4. Junit5
# 4.1 依赖导入
| |
| <dependency> |
| <groupId>org.junit.jupiter</groupId> |
| <artifactId>junit-jupiter-api</artifactId> |
| <version>${junit5.version}</version> |
| <scope>test</scope> |
| </dependency> |
| <dependency> |
| <groupId>org.junit.jupiter</groupId> |
| <artifactId>junit-jupiter-engine</artifactId> |
| <version>${junit5.version}</version> |
| <scope>test</scope> |
| </dependency> |
# 4.2 使用示例
报错,结束项目开发。
【参考文章】实例演示如何以全注解的方式搭建 SSM(Spring+SpringMVC+Mybatis)项目_將晨的博客 - CSDN 博客 Spring 中 Resource(资源)的获取_詹姆斯哈登的博客 - CSDN 博客_spring 获取 resource