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>

 


目录
相关文章
|
Java
百度搜索:蓝易云【hutool Http 工具发送POST请求的几种方式。】
以上是使用Hutool发送POST请求的几种方式。根据实际需求和代码复杂度,选择合适的方式来发送POST请求。
1332 0
JDK8之stream流的使用:分组
JDK8之stream流的使用:分组
903 0
|
Java Maven
IDEA离线使用本地maven仓库
IDEA离线使用本地maven仓库
4622 1
IDEA离线使用本地maven仓库
|
小程序 数据库 微服务
结合ruoyi-cloud和ruoyi-app实现微信小程序的授权登录
结合ruoyi-cloud和ruoyi-app实现微信小程序的授权登录
1758 0
|
6月前
OpenCode新版与Spec-Kit新版
本文详解OpenCode+Spec-Kit组合安装报错原因:新版OpenCode(1.14.28)与Spec-Kit不兼容,后者已移除OpenCode模板。解决方案为锁定旧版本——OpenCode 1.4.7 + specify-cli 0.4.1,可完美规避兼容性问题。(239字)
598 0
|
负载均衡 Java Nacos
Nacos有几种负载均衡策略?
Nacos有几种负载均衡策略?
1902 0
Nacos有几种负载均衡策略?
|
SQL 敏捷开发 Java
Springboot 整合tk-mybatis , 妈妈,我再也不想敲CRUD的代码了!
Springboot 整合tk-mybatis , 妈妈,我再也不想敲CRUD的代码了!
1753 0
Springboot 整合tk-mybatis , 妈妈,我再也不想敲CRUD的代码了!
|
Java 数据安全/隐私保护 Spring
springboot实现邮箱发送(激活码)功能
本文介绍了如何在Spring Boot应用中配置和使用邮箱发送功能,包括开启邮箱的SMTP服务、添加Spring Boot邮件发送依赖、配置application.properties文件,以及编写邮件发送的代码实现。
1077 2
springboot实现邮箱发送(激活码)功能
|
开发工具 git 开发者
|
UED
【Qt 学习笔记】Qt窗口 | 工具栏 | QToolBar的使用及说明
【Qt 学习笔记】Qt窗口 | 工具栏 | QToolBar的使用及说明
3188 2