Tkmybatis快速入手

Tkmybatis

案例码云地址:使用 git clone 即可查看
https://gitee.com/JavaBigDataStudy/tk-mybatis-jpa-demo.git

让你远离sql语句的Mybatis工具==>去除mapper.xml

只需三步即可使用单表常用操作:导包==》继承==》在启动类中设置扫描 ==>使用

通过我们去调用它封装的各种方法来实现sql语句的效果。对于单表查询不需要写SQL语句,这样就不用像mybatis那样每次写一个接口就要写一条sql语句

特点:

Tkmybatis是基于Mybatis框架开发的一个工具,通过调用它提供的方法实现对单表的数据操作,不需要写任何sql语句,这极大地提高了项目开发效率。

Tkmybatis使用 :

项目结构:
在这里插入图片描述

1、pom.xml添加依赖

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper</artifactId>
        <version>3.4.2</version>
    </dependency>
    <!--mapper-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
     </dependency>
</dependencies>

2、创建数据库表对应实体类对象

DROP TABLE IF EXISTS category;
CREATE TABLE category (
  category_id   INT PRIMARY KEY,
  category_name VARCHAR (50) NOT NULL,
  description   VARCHAR (100)
);

DELETE FROM category;
INSERT INTO category (category_id, category_name, description) VALUES
  (1, 'Beverages', 'test'),
  (2, 'Condiments', 'test'),
  (3, 'Oil', 'test');
@Data
@Table(name = "category")
public class Category {
    @Id
    @Column(name = "category_id")
    private Integer categoryID;
    private String categoryName;
    private String description;
}
实体类中,使用了以下注解:

@Table
描述数据库表信息,主要属性有name(表名)、schema、catalog、uniqueConstraints等。

@Id
指定表主键字段,无属性值。

@Column
描述数据库字段信息,主要属性有name(字段名)、columnDefinition、insertable、length、nullable(是否可为空)、precision、scale、table、unique、updatable等。

@ColumnType
描述数据库字段类型,可对一些特殊类型作配置,进行特殊处理,主要属性有jdbcType、column、typeHandler等。

@Transient
标识该属性不进行数据库持久化操作,无属性。

还有其他相关注解,如@ColumnResult、@JoinColumn、@OrderBy、@Embeddable等,可以做简单解一下,用的不多。

@Data
lombok插件用于简化代码的注解,无需写get set 等代码。

3、Mapper数据库操作接口

只需要继承tk.mybatis.mapper.common.Mapper即可。

4、Tkmybatis数据库操作方法API

增
Mapper.insert(record);

保存一个实体,null的属性也会保存,不会使用数据库默认值

Mapper.insertSelective(record);

保存一个实体,null的属性不会保存,会使用数据库默认值

删
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.selectByExampleAndRowBounds(example, rowBounds)

根据example条件和RowBounds进行分页查询

Mapper.selectByPrimaryKey(key)

根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号

Mapper.selectByRowBounds(record, rowBounds)

根据实体属性和RowBounds进行分页查询

Mapper.selectCount(record)

根据实体中的属性查询总数,查询条件使用等号

Mapper.selectCountByExample(example)

根据Example条件进行查询总数

Mapper.selectOne(record)

根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号

Example条件 (自定义查询条件)

 //注意:(addEqualTo)这里的userId是映射的实体类。
    @Test
    public void selectAllTest2() {
        Example example = new Example(Category.class);
        example.createCriteria()
                .andEqualTo("categoryID",1)
                .andEqualTo("categoryName","Beverages");

        List<Category> categories = categoryDao.selectByExample(example);
        System.out.println(categories);
        assertEquals(true, categories.size() > 0);
    }

    //注意:(addCondition)这里的user_id是数据库的字段。即where后面的条件。应该写sql语句。
    @Test
    public void selectAllTest3() {
        Example example = new Example(Category.class);
        example.createCriteria()
                .andCondition("category_id=",1)
                .andCondition("category_name=","Beverages");

        List<Category> categories = categoryDao.selectByExample(example);
        System.out.println(categories);
        assertEquals(true, categories.size() > 0);
    }

Example条件基本涵盖了常用的sql条件,并且支持使用原生sql语句字符串查询。

结语

Tkmybatis极大地提高了我们对单表数据库操作的开发效率,可以在实际项目开发中推荐使用。

发布了234 篇原创文章 · 获赞 12 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/100166637