模块 | 子模块 | 说明 |
nuts.springboot.boot | 工程启动引导项目 | |
nuts-springboot-listener | actuator admin项目 | |
components | ||
nuts-springboot-dbpool | 数据库连接池组件 | |
nuts-springboot-log4j2 | 使用log4j2作为日志组件 | |
nuts-springboot-logback | springboot默认日志组件 | |
nuts-springboot-mybatis | 使用mybatis作为ORM组件 | |
endpoints | 后端api响应接口 | |
nuts-springboot-clover | 模板项目后端 | |
nuts-springboot-home | 统一异常处理、统一返回结构 | |
kernels | 工程内核模型 | |
nuts-springboot-core | 核心模型 | |
mybatis.gradle文件引入mybatis相关依赖
dependencies {
implementation('org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.2') {
exclude group: 'com.zaxxer', module: 'HikariCP'
}
// 数据库连接池
implementation project(':components:nuts.springboot.dbpool')
}
注意 :没有使用默认HikariCP数据库连接池时,需要排除对应的依赖;否则默认使用HikariCP,并报url没有找到属性值
mybatis使用参照单元测试样例:
UserMapper
@Mapper
@Repository
public interface UserMapper {
/**
* 查询所有用户
*
* @return 用户列表
*/
List selectAllUser();
}
UserRepositoryTest
@SpringBootTest
public class UserRepositoryTest extends AbstractSpringBootTest {
private static final Logger logger = LoggerFactory.getLogger(UserRepositoryTest.class);
@Test
public void testQueryUser() {
UserMapper userMapper = webApplicationContext.getBean(UserMapper.class);
Assert.isInstanceOf(UserMapper.class, userMapper, "UserMapper对象不能为空");
List userPOList = userMapper.selectAllUser();
userPOList.forEach(userPO -> {
logger.info(userPO.toString());
});
Assert.isTrue(userPOList.size() > 0, "数据库记录查询异常");
}
@Test
public void testQueryUserPage() {
UserMapper userMapper = webApplicationContext.getBean(UserMapper.class);
Assert.isInstanceOf(UserMapper.class, userMapper, "UserMapper对象不能为空");
PageHelper.startPage(1, 1);
List userPOList = userMapper.selectAllUser();
PageInfo pageInfo = new PageInfo<>(userPOList, 1);
userPOList.forEach(userPO -> {
logger.info(userPO.toString());
});
Assert.isTrue(userPOList.size() == 1, "数据库记录查询异常");
}
}
UserMapper.xml
注意:Mapper下的接口类与资源文件夹中的xml文件名要求一致,否则提示无法找到对应的xml或定义
这里是对应Mapper接口类的全类名
application-mybatis-dev.yaml
# https://mybatis.org/mybatis-3/zh/configuration.html#settings
mybatis:
props:
name: mybatis-dev
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
cache-enabled: true
mapper-locations: classpath*:mappings/**/**.xml
type-aliases-package: org.hazulnut.admin.mapper
单元测试运行结果:
使用PageHelper作为分页组件
mybatis.gradle文件引入mybatis相关依赖
dependencies {
implementation('org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.2') {
exclude group: 'com.zaxxer', module: 'HikariCP'
}
implementation('com.github.pagehelper:pagehelper-spring-boot-starter:1.2.13') {
exclude group: 'com.zaxxer', module: 'HikariCP'
}
// 数据库连接池
implementation project(':components:nuts.springboot.dbpool')
}
application-mybatis-dev.yaml
# https://mybatis.org/mybatis-3/zh/configuration.html#settings
mybatis:
props:
name: mybatis-dev
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
cache-enabled: true
mapper-locations: classpath*:mappings/**/**.xml
type-aliases-package: org.hazulnut.admin.mapper
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
单元测试运行结果:
至此完成mybatis、pagehelper配置使用。
————————————————
Github项目 https://github.com/HazelNutsWorkGroup/nuts.springboot.single ,
Gitee项目 https://gitee.com/sleeber/nuts.springboot.single
欢迎大家交流