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

MyBatis-Plus的CRUD测试

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

MyBatis-Plus的CRUD测试

文章目录
      • Mybatis-Plus(CRUD)
        • 1.导入依赖
        • 2.创建数据库student
        • 3.编写yml配置
        • 4.编写实体类
        • 5.编写StudentMapper和mapper.xml
        • 6.编写StudentService
        • 7.编写StudentService实现类
        • 8.记得在启动类上添加包扫描
        • 9.测试
        • 10.测试结果

Mybatis-Plus(CRUD) 1.导入依赖


org.springframework.boot
spring-boot-starter-web



	mysql
	mysql-connector-java
	runtime



	org.projectlombok
	lombok
	true



	junit
	junit
	test



	com.baomidou
	mybatis-plus-boot-starter
	3.5.1



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

2.创建数据库student
create table student
(
    sid      int(5) auto_increment
        primary key,
    sname    varchar(20) null,
    sex      varchar(2)  null,
    age      int(3)      null,
    birthday date        null,
    hobby    varchar(10) null,
    province varchar(20) null
);
-- 插入几条数据
insert into `student` (`sid`, `sname`, `sex`, `age`, `birthday`, `hobby`, `province`) values('1','孙小鸟','男','19','2020-09-17','平时喜欢看影','湖南省');
insert into `student` (`sid`, `sname`, `sex`, `age`, `birthday`, `hobby`, `province`) values('2','黄小明','女','12','2019-09-17','平时喜欢打游戏','湖北省');
insert into `student` (`sid`, `sname`, `sex`, `age`, `birthday`, `hobby`, `province`) values('3','大黄','男','15','2022-05-19','甚','湖北省');
insert into `student` (`sid`, `sname`, `sex`, `age`, `birthday`, `hobby`, `province`) values('4','孙权','男','20','2022-05-12','打仗','四川省');
insert into `student` (`sid`, `sname`, `sex`, `age`, `birthday`, `hobby`, `province`) values('5','孙尚香','女','16','2022-05-12','cosplay','四川省');
insert into `student` (`sid`, `sname`, `sex`, `age`, `birthday`, `hobby`, `province`) values('6','曹老板','女','50','2022-05-19','那啥啥','湖北省');
3.编写yml配置
# 数据源配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/javaweb_db?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimeZone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
# 端口
server:
  port: 9000

# 开启mybatis日志
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.编写实体类
@TableName(value ="student")
@Data
public class Student implements Serializable {
    @TableId(type = IdType.AUTO)
    private Integer sid;//id

    private String sname;//姓名

    private String sex;//性别

    private Integer age;//年龄
 
    private Date birthday;//生日

    private String hobby;//爱好

    private String province;//address

    //省略getter/setter

}
5.编写StudentMapper和mapper.xml
// 继承BaseMapper
//BaseMapper可以使用mp封装好的一些方法
//有兴趣的可以自己研究下,下载源码会有注释
public interface StudentMapper extends BaseMapper {

}




	//配置结果集映射
    
            
            
            
            
            
            
            
    
	//sql片段
    
        sid,sname,sex,
        age,birthday,hobby,
        province
    

编写6、7只是为了测试时对比,BaseMapper没有的方法可以使用IService中的方法

6.编写StudentService
//继承IService 同上BaseMapper
public interface StudentService extends IService {

}
7.编写StudentService实现类
@Service
public class StudentServiceImpl extends ServiceImpl
    implements StudentService{
}
8.记得在启动类上添加包扫描
@MapperScan("com/lqz/mapper")
9.测试
@SpringBootTest
class Springboot11MybatisPlusApplicationTests {

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StudentService service;

    
    @Test
    void testSelectList() {
        List students = studentMapper.selectList(null);
        for (Student student : students) {
            System.out.println(student);
        }
    }

    
    @Test
    void testQueryWrapper() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sname","孙");
        List students = studentMapper.selectList(queryWrapper);
        students.forEach(System.out::println);
    }

    
    @Test
    void testQueryWrapper1() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sname","孙").like("sex","男");
        List students = studentMapper.selectList(queryWrapper);
        students.forEach(System.out::println);
    }

    
    @Test
    void testQueryWrapper2() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sname","sex","birthday");
        List students = studentMapper.selectList(queryWrapper);
        students.forEach(System.out::println);
    }

    
    @Test
    void testCount() {
        long count = service.count();
        System.out.println(count);
    }

    
    @Test
    void testCountByCondition() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sname","孙");
        long count = service.count(queryWrapper);
        System.out.println(count);
        System.out.println("-----------------");

        queryWrapper.notLike("sname","孙");
        long count1 = service.count(queryWrapper);
        System.out.println("count1 = " + count1);
    }

    
    @Test
    void testCountByCondition2() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.notLike("sname","孙");
        long count1 = service.count(queryWrapper);
        System.out.println("count1 = " + count1);
    }

    
    @Test
    void testSave() {
        Student student = new Student();
        student.setSid(6);
        student.setSname("曹操");
        student.setAge(50);
        student.setBirthday(new Date());
        student.setHobby("那啥啥");
        student.setProvince("湖北省");
        int insert = studentMapper.insert(student);
        Assert.assertEquals(insert,1);
    }

    
    @Test
    void testBatchAdd(){
        Collection collection = new ArrayList<>();
        //批量添加
        for (int i=7;i<15;i++) {
            Student student = new Student();
            student.setSid(i);
            student.setSname("皮卡丘" + i);
            student.setAge(1 + i);
            student.setBirthday(new Date());
            student.setHobby("皮卡啾");
            student.setProvince("湖北省");
            //加入集合中
            collection.add(student);
        }
        boolean b = service.saveBatch(collection);
        Assert.assertEquals(b,true);
    }

    
    @Test
    void testDel(){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sname","皮卡");
        int delete = studentMapper.delete(queryWrapper);
        Assert.assertEquals(delete,8);
    }

    
    @Test
    void testUpdate() {
        Student student = new Student();
        student.setSid(6);
        student.setSname("曹老板");
        student.setSex("男");
        int i = studentMapper.updateById(student);
        Assert.assertEquals(i,1);
    }

    
    @Test
    void testUpdateService() {
        Student student = new Student();
        student.setSid(6);
        student.setSname("曹老板");
        student.setSex("女");
        boolean b = service.updateById(student);
        Assert.assertEquals(b,true);
    }

    
    @Test
    void testBatchUpdate() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sname","皮卡");
        Student student = new Student();
        student.setSname("张三");
        boolean update = service.update(student,queryWrapper);
        Assert.assertEquals(update,true);
    }

}
10.测试结果

下次更新下mybatis-plus的分页

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

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

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