zoukankan      html  css  js  c++  java
  • 让你远离sql语句的Mybatis工具:Tkmybatis

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

    Tkmybatis使用

    1、pom.xml添加依赖

    有Spring Boot专用依赖,这里选用2.1.5版本

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

    create table TK_TEST(tk_id INTEGER not null, tk_name VARCHAR2(50), tk_date DATE, tk_clob CLOB)

    数据库TK_TEST表结构,定义了几个常见类型的字段。

    实体类中,使用了以下注解:

    @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等,可以了解一下。

    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条件

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

  • 相关阅读:
    牛客练习赛51 D题
    Educational Codeforces Round 72 (Rated for Div. 2) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Educational Codeforces Round 72 (Rated for Div. 2) B题
    Educational Codeforces Round 72 (Rated for Div. 2) A题
    《DSP using MATLAB》Problem 7.2
    《DSP using MATLAB》Problem 7.1
    《DSP using MATLAB》Problem 6.24
  • 原文地址:https://www.cnblogs.com/lijinchang/p/11474220.html
Copyright © 2011-2022 走看看