In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "ibatis how to dynamically query the sql code inside", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "ibatis how to dynamically query the sql code inside" it!
Sql code in ibatis dynamic query:
Ibatis dynamic query Xml code:
AuthorId=#authorId#
MarketId=#marketId#
IsDelete=#isDelete#
IsBest=#isBest#
$statusStr$
# marketIdList [] #
$orderStr$
Limit # begin#
# max#
AuthorId=#authorId#
MarketId=#marketId#
IsDelete=#isDelete#
IsBest=#isBest#
$statusStr$
# marketIdList [] #
Ibatis dynamic query Xml code:
AuthorId=#authorId#
MarketId=#marketId#
IsDelete=#isDelete#
IsBest=#isBest#
$statusStr$
# marketIdList [] #
$orderStr$
Limit # begin#
# max#
AuthorId=#authorId#
MarketId=#marketId#
IsDelete=#isDelete#
IsBest=#isBest#
$statusStr$
# marketIdList [] #
What needs to be noted here is:
# xxx# represents the attribute value of xxx, the key in map or the attribute in your pojo object, and ibatis will automatically put quotation marks around it, as shown in the sql statement like where xxx = 'xxx'; while $xxxx$ concatenates xxxx as a string into your sql statement, such as order by topicId, if you use # instead of $, it will be enclosed in quotation marks. Order by # xxx# (xxx is the string topicId you passed in), ibatis will translate it into order by 'topicId' and report an error. This is the result of $order by topicId.
In addition, we should pay attention to its iterate in ibatis dynamic query.
Java code
# marketIdList [] #
Pay attention to the property attribute of iterate, although you have this sentence in the isNotNull above, but you must write it clearly here, otherwise ibatis will not find your list, and ibatis dynamic query will not be realized.
Data access layer code in ibatis dynamic query:
Java code
Public ListgetTopics (Mapmap) {
Return getSqlMapClientTemplate () .queryForList (getTopics, map)
}
Service layer code in ibatis dynamic query:
Java code
Public ListgetTopicsByMarketIdList (Long authorId,ListmarketIdList
Integer orderby, Integer status, Pagination pagination) {
Mapmap = new HashMap ()
Map.put ("authorId", authorId)
Map.put ("isDelete", false)
Map.put ("marketIdList", marketIdList)
Map.put ("orderStr", "here you assemble your order string")
Map.put ("statusStr", "here you assemble your status string")
Map.put ("begin", pagination.getOffset ())
Map.put ("max", pagination.getPageSize ())
/ / the getTopicCount () method is roughly the same as getTopics (), so I omitted it in my dao
Long total = topicDao.getTopicCount (map)
If (total = = 0) {
Return new ArrayList ()
} else {
Pagination.setTotal (total)
Listres = topicDao.getTopics (map)
Return res
}
}
Java code
Public ListgetTopicsByMarketIdList (Long authorId,ListmarketIdList
Integer orderby, Integer status, Pagination pagination) {
Mapmap = new HashMap ()
Map.put ("authorId", authorId)
Map.put ("isDelete", false)
Map.put ("marketIdList", marketIdList)
Map.put ("orderStr", "here you assemble your order string")
Map.put ("statusStr", "here you assemble your status string")
Map.put ("begin", pagination.getOffset ())
Map.put ("max", pagination.getPageSize ())
/ / the getTopicCount () method is roughly the same as getTopics (), so I omitted it in my dao
Long total = topicDao.getTopicCount (map)
If (total = = 0) {
Return new ArrayList ()
} else {
Pagination.setTotal (total)
Listres = topicDao.getTopics (map)
Return res
}
}
Java code
Public class Topic extends BaseObject implements Serializable {
/ * *
*
, /
Private static final long serialVersionUID =-851973667810710701L
Private Long id
Private Long authorId
Private String authorName
Private Long marketId
Private String title
Private String tags
Private String content
Private Date pubdate
Private Integer isBest
Private Integer status
Private Integer isDelete
Private Integer clickCount
Private Integer replyCount
Private Date lastReplyTime
/ / getter and setter omitted...
}
Java code
Public class Topic extends BaseObject implements Serializable {
/ * *
*
, /
Private static final long serialVersionUID =-851973667810710701L
Private Long id
Private Long authorId
Private String authorName
Private Long marketId
Private String title
Private String tags
Private String content
Private Date pubdate
Private Integer isBest
Private Integer status
Private Integer isDelete
Private Integer clickCount
Private Integer replyCount
Private Date lastReplyTime
/ / getter and setter omitted...
}
Pagination code in ibatis dynamic query:
Java Code:
Public class Pagination {
/ * *
* Page number to view
, /
Private int page
/ * *
* number of displays per page
, /
Private int pageSize
/ * *
* how many pages are there?
, /
Private int totalPage
/ * *
* how many records are there?
, /
Private long total
/ * *
* number of records on the current page
, /
Private int size
/ * *
* only need topxx, not page count information
, /
Private boolean topOnly
/ * *
* starting with the number of records
, /
Private int offset
Public void setOffset (int offset) {
This.offset = offset
}
Public Pagination (int page, int pageSize) {
This.page = page
This.pageSize = pageSize
}
Public Pagination () {
}
Public boolean require () {
Return pageSize > 0? True: false
}
Public int from () {
Return page * pageSize
}
Public int to () {
Return from () + size
}
Public int getPage () {
Return page
}
Public void setPage (int page) {
This.page = page
}
Public int getPageSize () {
Return pageSize
}
Public void setPageSize (int pageSize) {
This.pageSize = pageSize
}
Public int getTotalPage () {
Return totalPage
}
Public void setTotalPage (int totalPage) {
This.totalPage = totalPage
}
Public long getTotal () {
Return total
}
Public void setTotal (long total) {
This.total = total
If (pageSize > 0) {
This.totalPage = int) Math.ceil (total / (double) pageSize)
} else {
This.totalPage = 1
}
If (page > = totalPage) {
Page = totalPage-1
}
If (page
< 0) page = 0; if (pageSize >0) {
If (page < totalPage-1)
This.size = pageSize
Else
This.size = (int) (total% pageSize)
} else
Thank you for your reading, the above is the content of "ibatis how to dynamically query the sql code inside". After the study of this article, I believe you have a deeper understanding of how to dynamically query the sql code inside ibatis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.