tkMapper 简记

tkMapper 简记,第1张

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 的整合

    • 添加依赖

      <dependency>
          <groupId>tk.mybatisgroupId>
          <artifactId>mapper-spring-boot-starterartifactId>
          <version>2.1.5version>
      dependency>
      
    • 修改启动类的@MapperScan注解的包为 tk.mybatis.spring.annotation.MapperScan

      
      package com.sh;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      /*mapperscan 注意:用tk.mybatis.spring.annotation.MapperScan*/
      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.*;

/**
 * @Author Mr.suho
 * @Date 2022/4/22 10:52
 * @Description CategoryDaoTest
 * @Version 1.0
 */
@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);
        /*
        * id 回填:添加 *** 作时,可以取到添加的这条数的id
        * 条件:在实体标注哪个是 id 字段,@Id
        * */
        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);
        });
        /*for循环遍历数据*/
        List categories = categoryDao.selectAll();
        for (Category c: categories
             ) {
            System.out.println(c);
        }
        /*根据id(主键)查询*/
        Category category = categoryDao.selectByPrimaryKey(57);
        System.out.println("-------"+category);

        /*条件查询
        * 1.创建一个 Example 封装,类别 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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/726007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存