2017-06-08-忆美08天
Xplorist Lv6

2017-06-08-忆美08天

关于按条件查询

先想一下dao层中的多个条件查询用一条语句怎么实现。

只要将思路理清楚了,你才能够体会到别人的代码的精妙之处。吃透别人的代码,对自己的提升年真的很大。

其实MyBatis中的动态SQL很强大,只要了解语法就能够直接解决这些问题,权限框架中的纯jdbc与sql实现,里面要做一些比较复杂的条件判断,来生成条件判断语句。

这也说明了,很多东西都还需要深入研究与探索,现在只是掌握了一些皮毛而已,很多功能无法实现,其实就是知道的太少了,努力学习吧。

数据库中的按条件查询的SQL语句拼接可以直接使用MyBatis中的动态SQL语句来直接动态执行,不用再在Service层中进行判断,只要一个SQL映射语句就行了。

现在再考虑controller中的问题,是直接使用前面的controller还是重新创建一个controller来进行分发管理?因为按条件查询了之后还是有很多的东西都是重复的,直接整合到之前的非条件查询中就行了,只不过使用哪种数据库查询语句要经过判断。

思路都是虚的,最重要的是通过代码把思路实现了。

要想实现动态SQL的话必须使用MyBatis的特殊属性,多参数传入的每个参数绑定。

最麻烦的还是controller中如何和以前的全部查询区别开来,直接使用一个逻辑标志使其区分开。

其实前面的查询全部是动态条件查询的一种特殊情况,完全可以整合到动态查询的controller中。不用使用判断标志区分了,直接使用动态条件查询就行了。

实际的代码中才能看到真实的情况。

作为一个中国程序员,也许是一种幸运,虽然没有正宗的英语源码思维,但是可以了解更多。

想要得到更多,有时候你不得不接受混沌的状态,越是包容,混沌其实也并不是什么可怕的东西。你接受的东西多了,自然就没必要去区分混沌与秩序了。人与人之间的不同才是意义所在。

将controller中的整合,参数为空的情况。

其实dao层中的数据查询是有问题的,只是考虑到了最初的状况,并不是查询所有,每一次查询都是要分页查询的,所以dao层中的SQL映射还需要改一下。

按条件查询也要做出查询数据库中的最大数据条数。

分页是一个很重要的东西,和查询绑定了很多的东西。

代码升级进化,并不是一次性就能够想到所有的情况,代码是在一次次的迭代中进化的。

能否在Service层中进行参数的个数。SQL中能否实现一次查询然后将查询的条件不一样的结果查询出来?

根据查询的效率,最好不要将查询分页的最大条数用映射的对象集合的大小来进行,虽然这样的代码比较简洁。

但是权限查询可以整合到分页一条动态分页SQL查询语句中。

动态条件查询中的条件传输过来,不进行非空判断,直接传入到Service层中,再传到dao层中,如果不行就将null设置为“”。

将条件查询的条件放入session中,如果不放到session中那就只能够放到request中,request中进行分页比较麻烦,所以还是放到session中,但是session中提交的条件,进入显示页面中,每次都要将session中的条件清空,然后再将条件参数,分页也每次都要提交条件,显示页面中也要发生相应的变化,是否要重新创建一个显示页面。有必要,将这个页面,的东西改一改,保存了两个版本。

点击分页中的查询连接还要将条件传过去,这个怎么实现?

点击链接响应的不是直接带参数跳转,而是通过js函数处理将form表单传递过去,同时携带页数的参数。

带条件查询和普通查询还是必须分开,如果不分开的话,点击页面中的分页,带参数过去,显示就会很混乱,如果带条件查询的话进入相应的条件也进行显示,不然的话,就要通过ajax来进行显示了,刷新之后所有的条件都为空了。

所以现在就有两种选择:

1.再专门创建一个条件显示的页面,直接跳转过去显示

2.使用ajax局部刷新,直接在当前页面进行显示,同时条件输入框的东西都还在。

好纠结,是使用ajax呢?还是使用到一个专门的条件显示页面进行显示呢?

感觉这个按条件查询真的不简单。

如果使用跳转专门的条件显示页面的话,其实也还挺简单,复制代码就行了,但是体验并不好,实现分页的跳转就直接通过专门的session中的参数进行分页跳转就行了。

反正两种方式都能够实现,就选择使用ajax这种方式来进行实现吧。

使用ajax来实现的话,点击查询之后进入到服务器,服务器成功响应了,就会将前面的显示列表和分页效果显示全部清空,然后将服务器中响应的数据以json格式响应到页面中,页面中的js再对json数据解析然后加工成要显示的列表数据和分页效果显示。

