TkMybatis用法总结

简介: TkMybatis用法总结

自定义SQL写法

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@Autowired
protected NamedParameterJdbcTemplate namedJdbcTemplate;
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource()
                .addValue("failedAttempts", user.getFailedAttempts())
                .addValue("lockedAt", user.getLockedAt())
                .addValue("id", user.getId());
namedJdbcTemplate.update("update user u set u.failed_attempts = :failedAttempts,  u.locked_at =:lockedAt where u.id = :id", mapSqlParameterSource);

 

 

多条件组合查询写法

Example queryExample = new Example(UserActiveCode.class);
Example.Criteria criteria = queryExample.createCriteria();
criteria.andEqualTo("activeId", code);
criteria.andEqualTo("userId", userId);
UserActiveCode userActiveCode = userActiveCodeDao.selectOneByExample(queryExample);
注意:queryExample也可以用MapSqlParameterSource参数代替

 

 

分页模板

public class PageResult<T> extends BaseResult<T> {
    private Integer pageSize = 20;
    private Integer pageNo = 0;
    private Integer totalPageCount = 0;
    private Integer record = 0;
    private Integer lastPage = 0;
    public PageResult() {
    }
    public PageResult(int pageNo, int pageSize, int record, T data) {
        this.wrapPageResult(pageNo, pageSize, record, data);
    }
    public void setTotalPageCount() {
        int totalP = this.record % this.getPageSize() == 0 ? this.record / this.getPageSize() : this.record / this.getPageSize() + 1;
        this.totalPageCount = totalP;
    }
    public void setRecord(Integer record) {
        this.record = record;
        this.setTotalPageCount();
    }
    public void wrapPageResult(int pageIndex, int pageSize, int record, T data) {
        if (record != 0) {
            this.record = record;
            super.setData(data);
            this.pageNo = this.pageNo;
            this.pageSize = pageSize;
            this.totalPageCount = (record - 1) / pageSize + 1;
        }
    }
    public Integer getPageSize() {
        return this.pageSize;
    }
    public Integer getPageNo() {
        return this.pageNo;
    }
    public Integer getTotalPageCount() {
        return this.totalPageCount;
    }
    public Integer getRecord() {
        return this.record;
    }
    public Integer getLastPage() {
        return this.lastPage;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }
    public void setTotalPageCount(Integer totalPageCount) {
        this.totalPageCount = totalPageCount;
    }
    public void setLastPage(Integer lastPage) {
        this.lastPage = lastPage;
    }
}
public PageResult<List<ShoppingInfo>> getPurchaseOder(String id, OrderGetForm form) {
        String querySql = "select *";
        String countSql = " select count(id) ";
        String whereSql = " from shopping_info ";
        whereSql += " where user_trade_id = :id";
        whereSql += " order by id desc";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        String pageSql = " limit " + form.getPageNo() * form.getPageSize() + "," + form.getPageSize();
        List<ShoppingInfo> pageList = namedJdbcTemplate.query(querySql + whereSql + pageSql, paramMap, new BeanPropertyRowMapper(ShoppingInfo.class));
        Integer count = namedJdbcTemplate.queryForObject(countSql + whereSql, paramMap, Integer.class);
        PageResult<List<ShoppingInfo>> pageResult = new PageResult<>();
        pageResult.setRecord(count);
        pageResult.setPageNo(form.getPageNo());
        pageResult.setPageSize(form.getPageSize());
        pageResult.setData(pageList);
        return pageResult;
}

 

 

注解写法

@Select(value = "select * from user_account where userId = #{userId}")
UserAccount findByUserId(String userId);
@Update(value = "update user_account set balance = balance - #{money} where user_id = #{userid} and balance - #{money} >= 0")
Integer deductBalance(@Param("userid") String userid, @Param("money") Integer money);

 

 

调用存储过程

注解写法:
@Select("call proc_attestation_counter(#{date})")
@Options(statementType = StatementType.CALLABLE)
Interger recountAttestationCounter(Date date);
xml文件写法:
<select id="procAttestationCounter" statementType="CALLABLE" parameterType="Date" resultType="Integer">
    call proc_attestation_counter(#{date})
</select>

 


目录
相关文章
|
XML Java 测试技术
通义灵码与githubcopilot的对比评测
本文评测了通义灵码,与github copilot在一些代码编写能力上面的能力比较。 虽然github copilot要强很多,但灵码目前的能力也不算很弱,并且在一些小类上会做的更好一些。 值得试试看,也是免费的
58086 10
|
流计算 资源调度 Java
Apache Flink 零基础入门(二):开发环境搭建和应用的配置、部署及运行
本文主要面向于初次接触 Flink、或者对 Flink 有了解但是没有实际操作过的同学。希望帮助大家更顺利地上手使用 Flink,并着手相关开发调试工作。
Apache Flink 零基础入门(二):开发环境搭建和应用的配置、部署及运行
|
4月前
|
安全 测试技术 Linux
Acunetix v25.5.0 发布,新增功能简介
Acunetix v25.5.0 (Linux, Windows) - Web 应用程序安全测试
116 0
|
SQL Oracle 关系型数据库
SqlSugar
【8月更文挑战第1天】
334 3
|
JavaScript 开发者 UED
Vue入门到关门之第三方框架elementui
ElementUI是一个基于Vue.js的组件库,包含丰富的UI组件如按钮、表格,强调易用性、响应式设计和可自定义主题。适用于快速构建现代化Web应用。官网:[Element.eleme.cn](https://element.eleme.cn/#/zh-CN)。安装使用时,需在项目中导入ElementUI和其样式文件。
285 0
|
存储 安全 Android开发
F-Droid:尊重自由与隐私的安卓应用商店
F-Droid 是安卓平台上的自由开源应用商店,专为关注隐私和数据安全的用户设计。本文详细介绍了 F-Droid 的特点,包括其对自由和隐私的重视、无广告和无追踪代码的承诺、强大的应用搜索与管理功能,以及对开源社区的支持。用户可以通过 F-Droid 安全地浏览、安装和管理应用程序,并且开发者也可以发布开源应用。未来,F-Droid 将继续提升用户体验,鼓励更多的开发者与用户参与其中,推动自由开源软件的发展。
1024 1
|
SQL 安全 JavaScript
0x00.基础漏洞篇
0x00.基础漏洞篇
381 3
|
SQL XML Java
工作问题详解:tkMybatis通用mapper封装
工作问题详解:tkMybatis通用mapper封装
619 0
|
开发框架 NoSQL 关系型数据库
基于SqlSugar的开发框架循序渐进介绍(27)-- 基于MongoDB的数据库操作整合
基于SqlSugar的开发框架循序渐进介绍(27)-- 基于MongoDB的数据库操作整合
|
监控 安全 Ubuntu
挡不住的入侵者?试试Fail2ban,拦截黑客攻击
挡不住的入侵者?试试Fail2ban,拦截黑客攻击
876 0