1、配置环境

(1)引入 pom 依赖

<dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.0.2</version>
</dependency>

这些基本都可以通过代码生成器生成,主要介绍一些注意点:

2、实体类

实体类:

  • 默认表名 = 类名 字段名 = 属性名
  • 使用@Table(name = "表名")进行表名指定
  • 使用@Column(name = "字段名")进行字段指定
  • 使用@Transient不进行字段映射
@Table(name = "tb_user")//表名与类不一致时
public class User1 implements Serializable {private static final long serialVersionUID = 1L;@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;//自动转换下换线到驼峰命名user_name -> userName//@Column(name = "user_name")private String userName;private Integer age;@Transient//不进行字段映射private String info;
}

3、mapper 接口

dao:继承Mapper接口,不要忘记指定 实体对象

继承 Mapper 后,就继承了 Mapper 的通用 crud 方法

public interface UserMapper extends Mapper<User> {public List<User> findByUser(User user);
}

4、映射文件

有时候面对复杂的场景,需要自己编写 映射文件,tk-mybatis 同样支持

<?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.xj0927.dao.UserMapper"> <select id="findByUser" resultType="com.xj0927.bean.User1">SELECT * FROM tb_user<where><if test="name != null">name like '%${name}%'</if><if test="note != null">or note like '%${note}%'</if></where></select>
</mapper>

5、启动类

此时 @SpringBootApplication 使用 tx mybatis 的构件:tk.mybatis.spring.annotation.MapperScan

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;@SpringBootApplication
@MapperScan("com.xj0927.dao") //扫描接口
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}
}

6、测试

为了更好看见所执行的 sql 语句,可以在配置文件添加如下配置:

(1)添加
@Test
public void insert() {Brand brand = new Brand();brand.setName("测试1");brandMapper.insert(brand);brandMapper.insertSelective(brand);
}

insert 和 insertSelective 区别:

前提Goods商品表里面有三个字段:id,name,price1.此时我只设置了一个字段名字: Goods g = new Goods(); g.setName("手机");insertSelective(g);insertSelective执行对应的sql语句的时候,只插入对应的name字段;(主键是自动添加的,默认插入为空)insert into tb_goods (id,name) value (null,"手机");注意:此时是没有price什么事的2、如果使用insert则是不论你设置多少个字段,统一都要添加一遍,不论你设置几个字段,即使是一个。Goods g=new Goods();g.setName("冰箱");insert(g)insert执行对应的sql语句的时候,统一都要添加一遍;insert into tb_goods (id,name,price) value (null,"冰箱",null);注意:price也在哦!!

insert 和 insertSelective 插入数据库后,在数据库中的效果是一样的,只是 sql 语句不同

insert 就是把所有值插入,此时数据库中有 default 值,default 值就不起作用了

insertSelective 不会忽略 default 值


(2)删除
@Test
public void insert() {//1.根据主键值删除元素brandMapper.deleteByPrimaryKey(23);//2.传入实体对象删除Brand brand = new Brand();brand.setId(23L);brandMapper.delete(brand);//3.创建 Example 对象,添加限制条件,再传入对象进行删除Example example = new Example(Brand.class);Example.Criteria criteria = example.createCriteria();criteria.andEqualTo("id","23");brandMapper.deleteByExample(example);
}

(3)修改
@Test
public void insert() {Brand brand = new Brand();brand.setId(22l);brand.setName("测试1");Example example = new Example(Brand.class);Example.Criteria criteria = example.createCriteria();criteria.andEqualTo("id","22");//2.传入实体类+添加example查询条件brandMapper.updateByExample(brand, example);brandMapper.updateByExampleSelective(brand,example);//1.根据传入实体类的id进行修改brandMapper.updateByPrimaryKey(brand);brandMapper.updateByPrimaryKeySelective(brand);
}

updateByPrimaryKey 和 updateByPrimaryKeySelective 区别:

​ updateByPrimaryKey 更新的时候不会对字段进行 null 判断.如果为 null,这个字段就被更新为 null

​ updateByPrimaryKeySelective 更新的时候会对字段进行 null 判断,如果为 null,就不更新这个字段

updateByExampleSelective传入参数的含义:

xxxMapper.updateByExampleSelective(参数一,参数二);第一个参数 是要修改的部分值组成的对象,其中有些属性为null则表示该项不修改。第二个参数 是一个对应的查询条件的类, 通过这个类可以实现 order by 和一部分的where 条件。

(4)查询
@Test
public void insert() {Brand brand = new Brand();brand.setId(22l);brand.setName("测试2");//1.根据主键进行查询Brand brand1 = brandMapper.selectByPrimaryKey(1);//2.根据实体对象查询符合条件的:结果为ListList<Brand> select = brandMapper.select(brand);//3.查询单个数据:结果为单个对象Brand brand2 = brandMapper.selectOne(brand);//4.查询所有List<Brand> brands = brandMapper.selectAll();//5.根据(属性条件)查询符合条件的总条数int i = brandMapper.selectCount(brand);//6.根据Example对象添加限制条件查询Example example = new Example(Brand.class);Example.Criteria criteria = example.createCriteria();criteria.andEqualTo("id","22");List<Brand> brands1 = brandMapper.selectByExample(example);
}

