tk-mybatis的使用教程及使用Example进行查询的几种方式

1.引入依赖

复制代码
<!--通用mapper起步依赖-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.0.4</version>
</dependency>

<!--每个工程都有Pojo,都需要用到该包对应的注解-->
<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0</version>
    <scope>compile</scope>
</dependency>
复制代码

2.mapper接口继承基础增删改查Mapper,批量增删改查MySqlMapper

public interface StudentMapper extends Mapper<Student>, MySqlMapper<Student>,ExampleMapper<Student> {
}

3.在启动类上加上@MapperScan("com.study.test.mapper.**"),注意导入tkMybatis的包下的注解。

@SpringBootApplication
@MapperScan(basePackages = {"com.study.test.mapper"})
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

4.crud(增删改查)方法

复制代码
增加
Mapper.insert(record); 保存一个实体,null的属性也会保存,不会使用数据库默认值
Mapper.insertSelective(record); 保存一个实体,忽略空值,即没提交的值会使用使用数据库默认值

删除
Mapper.delete(record); 根据实体属性作为条件进行删除,查询条件使用等号
Mapper.deleteByExample(example) 根据Example条件删除数据
Mapper.deleteByPrimaryKey(key) 根据主键字段进行删除,方法参数必须包含完整的主键属性

修改
Mapper.updateByExample(record,example) 根据Example条件更新实体record包含的全部属性,null值会被更新
Mapper.updateByExampleSelective(record,example) 根据Example条件更新实体record包含的不是null的属性值
Mapper.updateByPrimaryKey(record) 根据主键更新实体全部字段,null值会被更新
Mapper.updateByPrimaryKeySelective(record) 根据主键更新属性不为null的值

查询
Mapper.select(record) 根据实体中的属性值进行查询,查询条件使用等号
Mapper.selectAll() 查询全部结果
Mapper.selectByExample(example) 根据Example条件进行查询
Mapper.selectByPrimaryKey(key) 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
Mapper.selectCount(record) 根据实体中的属性查询总数,查询条件使用等号
Mapper.selectCountByExample(example) 根据Example条件进行查询总数
Mapper.selectOne(record) 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号。
但是如果存在某个属性为int,则会初始化为0。可能影响到实际使用
复制代码

5.条件查询,在使用Example进行查询的几种方式

其中weekend方式需要升级jdk到1.8及以上。

首先定义数据库表的实体类:

复制代码
public class Student {
    private Long id;
    private Long count;
    private String name;

    public Long getId() {
        return id;
    }
    public Long getCount() {
        return count;
    }
    public String getName() {
        return name;
    }

//    setter……
}
复制代码

此处省略了数据库表映射和set方法。

接下来就是实现example查询的几种方式,核心代码如下:

方式一:普通Example方式(从and方法开始可以实现动态sql拼接)

复制代码
    Example example = new Example(Student.class);
    example
      //.selectProperties("name","cabName")
        .and().andEqualTo("count",0)
        .andLike("name","%d%");

    // 排序
    example.orderBy("CreatedTime")
        /*.desc()*/
          .orderBy("Id").desc();

    // 获得结果
    List<Student> brands = brandEntityMapper.selectByExample(example);
复制代码

 

方式二:Criteria方式(可使用criteria完成动态sql拼接)

Example example = new Example(Student.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("count", 0)
        .andLike("name", "%d%");
example.orderBy("count")
        //.desc()
        .orderBy("name").desc();
List<Student> demos = mybatisDemoMapper.selectByExample(example);

 

方式三:Example.builder 方式(其中where从句中内容可以拿出来进行动态sql拼接)

Example example = Example.builder(Student.class)
        .select("Id","Name")
        .where(Sqls.custom().andEqualTo("count", 0)
        .andLike("name", "%d%"))
        .orderByDesc("count","name")
        .build();
List<Student> demos = mybatisDemoMapper.selectByExample(example);

 

方式四:Example.builder + Weekend方式,优势:不用输入属性名,避免数据库有变动或输入错误就会出错

//获得seekendsql
WeekendSqls<Student> sqls = WeekendSqls.<Student>custom();

//可进行动态sql拼接
sqls = sqls.andEqualTo(Student::getCount,0).andLike(Student::getName,"%d%");

//获得结果
List<Student> demos = mybatisDemoMapper.selectByExample(Example.builder(Student.class).where(sqls).orderByDesc("count","name").build());

 

参考内容:

https://github.com/abel533/Mapper/wiki/6.example

https://cloud.tencent.com/developer/article/2063029

https://blog.csdn.net/weixin_59816940/article/details/127747890

posted @   思凡念真  阅读(242)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示