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

tkMapper 简记

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

tkMapper 简记

tkMapper 实现基本CRUD操作以及分页查询和联表查询方法
      • 目录
        • 1. tkMapper
          • 2. tkMapper 简介
          • 3. tkMapper 整合
          • 4. tkMapper 使用
          • 5. tkMapper 提供的方法

目录 1. tkMapper

基于 Mybatis 提供了很多第三方插件,通常可以完成通过数据方法的封装,数据库逆向工程的工作(根据数据表生成实体类、生成映射文件)

  • Mybatis-plus
  • tkMapper
2. tkMapper 简介

tkMapper 是一个 Mybatis 插件,是在 Mybatis 的基础上提供了很多工具,让开发变得简单,提高开发效率

  • 提供了针对单表的通用数据库操作
  • 提供了逆向工程(根据数据表生成实体类、dao 接口、映射文件)
3. tkMapper 整合
  • 基于 SpringBoot 完成 Mybatis 的整合

    • 添加依赖

      
          tk.mybatis
          mapper-spring-boot-starter
          2.1.5
      
      
    • 修改启动类的@MapperScan注解的包为 tk.mybatis.spring.annotation.MapperScan

      package com.sh;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      import tk.mybatis.spring.annotation.MapperScan;
      
      @MapperScan("com.sh.dao")
      @SpringBootApplication
      public class TkMapperDemoApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(TkMapperDemoApplication.class, args);
          }
      
      }
      
4. tkMapper 使用
  1. 创建数据表

  2. 创建实体类

  3. 创建 Dao 接口

    • tkMapper 已经完成了对单表的通用操作的封装封装在 Myapper 接口和 MysqlMapper 接口;因此我们要完成单表的操作,只需自定义 Dao ji恶口继承 Mapper 接口和 MysqlMapper 接口

      public interface UserDao extends Mapper, MySqlMapper {
      }
      
  4. 测试

    @Test
        public void test(){
            Users users = new Users();
            users.setUsername("saaa");
            users.setPassword("111");
            users.setUserImg("img/");
            users.setUserRegtime(new Date());
            users.setUserModtime(new Date());
            int i = userDao.insert(users);
            System.out.println(i);
           
        }
    
5. tkMapper 提供的方法

单表

package com.sh.dao;

import com.sh.TkMapperDemoApplication;
import com.sh.beans.Category;
import org.apache.ibatis.session.RowBounds;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import static org.junit.Assert.*;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkMapperDemoApplication.class)
public class CategoryDaoTest {
    @Autowired
    CategoryDao categoryDao;
    
    @Test
    public void testInsert(){
        Category category = new Category(0,"测试3",1,0,"01.png","hehe","aaa.png","black");
        //int i = categoryDao.insert(category);
        
        int a = categoryDao.insertUseGeneratedKeys(category);
        System.out.println("当前添加数据的id:"+category.getCategoryId());
        assertEquals(1,a);//断言:判断返回值和期望值是否相等
    }
    
    @Test
    public void testUpdate(){
        Category category = new Category(57,"测试5",1,0,"0dadsdada1.png","hehe","aaa.png","black");
        int i = categoryDao.updateByPrimaryKey(category);
        assertEquals(1,i);
    }
    
    @Test
    public void testDelete(){
        int i = categoryDao.deleteByPrimaryKey(56);
        assertEquals(1,i);
    }
    
    @Test
    public void testSelect1(){
        
        //java8 写法
        categoryDao.selectAll().stream().forEach(c ->{
            System.out.println(c);
        });
        
        List categories = categoryDao.selectAll();
        for (Category c: categories
             ) {
            System.out.println(c);
        }
        
        Category category = categoryDao.selectByPrimaryKey(57);
        System.out.println("-------"+category);

        
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        //查询等级等于 1 和(并且)分类名称中包含“干“的数据
        //注意:和,并且 and,或者 or
        criteria.andEqualTo("categoryLevel",1);
        criteria.andLike("categoryName","%干%");
        List categories1 = categoryDao.selectByExample(example);
        for (Category c: categories1
             ) {
            System.out.println(c);
        }

        
        int pageNum = 2; //条数
        int pageSize = 10; //页数
        int start = (pageNum - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(start,pageSize);
        List categories2 = categoryDao.selectByRowBounds(new Category(), rowBounds);
        for (Category cs: categories2
             ) {
            System.out.println(cs);
        }
        
        int i = categoryDao.selectCount(new Category());
        System.out.println("-----总数:"+i);

        
        //条件
        Example example1 = new Example(Category.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria.andEqualTo("categoryLevel",1); //条件:为等级等于 1
        //分页
        int pageNum1 = 1; //条数
        int pageSize1 = 3; //页数
        int start1 = (pageNum1 - 1) * pageSize1;
        RowBounds rowBounds1 = new RowBounds(start1,pageSize1);
        List categories3 = categoryDao.selectByExampleAndRowBounds(example1, rowBounds1);
        for (Category a: categories3
        ) {
            System.out.println(a);
        }
        
        int s = categoryDao.selectCountByExample(example1);
        System.out.println("-----总数:"+s);
    }
}

联表

  • 所有关联查询都可以通过多个单表实现操作

       //查询用户同时查询订单
            Example example = new Example(Users.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("username","zhansan");
            //1.根据用户名查询用户信息
            List users = userDao.selectByExample(example);
            Users users1 = users.get(0);
            //2.根据用户 id 到订单表查询订单信息
            Example example1 = new Example(Orders.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("userId",users1.getUserId());
            List orders = ordersDao.selectByExample(example1);
            //3.将查询到的订单集合设置到 Users 中
            users1.setOrderList(orders);
            System.out.println(users1);
    

自定义连接查询:

在使用 tkMapper.Dao 继承 Mapper 和 MysqlMapper 之后,还可以自定义查询

  • 步骤
    • 创建 dao 接口

    • 创建 Mapper.xml 文件 自己写SQL

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

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

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