SpringBoot单元测试Mybatis的DAO层

原文链接: https://blog.csdn.net/qq_19636353/article/details/44677953

添加spring-boot-starter-test依赖

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

创建测试类

@RunWith(SpringRunner.class) 指定当前运行环境
@SpringBootTest 指定当前类是一个测试类,classes属性指定启动类
@Test 指定当前方法是一个单元测试方法

AppTest.java

/**
 * http://localhost:8080/queryDepts?name=%E5%B8%82%E5%9C%BA%E9%83%A8&¤tPage=0&&pageSize=10
 */
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes= {App.class})
public class AppTest {
	@Autowired
	private DeptDao deptDao;
	
	@Test
	public void test0() {
		assert deptDao != null;
		System.out.println(deptDao.queryDepts("市场部", 0, 10)); //分页查询
	}

    @Test
    public void test1() {
    	Dept dept = new Dept();
    	for(int i=1033;i<=1042;i++)
    	{
    		dept.setId(""+i);
    		dept.setName("dept"+i);
    		dept.setLevel(1);
    		deptDao.addDept(dept);
    	}
    }
    

Mybatis持久层

引入mybatis.cfg.xml 文件
application.properties


#当前数据源类型 datasource-druid 
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#数据库驱动程序、连接地址、用户名、密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

# 连接池设置:初始连接数、最小、最大连接数、最大超时时间
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=6000

# druid监听过滤配置
spring.datasource.druid.filters=stat,wall,log4j

# 配置 Mybatis
mybatis.configuration-location=classpath:mybatis.cfg.xml
#定义所有操作类的别名所在包
mybatis.type-aliases-package=com.example
#所有mapper映射文件
mybatis.mapperLocations=classpath:mapper/*.xml

DeptDao.java

package com.example.demo;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface DeptDao {
	public List queryDepts(@Param("name") String name, @Param("currentPage") int currPage, @Param("pageSize") int pageSize);
	
	public boolean addDept(Dept dept);
	
	public Dept queryDeptById(String id);
	
	public boolean update(Dept dept);
}

DeptDao.xml

路径 src\main\resource\mapper\DeptDao.xml




	
	
	
	
	
		insert into dept(id, name, level) values(#{id}, #{name}, #{level});
	
	
	
		update dept set name=#{name}, level=#{level} where id=#{id}
	
	


SpringBoot单元测试Mybatis的DAO层_第1张图片

资料

SpringBoot整合Mybatis+PageHelper分页实现增删查改
https://blog.csdn.net/weixin_36279318/article/details/82776632

SpringBoot整合Mybatis实现自动转换枚举类型
https://blog.csdn.net/qq_26440803/article/details/83451221

你可能感兴趣的:(springboot)