tkmapper教程_tkr和tka的区别

tkmapper教程_tkr和tka的区别tkMapper入门1、tkMapper介绍1.1SpringBoot整合MyBatis以及tkMapper1.2在启动类上修改注解`@MapperScan`2、tkMapper的使用2.1创建数据表2.2创建实体类2.3创建Mapper接口2.4测试3、tkMapper的常用方法3.1类别实体类3.2类别接口增加修改删除查询查询所有根据主键查询条件查询分页查询带条件的分页查询关联查询1、通过多个单表操作实现2、自定义连接查询2.1、在DAO接口自定义方法2.2、创建Mapper文件1、

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

1、tkMapper介绍

tkMapper是MyBatis的插件,提供了对单表通用的操作数据库的方法

1.1 SpringBoot整合MyBatis以及tkMapper

 <!-- mybatis starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
 <!--1.tkMapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>

1.2 在启动类上修改注解@MapperScan

import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
/** @MapperScan 换 tk.mybatis*/
@MapperScan("com.jia.fmmall.dao")
public class ApiApplication { 
   
	public static void main(String[] args) { 
   
		SpringApplication.run(ApiApplication.class, args);
	}
}

2、tkMapper的使用

2.1 创建数据表

CREATE TABLE `users` (
 `user_id` int(64) NOT NULL AUTO_INCREMENT COMMENT '主键id ⽤户id',
 `username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT
NULL COMMENT '⽤户名 ⽤户名',
 `password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT
NULL COMMENT '密码 密码',
 `nickname` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci
NULL DEFAULT NULL COMMENT '昵称 昵称',
 `realname` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci
NULL DEFAULT NULL COMMENT '真实姓名 真实姓名',
 `user_img` varchar(1024) CHARACTER SET utf8 COLLATE utf8_general_ci
NOT NULL COMMENT '头像 头像',
 `user_mobile` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci
NULL DEFAULT NULL COMMENT '⼿机号 ⼿机号',
 `user_email` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci
NULL DEFAULT NULL COMMENT '邮箱地址 邮箱地址',
 `user_sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL
DEFAULT NULL COMMENT '性别 M(男) or F(⼥)',
 `user_birth` date NULL DEFAULT NULL COMMENT '⽣⽇ ⽣⽇',
 `user_regtime` datetime(0) NOT NULL COMMENT '注册时间 创建时间',
 `user_modtime` datetime(0) NOT NULL COMMENT '更新时间 更新时间',
 PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE =
utf8_general_ci COMMENT = '⽤户 ' ROW_FORMAT = Compact;

2.2 创建实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User { 
   
 private int userId;
 private String username;
 private String password;
 private String nickname;
 private String realname;
 private String userImg;
 private String userMobile;
 private String userEmail;
 private String userSex;
 private Date userBirth;
 private Date userRegtime;
 private Date userModtime;
}

2.3 创建Mapper接口

tkmapper 中有对单表通用操作的封装,封装到Mapper接口和MySqlMapper接口;对单表操作,只需要自定义DAO接口继承Mapper接口MySqlMapper接口即可


import com.jia.beans.Users;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface UserDAO  extends Mapper<Users>, MySqlMapper<Users> { 
   
}

2.4 测试


import com.jia.TkmapperDemoApplication;
import com.jia.beans.Users;
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 java.util.Date;
/**生成测试类的快捷键 ctrl+shift +t */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class UsersDAOTest { 
   
    @Autowired
    private UserDAO userDAO;
    @Test
    public void test(){ 
   
        Users user = new Users();
        user.setUsername("aaaa");
        user.setPassword("1111");
        user.setUserImg("img/default.png");
        user.setUserRegtime(new Date());
        user.setUserModtime(new Date());
        int i = userDAO.insert(user);
        System.out.println(i);
    }
}

3、tkMapper的常用方法

首先定义类别的实体类以及接口

3.1 类别实体类

package com.blb.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Id;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Category { 
   

    @Id
    private Integer categoryId;
    private String categoryName;
    private Integer categoryLevel;
    private Integer parentId;
    private String categoryIcon;
    private String categorySlogan;
    private String categoryPic;
    private String categoryBgColor;

}


3.2 类别接口

import com.blb.entity.Category;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface CategoryMapper extends Mapper<Category>, MySqlMapper<Category> { 
   
}

增加

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
	 @Test
	 public void testInsert(){ 
   
	 Category category = new Category(0,"测试类别
	3",1,0,"03.png","xixi","aaa.jpg","black");
	 //int i = categoryDAO.insert(category);
	 int i = categoryDAO.insertUseGeneratedKeys(category);
	 System.out.println(category.getCategoryId());
	 assertEquals(1,i);
	 }
}

主键回填一定要在对应的实体类的主键上添加@Id

public class Category { 
   
    /** * 主键 分类id主键 */
    @Id
    @Column(name = "category_id")
    private Integer categoryId;
}

修改

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
	@Test
	 public void testUpdate(){ 
   
	 Category category = new Category(48,"测试类别
	4",1,0,"04.png","heihei","aaa.jpg","black");
	 int i = categoryDAO.updateByPrimaryKey(category);
	 // 根据⾃定义条件修改,Example example就是封装条件的
	 // int i1 = categoryDAO.updateByExample( Example example);
	 assertEquals(1,i);
	 }
}

删除

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
@Test
 public void testDelete(){ 
   
 int i = categoryDAO.deleteByPrimaryKey(48);
 // 根据条件删除
 //int i1 = categoryDAO.deleteByExample(Example example);
 assertEquals(1,i);
 }
}

查询

查询所有

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
@Test
 public void testSelect1(){ 
   
 //查询所有
 List<Category> categories = categoryDAO.selectAll();
   for (Category category: categories) { 
   
   System.out.println(category);
   }
 }
}

根据主键查询

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
@Test
 public void testSelect2(){ 
   
 //根据主键查询
 Category category = categoryDAO.selectByPrimaryKey(47);
 System.out.println(category);
 }
}

条件查询

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
@Test
 public void testSelect3(){ 
   
 //条件查询
 //1.创建⼀个Example封装 类别Category查询条件
 Example example = new Example(Category.class);
 Example.Criteria criteria = example.createCriteria();
 criteria.andEqualTo("categoryLevel",1);
 criteria.orLike("categoryName","%⼲%");
 List<Category> categories =
categoryDAO.selectByExample(example);
  for (Category category: categories) { 
   
  System.out.println(category);
  }
 }
}

分页查询

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
@Test
 public void testSelect4(){ 
   
 //分⻚查询
 int pageNum = 2;
 int pageSize = 10;
 int start = (pageNum-1)*pageSize;
 RowBounds rowBounds = new RowBounds(start,pageSize);
 List<Category> categories = categoryDAO.selectByRowBounds(new
 Category(), rowBounds);
 for (Category category: categories) { 
   
 System.out.println(category);
 }
 //查询总记录数
 int i = categoryDAO.selectCount(new Category());
 System.out.println(i);
 }
}

带条件的分页查询

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
@Test
 public void testSelect5(){ 
   
 //带条件分⻚
 //条件
 Example example = new Example(Category.class);
 Example.Criteria criteria = example.createCriteria();
 criteria.andEqualTo("categoryLevel",1);
 //分⻚
 int pageNum = 2;
 int pageSize = 3;
 int start = (pageNum-1)*pageSize;
 RowBounds rowBounds = new RowBounds(start,pageSize);
 List<Category> categories =
categoryDAO.selectByExampleAndRowBounds(example,rowBounds);
 for (Category category: categories) { 
   
 System.out.println(category);
 }
 //查询总记录数(满⾜条件)
 int i = categoryDAO.selectCountByExample(example);
 System.out.println(i);
  }
 }
}

关联查询

1、通过多个单表操作实现

例子:查询zhangsan的订单

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TkmapperDemoApplication.class)
public class CategoryDAOTest { 
   
 @Autowired
 private CategoryDAO categoryDAO;
@Test
 public void testSelect6(){ 
   
	//查询⽤户同时查询订单
	Example example = new Example(User.class);
	Example.Criteria criteria = example.createCriteria();
	criteria.andEqualTo("username","zhangsan");
	//根据⽤户名查询⽤户
	//1.先根据⽤户名查询⽤户信息
	List<User> users = userDAO.selectByExample(example);
	User user = users.get(0);
	//2.再根据⽤户id到订单表查询订单
	Example example1 = new Example(Orders.class);
	Example.Criteria criteria1 = example1.createCriteria();
	criteria1.andEqualTo("userId",user.getUserId());
	List<Orders> ordersList = orderDAO.selectByExample(example1);
	//3.将查询到订单集合设置到user
	user.setOrdersList(ordersList);
	System.out.println(user);
  }
}

2、自定义连接查询

2.1、在DAO接口自定义方法
public interface UserDAO extends GeneralDAO<User> { 
   
   public User selectByUsername(String username);
}
2.2、创建Mapper 文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jia.fmmall.dao.UserDAO">
 <insert id="insertUser">
 insert into
users(username,password,user_img,user_regtime,user_modtime)
 values(#{username},#{password},#{userImg},#{userRegtime},#
{userModtime})
 </insert>
 <resultMap id="userMap" type="User">
 <id column="user_id" property="userId"/>
 <result column="username" property="username"/>
 <result column="password" property="password"/>
 <result column="nickname" property="nickname"/>
 <result column="realname" property="realname"/>
 <result column="user_img" property="userImg"/>
 <result column="user_mobile" property="userMobile"/>
 <result column="user_email" property="userEmail"/>
 <result column="user_sex" property="userSex"/>
 <result column="user_birth" property="userBirth"/>
 <result column="user_regtime" property="userRegtime"/>
 <result column="user_modtime" property="userModtime"/>
 </resultMap>
 <select id="queryUserByName" resultMap="userMap">
 select
 user_id,
 username,
 password,
 nickname,
 realname,
 user_img,
 user_mobile,
 user_email,
 user_sex,
 user_birth,
 user_regtime,
 user_modtime
 from users
 where username=#{name}
 </select>
</mapper>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/184885.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 手把手教你安装黑苹果之openCore-0.6.3 EFI制作全过程,非常详细

    手把手教你安装黑苹果之openCore-0.6.3 EFI制作全过程,非常详细文章目录前言一、EFI是什么?二、获取EFI1.Hackintosh黑苹果长期维护机型整理清单2.搜索efi二、自制EFI总结前言这篇文章主要是记录自己动手安装BigSur在过程,和心理。略显繁琐,请自行跳跃观看。macOSBigSur正式版于11月12日星期四(北京时间11月13日)发布,改动还是挺大的。基于M1苹果自研芯片的macbook也发布了,看了各种up主的评测,网上一堆好评。我馋了好久了,奈何囊中羞涩。等我有钱了,一定要买macbook。看了一眼跟了我6年之前的

  • 个人博客数据库设计

    个人博客数据库设计文章目录前言一、概述及分析1.1项目背景1.2分析1.3系统功能1.3.1用户管理1.3.2博文管理1.3.3评论管理1.3.4分类管理1.3.5标签管理二、数据库概念模型设计—基本ER图三、数据库逻辑模型设计四、数据库语句4.1建立数据表语句(部分)4.2建立视图(部分)五、小结&amp;参考资料小结参考资料前言最近要做数据库大作业,在思考了很久之后,还是设计一个简单…

  • linux中sftp默认登录的端口号是多少? sftp通过指定的端口号连接?sftp默认端口号[通俗易懂]

    linux中sftp默认登录的端口号是多少? sftp通过指定的端口号连接?sftp默认端口号[通俗易懂]需求描述:  今天一个同事,遇到个问题,程序连接sftp服务器连接不上,问我端口号是多少,  我想了一下是21还是22,所以就做了测试,发现sftp默认的连接端口号是22,  在此做下记录.操作过程:1.使用sftp默认的端口号进行登录aiuap_cj@cuchc802:~>sftpaisftp@10.124.163.133aisftp@10.124.163.13…

  • flvjs api 中文[通俗易懂]

    flvjs api 中文[通俗易懂]flvjs.createPlayer()functioncreatePlayer(mediaDataSource:MediaDataSource,config?:Config):Player;MediaDataSource Field Type Description type string Indicatesmediatype,’flv’or’mp4′ isLive? boolean Indic…

    2022年10月22日
  • P2P技术介绍

    P2P技术介绍BT技术其实是P2P技术的一种应用,所以先让我们来看一下什么是P2P技术:   P2P即PeertoPeer,称为对等连接或对等网络,P2P技术主要指由硬件形成连接后的信息控制技术,其代表形式是软件。  技术背景    P2P起源于最初的联网通信方式,如在建筑物内PC通过局域网互联,不同建筑物间通过Modem远程拨号互联。其中建立在TCP/IP协议之上的通信模式构成了今日互联网的

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号