| | |
| | | package com.hx.mybatisTool; |
| | | |
| | | import com.hx.exception.TipsException; |
| | | import com.hx.util.SimpleTool; |
| | | import com.hx.util.StringUtils; |
| | | |
| | | import java.util.Map; |
| | |
| | | */ |
| | | public class SqlSentence { |
| | | |
| | | /**执行语句*/ |
| | | private String sqlSentence; |
| | | |
| | | /**新增存在查询判断*/ |
| | | private String whereExist; |
| | | |
| | | private Map<String,Object> m; |
| | | /**类型*/ |
| | | private Integer type = TYPE_NORMAL; |
| | | /**排序*/ |
| | | private String orderBy; |
| | | /**开始页数*/ |
| | | private Integer startPage = 0; |
| | | /**每页数量*/ |
| | | private Integer pageNum = 0; |
| | | /**条数(当前类使用)*/ |
| | | private Integer pageSize; |
| | | |
| | | ////////////////////////////////////////////////////////////// |
| | | /**正常*/ |
| | | public static final int TYPE_NORMAL = 0; |
| | | /**动态*/ |
| | | public static final int TYPE_DYN = 1; |
| | | |
| | | /********************mother****************************/ |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 设置动态查询 |
| | | * @param map 查询 |
| | | * sql整条语句-分页 |
| | | * @param sql 如:select * from user Where name = #{m.userName} order by age desc |
| | | * @param values 存放的值如:values.put("userName","ChenJiaHe") |
| | | * @param pageNum 当前页数 |
| | | * @param pageSize 当前条数 |
| | | */ |
| | | public void setDynSql(Map<String, Object> map) |
| | | { |
| | | this.type = TYPE_DYN; |
| | | this.m = map; |
| | | public void sqlSentencePage(String sql,Map<String,Object> values,int pageNum,int pageSize) { |
| | | if(StringUtils.isEmpty(sql)) { |
| | | throw new TipsException("sql is null"); |
| | | } |
| | | |
| | | if(values == null) { |
| | | throw new TipsException("values is null"); |
| | | } |
| | | if(pageNum < 1){ |
| | | pageNum = 1; |
| | | } |
| | | if(pageSize < 1){ |
| | | pageSize = 1; |
| | | } |
| | | this.pageNum = pageNum; |
| | | this.pageSize = pageSize; |
| | | |
| | | //算起点 |
| | | int pageStart = (pageNum-1)*pageSize; |
| | | sql = sql+" LIMIT "+pageStart+","+pageSize; |
| | | sqlSentence = sql; |
| | | m = values; |
| | | } |
| | | |
| | | /**分页组装信息 |
| | | * @param data 装载信息 |
| | | * @param totalNum 总页数 |
| | | * @return 装载信息 |
| | | */ |
| | | public Map<String,Object> pageAssembleData(Map<String,Object> data,int totalNum){ |
| | | data.put("pageNum",pageNum); |
| | | data.put("pageSize",pageSize); |
| | | data.put("total",totalNum); |
| | | int pages; |
| | | if(pageSize > 0){ |
| | | if(totalNum%pageSize > 0){ |
| | | pages = totalNum/pageSize+1; |
| | | }else{ |
| | | pages = totalNum/pageSize; |
| | | } |
| | | }else{ |
| | | pages = 0; |
| | | } |
| | | data.put("pages",pages); |
| | | if(pages <= pageNum){ |
| | | data.put("isLastPage",true); |
| | | }else{ |
| | | data.put("isLastPage",false); |
| | | } |
| | | return data; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 查询的语句 |
| | | * @param sql 如:id = #{m.userId} order by age DESC |
| | | * @param values 存放的值如:values.put("userId","123456") |
| | | */ |
| | | public void sqlWhere(String sql,Map<String,Object> values) { |
| | | if(!SimpleTool.checkNotNull(values)){ |
| | | throw new TipsException("values is null"); |
| | | } |
| | | if(!SimpleTool.checkNotNull(sql)) { |
| | | sql = "1=1"; |
| | | } |
| | | sqlSentence = sql; |
| | | m = values; |
| | | } |
| | | |
| | | /** |
| | | * 更新语句的语句 |
| | | * @param sql 如:name = #{m.name},age = ? WHERE id = #{m.id} |
| | | * @param values 存放的值 |
| | | */ |
| | | public void sqlUpdate(String sql,Map<String,Object> values) { |
| | | if(!SimpleTool.checkNotNull(values)){ |
| | | throw new TipsException("values is null"); |
| | | } |
| | | m = values; |
| | | sqlSentence = sql; |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | public void setSqlSentence(String sqlSentence) { |
| | | this.sqlSentence = sqlSentence; |
| | | } |
| | | |
| | | public Integer getType() { |
| | | return type; |
| | | } |
| | | |
| | | public void setType(Integer type) { |
| | | this.type = type; |
| | | } |
| | | |
| | | public String getOrderBy() { |
| | |
| | | return (startPage - 1) * pageNum; |
| | | } |
| | | |
| | | public String getWhereExist() { |
| | | return whereExist; |
| | | } |
| | | |
| | | public void setWhereExist(String whereExist) { |
| | | if(StringUtils.isNull(whereExist)){ |
| | | whereExist = null; |
| | | } |
| | | this.whereExist = whereExist; |
| | | } |
| | | |
| | | public Integer getPageSize() { |
| | | return pageSize; |
| | | } |
| | | |
| | | public void setPageSize(Integer pageSize) { |
| | | this.pageSize = pageSize; |
| | | } |
| | | } |
| | | |
| | | |