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>

 


目录
打赏
0
0
0
0
25
分享
相关文章
runas的用法
今天同事的电脑安装了一个软件,运行时需要管理员权限,因为是在域环境中,无法提供管理员权限,这种情况可以用到runas。runas命令就是可以在A账户中用B账户运行某个软件。 runas /user:用户名 软件路径 在了解了runas用法后,我先在CMD中输入命令 需要输入账户密码 输入密码成功后就可以运行软件了,检查一下,是以这个用户运行的软件 后来想想运行这个命令每次都要输入管理员密码,这就没什么效果了。
2730 0
|
10月前
ThreadHelper用法
ThreadHelper用法
52 0
/与%,%与/的用法
/与%,%与/的用法
183 0
JackJSON的用法
网上大多数都是FastJSON的用法,很少有JackJSON的用法。我总结了几个自己经常用到的,供大家参考。
${}用法
[el表达式],它会从page,request,session,application中取值。比如:{name}它的意思就从以上4个对象中去名为name的值。
1394 0
EasyTouch基本用法
EasyTouch基本用法 本文提供全流程,中文翻译。Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) ...
1550 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等