ajax请求不返回字符串怎么办?

ajax响应的是json格式的字符串数据,将数据进行转换成json数据,怎么转换?

js中解析json数据就简单很多了,只要在ajax中声明返回的数据类型是json就行了。

想要在网页上显示分页效果,生成的分页效果,不能够使用之前的分页效果,因为之前的不是ajax,这是两种不同的模式。

分页的链接使用js函数,传递form表单的条件参数到统一请求映射方法中。

列表显示和分页的链接显示都该统一起来,对于上页,下页,具体页的当前页数据处理就用js处理。

关于分页的显示,使用ajax从Service层中获取数据传过来,反正不多,而且Java代码中处理更方便。

先将要生成的页面先写出来,将数据

 评论
// variables $gt-color-main := #6190e8 $gt-color-sub := #a1a1a1 $gt-color-loader := #999999 $gt-color-error := #ff3860 $gt-color-hr := #e9e9e9 $gt-color-comment-txt := #333333 $gt-color-link-active := #333333 $gt-color-btn := #ffffff $gt-size-base := 16px // default font-size $gt-size-border-radius := 5px $gt-breakpoint-mobile := 479px $gt-mask-z-index := 9999 // functions & mixins clearfix() { &:before &:after { display table clear both content "" } } em($px, $base-size = $gt-size-base) { u = unit($px) if (u is 'px') { unit($px / $base-size, 'em') } else { unit($px, u) } } mobile() { @media (max-width $gt-breakpoint-mobile) { {block} } } // variables - calculated $gt-size-loader-dot := em(6px) $gt-size-loader := em(28px) $gt-size-avatar := em(50px) $gt-size-avatar-mobi := em(32px) // styles // Put everything under container to avoid style conflicts .comments-container { .gt-container { box-sizing border-box * { box-sizing border-box } font-size $gt-size-base // common a { color $gt-color-main &:hover { color lighten($gt-color-main, 20%) border-color lighten($gt-color-main, 20%) } &.is--active { color $gt-color-link-active cursor default !important &:hover { color $gt-color-link-active } } } .hide { display none !important } // icons .gt-svg { display inline-block width em(16px) height em(16px) vertical-align sub svg { width 100% height 100% fill $gt-color-main } } .gt-ico { display inline-block &-text { margin-left em(5px) } &-github { width 100% height 100% .gt-svg { width 100% height 100% } svg { fill inherit } } } // loader .gt-spinner { position relative &::before { position absolute top 3px box-sizing border-box width em(12px) height em(12px) margin-top em(-3px) margin-left em(-6px) border 1px solid $gt-color-btn border-top-color $gt-color-main border-radius 50% animation gt-kf-rotate 0.6s linear infinite content '' } } .gt-loader { position relative display inline-block width $gt-size-loader height $gt-size-loader font-style normal // font-size: $gt-size-loader line-height $gt-size-loader border 1px solid $gt-color-loader border-radius 50% animation ease gt-kf-rotate 1.5s infinite &:before { position absolute top 0 left 50% display block width $gt-size-loader-dot height $gt-size-loader-dot margin-top -($gt-size-loader-dot / 2) margin-left -($gt-size-loader-dot / 2) background-color $gt-color-loader border-radius 50% content '' } } // avatar .gt-avatar { display inline-block width $gt-size-avatar height $gt-size-avatar +mobile() { width $gt-size-avatar-mobi height $gt-size-avatar-mobi } img { width 100% height auto border-radius 3px } &-github { width $gt-size-avatar - em(2px) height $gt-size-avatar - em(2px) cursor pointer +mobile() { width $gt-size-avatar-mobi - em(2px) height $gt-size-avatar-mobi - em(2px) } } } // button .gt-btn { display inline-block padding em(12px) em(20px) color $gt-color-btn font-size em(12px) line-height 1 white-space nowrap text-decoration none background-color $gt-color-main border 1px solid $gt-color-main border-radius $gt-size-border-radius outline none cursor pointer &-text { font-weight 400 } &-loading { position relative display inline-block width em(12px) height em(16px) margin-left em(8px) vertical-align top } &.is--disable { cursor not-allowed opacity 0.5 } &-login { margin-right 0 } &-preview { color $gt-color-main background-color var(--background-color) &:hover { background-color var(--second-background-color) } } &-public { &:hover { background-color lighten($gt-color-main, 20%) border-color lighten($gt-color-main, 20%) } } } } &-loadmore // loadmore /* error */ .gt-error { margin em(10px) color $gt-color-error text-align center } // initing .gt-initing { padding em(20px) 0 text-align center &-text { margin em(10px) auto font-size 92% } } // no int .gt-no-init { padding em(20px) 0 text-align center } // link .gt-link { border-bottom 1px dotted $gt-color-main &-counts &-project { text-decoration none } } // meta .gt-meta { position relative z-index 10 margin em(20px) 0 padding em(16px) 0 font-size em(16px) border-bottom 1px solid $gt-color-hr clearfix() } .gt-counts { margin 0 em(10px) 0 0 color var(--default-text-color) } .gt-user { float right margin 0 font-size 92% &-pic { width 16px height 16px margin-right em(8px) vertical-align top } &-inner { display inline-block cursor pointer .gt-user-name { color var(--default-text-color) } } .gt-ico { margin 0 0 0 em(5px) svg { fill var(--default-text-color) } } .is--poping { .gt-ico { svg { fill $gt-color-main } } } } .gt-version { margin-left em(6px) color $gt-color-sub } .gt-copyright { margin 0 em(15px) em(8px) padding-top em(8px) border-top 1px solid var(--border-color) } // popup .gt-popup { position absolute top em(38px) right 0 display inline-block padding em(10px) 0 font-size em(14px) letter-spacing 0.5px background var(--background-color) border 1px solid var(--border-color) .gt-action { position relative display block margin em(8px) 0 padding 0 em(18px) text-decoration none cursor pointer &.is--active { &:before { position absolute top em(7px) left em(8px) width em(4px) height em(4px) background $gt-color-main content '' } } } } // header .gt-header { position relative display flex &-comment { flex 1 margin-left em(20px) +mobile() { margin-left em(14px) } } &-textarea { display block box-sizing border-box width 100% min-height em(82px) max-height em(240px) padding em(12px) color var(--default-text-color) font-size em(14px) word-wrap break-word background-color var(--fourth-text-color) border 1px solid var(--border-color) border-radius $gt-size-border-radius outline none transition all 0.25s ease resize vertical &:hover { background-color var(--background-color) } } &-preview { padding em(12px) background-color var(--background-color) border 1px solid var(--border-color) border-radius $gt-size-border-radius } &-controls { position relative margin em(12px) 0 0 clearfix() +mobile() { margin 0 } &-tip { color $gt-color-main font-size em(14px) text-decoration none vertical-align sub +mobile() { display none } } .gt-btn { float right margin-left em(20px) +mobile() { float none width 100% margin em(12px) 0 0 } } } } &:after { position fixed top 0 right 0 bottom 100% left 0 opacity 0 content '' } &.gt-input-focused { position relative &:after { position fixed top 0 right 0 bottom 0 left 0 z-index $gt-mask-z-index background #000 opacity 0.6 transition opacity 0.3s, bottom 0s content '' } .gt-header-comment { z-index $gt-mask-z-index + 1 } } // comments .gt-comments { padding-top em(20px) &-null { text-align center } &-controls { margin em(20px) 0 text-align center } } // comment .gt-comment { position relative display flex padding em(10px) 0 &-content { flex 1 margin-left em(20px) padding em(12px) em(16px) overflow auto background-color var(--second-background-color) transition all ease 0.25s +mobile() { margin-left em(14px) padding em(10px) em(12px) } } &-header { position relative margin-bottom em(8px) font-size em(14px) } &-block-1 { float right width em(32px) height em(22px) } &-block-2 { float right width em(64px) height em(22px) } &-username { color $gt-color-main font-weight 500 text-decoration none &:hover { text-decoration underline } } &-text { margin-left em(8px) color $gt-color-sub } &-date { margin-left em(8px) color $gt-color-sub } &-like &-edit &-reply { position absolute height em(22px) &:hover { cursor pointer } } &-like { top 0 right em(32px) } &-edit &-reply { top 0 right 0 } &-body { // color: $gt-color-comment-txt !important color var(--second-text-color) !important .email-hidden-toggle a { display inline-block height 12px padding 0 9px color #444d56 font-weight 600 font-size 12px line-height 6px text-decoration none vertical-align middle background #dfe2e5 border-radius 1px &:hover { background-color #c6cbd1 } } .email-hidden-reply { display none white-space pre-wrap .email-signature-reply { margin 15px 0 padding 0 15px color #586069 border-left 4px solid #dfe2e5 } } .email-hidden-reply.expanded { display block } } &-admin { .gt-comment-content { background-color var(--fourth-text-color) } } } @keyframes gt-kf-rotate { 0% { transform rotate(0) } 100% { transform rotate(360deg) } } }