ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TkMybatis用法总结

2021-07-11 15:06:52  阅读:207  来源: 互联网

标签:总结 pageNo pageSize TkMybatis 用法 record id Integer public


自定义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>

 

标签:总结,pageNo,pageSize,TkMybatis,用法,record,id,Integer,public
来源: https://blog.51cto.com/u_13270164/3035971

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有