栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合Mybatis、单元测试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合Mybatis、单元测试

1、添加Mybatis、Jdbc驱动依赖

	org.mybatis.spring.boot
	mybatis-spring-boot-starter
	1.3.2


	mysql
	mysql-connector-java
	runtime
2、修改资源目录resources下的application.yml配置文件,添加数据库等配置
spring:
  datasource:
	#驱动名称,srpingboot2.0之后不再使用com.mysql.jdbc.Driver
    driver-class-name: ${datasource_dirver:com.mysql.cj.jdbc.Driver}
    #数据库链接地址
    url: ${datasource_url:jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull}
    #数据库密码
    password: ${datasource_password:password}
    #数据库登录用户名称
    username: ${datasource_username:root}
    #数据库初始连接数
    initial-size: ${datasource_initSize:5}
    #数据库最小空闲数
    min-idle: ${datasource_minIdel:10}
    #数据库最大连接等待时间
    max-wait: 60000
    #数据库最大连接
    max-active: ${datasource_maxActive:50}
    #验证数据库连接的有效性
    validationgQuery: SELECt 'X'
3、在资源文件夹下创建mapper文件夹,用来存放编写动态SQL的Mapper.xml文件

4、继续改application.yml配置文件,指定资源文件Mapper.xml位置以及日志打印级别
mybatis:
  #mapper.xml文件存放位置
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.taneki.code.entity
#日志打印级别
logging:
  level:
    com:
      taneki:
        code:
          mapper: debug
5、编写DemoMapper接口类,在DemoMapper添加@Repository声明这是一个DAO接口,然后编写一条简单的查询
@Repository
public interface DemoMapper {

    long selectCount(@Param("beginDate")Date beginDate, @Param("endDate") Date endDate);
}

对应的DemoMapper.xml编写SQL语句





	
    
6、SpringBoot启动类开启Mapper包扫描
@SpringBootApplication
@MapperScan("com.taneki.code.mapper")
public class CodeApplication {

    @Resource
    private RestTemplateBuilder builder;

    public static void main(String[] args) {
        SpringApplication.run(CodeApplication.class, args);
    }

    @Bean
    RestTemplate restTemplate() {
        return builder.build();
    }

}
7、使用单元测试进行测试

第一步,引入springtest和junit依赖


	org.springframework.boot
	spring-boot-starter-test
	test


	junit
	junit
	4.12
	test

第二步,在test测试目录下新建用来测试的基类,引入@SpringBootTest和@RunTest注解,后续所有单元测试类都可以继承这个基类,不用重复引入这两个注解

@SpringBootTest(classes = CodeApplication.class)
@RunWith(SpringRunner.class)
public class baseTest {

}

第三步,新建单元测试进行测试

@Slf4j
public class DemoTest extends baseTest{

    @Autowired
    private DemoMapper demoMapper;

    @Test
    public void demoTest(){
        Date beginDate = new Date();
        Date endDate = DateUtils.getDateAfter(beginDate, 1);
        long count = demoMapper.selectCount(beginDate, endDate);
        log.info("统计数量: {}", count);
    }
}
8、因SpringBoot版本导致的报错,将SpringBoot版本号从2.6.1降低为2.4.0,运行正常
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name ‘dataSource’ defined in class path resource 
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfigurationGeneric.clas

	org.springframework.boot
	spring-boot-starter-parent
	2.6.1
	

修改为

	org.springframework.boot
	spring-boot-starter-parent
	2.4.0
	

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/771348.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号