In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Tk.mybatiszero SQL statement to achieve dynamic sql query methods have", 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 the "Tk.mybatiszero SQL statement to achieve dynamic sql query method which" bar!
Sometimes, you need to use dynamic sql to query data according to the conditions, so you need to use dynamic sql. Usually we write dynamic sql to implement it, such as:
SELECT id, name, score FROM tbl_student score > # {score} AND name LIKE # {pattern} ORDER BY score DESc
This sql is a query for student information with a score greater than 90 and whose name contains "I". But sometimes add a condition, but also to change the sql, change the parameter, is there a way to write dynamic sql just like writing code? If you have this idea, it is recommended that you learn about Tk.mybatis.
Implementation method:
1. Using Example to implement
two。 Use Example.createCriteria
3. Using Example.builder to implement
4. Using WeekendSqls to implement
Method 1: implement / * using Example first: use example query * / @ Test public void testSelectByExample () {Example example = new Example (Student.class); / / set query column example.selectProperties ("id", "name", "score"); / / dynamic sql example.and () .andGreaterThan ("score", 90) .andLike ("name", "% I%") / / deduplicated example.setDistinct (true); / / sort example.orderBy ("score"). Desc (); List students = studentMapper.selectByExample (example); System.out.println (students);} method 2: use example.createCriteria to implement / * second: use example.createCriteria query * / @ Test public void testSelectByExampleCriteria () {Example example = new Example (Student.class) / / set query column example.selectProperties ("id", "name", "score"); / / dynamic query example.createCriteria () .andGreaterThan ("score", 90) .andLike ("name", "% I%"); / / deduplicate example.setDistinct (true); / / sort example.orderBy ("score"). Desc (); List students = studentMapper.selectByExample (example) System.out.println (students) Method 3: use Example.builder to implement / * third: use Example.builder to implement * / @ Test public void testSelectByExampleBuilder () {Example example = Example.builder (Student.class) / / query column .select ("id", "name", "score") / / dynamic sql .where (Sqls.custom () .andGreaterThan ("score") 90) .andLike ("name", "% I%") / / remove duplicates / / sort .orderByDesc ("score") .build () List students = studentMapper.selectByExample (example); System.out.println (students);} method 4: use weekendSqls to implement / * * fourth: use weekendSqls to implement * / @ Test public void testSelectByWeekendSqls () {WeekendSqls sqls = WeekendSqls.custom (); sqls = sqls .andGreaterThan (Student::getScore,90) .andLike (Student::getName, "% I%") List sysRoles = studentMapper.selectByExample (Example.builder (Student.class). Select ("id", "name", "score") .where (sqls) .select () .orderByDesc ("score") .build (); System.out.println (sysRoles) Thank you for your reading, the above is the content of "what are the methods of Tk.mybatiszero SQL statement to achieve dynamic sql query". After the study of this article, I believe you have a deeper understanding of the method of Tk.mybatiszero SQL statement to achieve dynamic sql query, and the specific use still 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.