1_整合 tk mybatis相关推荐

  1. tk.mybatis.spring.annotation.MapperScan 无法引入

    问题现象: SpringBoot整合 tk.mybatis的时候 使用 显示红色无法引入: 原因: 原因是 我的mybatis 的版本是3.4.6 boot版本是2 版本不匹配 解决办法: pom文件 ...

  2. SpringBoot 2.x 整合Mybatis三:tk.mybatis

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80734057 本文出自[赵彦军的博客] 简介 地址:https://github. ...

  3. 搭建eclipse版的ssm+maven+tk.mybatis+redis及mybatis+spring多数据源配置集成的demo

    前言:我这里搭建好eclipse版的ssm+maven+tk.mybatis+redis及mybatis+spring多数据源配置集成的demo.新手快速上手直接看demo. 最后处提供完整高质量de ...

  4. java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider.<init>()的问题解决

    在使用通用mapper进行crud的时候运行报java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectPr ...

  5. mall整合SpringBoot+MyBatis搭建基本骨架

    本文主要讲解mall整合SpringBoot+MyBatis搭建基本骨架,以商品品牌为例实现基本的CRUD操作及通过PageHelper实现分页查询. mysql数据库环境搭建 下载并安装mysql5 ...

  6. java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.SpecialProvider 使用MySqlMapper的问题

    这是关于SpringBoot使用MySqlMapper中的一个错误 1.记得导入的是tk开头的包 import tk.mybatis.spring.annotation.MapperScan; 2.这 ...

  7. 【SpringBoot】tk.mybatis集成,帮你更加傻瓜式的写代码~

    大家日常mybatis开发的时候,有没有发现好多操作基本都差不多,比如通过id进行select.update.delete等等操作,虽然差不多,但是xml文件和mapper接口中也得写这一堆一模一样的 ...

  8. There is no getter for property named ‘distinct‘ in ‘class tk.mybatis.mapper

    今天调试接口时候发现程序报错 There is no getter for property named 'distinct' in 'class tk.mybatis.mapper.entity.E ...

  9. SSM项目使用Mybatis通用mapper插件tk.mybatis的用法

    SSM项目使用Mybatis通用mapper插件tk.mybatis的用法 https://blog.csdn.net/qq_40060806/article/details/82949722 TKm ...

最新文章

  1. 在 Linux中find命令使用技巧
  2. 【Flutter】Flutter 拍照示例 ( 创建应用 | 安装 image_picker 插件 )
  3. 使用dshow抓取摄像头数据时,回调函数时间为0的问题
  4. 向mvc controller传递json数组
  5. UE4 iOS游戏开发
  6. Hihocoder 1142 三分
  7. HDOJ 1106 排序
  8. linux的系统监视器图片_Linux中一个高效的资源监控器Bpytop
  9. Java异常处理-----finally
  10. jsp input输入实时校验长度并提示_拆解「输入框」,理解输入反馈的规则逻辑
  11. LaTeX 消除字Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
  12. OP-TEE内核学习笔记(一)(安全存储)—— 安全文件基础操作(创建、读、写)
  13. windows利用diskpart格式化磁盘
  14. Collectors.reducing总结
  15. 美团动态线程池实践思路开源项目(DynamicTp),线程池源码解析及通知告警篇
  16. 设计一可控同步四进制可逆计数器, 其由输入X1, X2控制, 用D触发器和74151及必要的门电路实现
  17. 安卓笔记之ViewPager页卡
  18. NUIST OJ 1364 [2017 江苏科技大学 程序设计竞赛]D.重复成绩统计(改编) 【STL-map】
  19. Pytorch之经典神经网络CNN(七) —— GoogLeNet(InceptionV1)(Bottleneck)(全局平均池化GAP)(1*1卷积)(多尺度)(flower花卉数据集)
  20. iOS百度人脸识别打包上传AppStore报错

热门文章

  1. 用decimal.js库解决JavaScript中计算精度丢失的问题
  2. Could not transfer metadata ../maven-metadata.xml from/to nexus ..: Not authorized
  3. 使用开源免费软件audacity录制windows 10系统声音
  4. (三) 立创EDA原理图库的创建
  5. Lucene之中文庖丁解牛(mmseg)分词器-yellowcong
  6. 文献分享:利用深度神经网络和单导联心电信号Detection of sleep apnea using deep neural networks and single-lead ECG signals
  7. 大漠插件调用系统环境配置
  8. N沟道和P沟道MOS FET开关电路
  9. 全国青少年机器人等级考试三级推荐教材
  10. “单点登录CAS集成应用系统”二次开发笔记