springboot多模块如何将mybatis项目拆分出去
作为一种快速开发框架,Spring Boot 能够帮助我们构建高效、可靠的项目。很多时候,我们需要按照业务规划将一个项目切分成多个模块,以便于后续的维护、扩展等工作。对于 Spring Boot 项目而言,我们通常会将模块分为业务模块、数据模块等不同类型,其中包括了 mybatis 项目。
Mybatis 是一个能够帮助我们简化与数据库的交互工作的轻量级 ORM 框架,非常适合用于中小型 Web 项目的开发中。因此,在 Spring Boot 项目中,将 mybatis 项目拆分出来,通常会比较有意义。
接下来,就让我们来一起了解一下如何在 Spring Boot 多模块的架构中,将 mybatis 项目拆分出去。
一、创建 Maven 项目
我们首先需要创建一个 Maven 项目,并按照如下的目录结构进行组织:
your-app ├── your-app-common (公共依赖) ├── your-app-dao (DAO 层) ├── your-app-service (Service 层) └── your-app-web (Web 层)
其中,your-app-common 为公共依赖模块,主要用于存放项目中的基础代码、公共配置等内容。your-app-dao 则为 DAO 层模块,主要用于存放 mybatis 相关代码,例如 mybatis 配置、DAO 接口以及对应的映射文件等。
二、新建 mybatis 代码与配置
在 your-app-dao 模块中,我们需要进行如下配置:
1、新建 resources 目录
只需要创建一个 resources 目录即可,目录下将包括如下内容:
resources ├── mapper (mybatis 映射文件) │ └── user.xml └── mybatis-config.xml (mybatis 主配置)
2、新建 mybatis-config.xml 文件
在 mybatis 配置文件中,我们需要填写数据库连接信息、mybatis 插件和别名等信息。具体代码如下:
<?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.eg.eggs.dao.model"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql" />
<property name="reasonable" value="true" />
<property name="supportMethodsArguments" value="false" />
</plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/user.xml"/>
</mappers>
</configuration>
其中,com.eg.eggs.dao.model 为实体类的全限定名,user.xml 是 mybatis 的映射文件名称。
3、新建实体类以及 DAO 接口
在 mybatis 项目中,我们需要新建对应的类和接口。通常情况下,实体类和 DAO 接口的定义和 Spring Boot 中的定义方式一样。以 User 实体类为例:
@Data
public class User {
private Integer id;
private String userName;
private String mobile;
private Integer sex;
}
DAO 接口需要在 com.eg.eggs.dao 包下进行定义,接口名称需要与具体的 DAO 对象名称保持一致。例如,如果要操作 user 数据库中的记录,则需要使用 UserDao 进行操作。关于 DAO 接口的具体定义方式,我们也可以参考 Spring Boot 的定义方式,只需要多加一个 @Mapper 注解即可实现。
@Mapper
@Repository
public interface UserDao {
User getById(int id);
List<User> getAll();
int insert(User user);
int update(User user);
int deleteById(int id);
}
三、对 DAO 层进行测试
在拆分出的 your-app-dao 模块中,我们需要进行测试以验证我们的配置和代码是否可行。测试包含两个部分:数据源的测试和操作数据库的测试。
1、测试数据源
在这里,我们需要新建一个测试类 DataSourceTest,并使用 DataSourceProperties 库导入数据源的配置项。具体代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class DataSourceTest {
private Logger logger = LoggerFactory.getLogger(DataSourceTest.class);
@Autowired
private DataSourceProperties dataSourceProperties;
@Test
public void testDataSource() {
logger.info(dataSourceProperties.getUrl());
}
}
通过运行测试类,我们可以验证当前的数据源配置是否正确。
2、测试 DAO 层
为了测试 DAO 层的代码是否可行,我们需要新建一个 UserDaoTest 测试类,并注入 UserDao 接口。在这个测试类中,我们可以使用 assertNotNull() 方法进行单元测试,例如:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void testGetAll() {
assertNotNull(userDao.getAll());
}
}
四、将公共依赖模块中的 mybatis 依赖移至 DAO 模块
在移动 mybatis 相关代码之前,我们需要将公共依赖模块中的 mybatis 依赖移至 DAO 模块。在 your-app-common 模块中,我们需要更新 pom.xml 文件中的依赖关系,以便于后续进行拆分。具体而言,我们需要在 your-app-common 中,将之前的 mybatis 依赖项从 pom.xml 文件中移除。
五、在测试类中添加 mybatis 依赖
在将 mybatis 项目拆分出去之后,我们需要为测试类添加相关的依赖。具体而言,我们需要在 your-app-dao 模块下的 pom.xml 文件中添加如下依赖项:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
其中,${mysql.version} 为当前项目所使用的 MySQL 驱动版本号。
六、在 Service 层进行集成测试
在将 mybatis 项目拆分出去之后,我们仍然需要对 DAO 层进行测试。不过,我们不再需要单独创建 DAO 层的测试类,而是在 Service 层中加入对 DAO 层的测试。因此,在 Service 层中,我们需要进行集成测试。
1、新建集成测试类
我们需要首先在 your-app-service 模块下新建一个集成测试类 UserServiceImplTest,并在测试类中注入 UserService,以便于进行数据查询等操作。UserServiceImplTest 中的 UserService 代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = YourAppApplication.class)
public class UserServiceImplTest {
@Autowired
private UserService userService;
@Test
public void testGetById() {
assertNotNull(userService.getById(1));
}
}
上述代码中,`YourAppApplication
