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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章