SSM-MyBatis-14:Mybatis中智能标签
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
谈论到智能,有什么要想的没有?
我下面放张图
相信都见过这个吧,你在之前没有学习过框架的时候怎么写的,动态sql?还是。。。
智能标签可以解决类似问题
它可以在sql语句中随传入参数是否为null甚至其他来自行加where或者and,或者其他等等用法
他分为 where ,if ,choose ,foreach的array方式 ,foreach的list方式 ,foreach的list自定义类型方式
我一块放在下面,对照着看吧
实体类
public class Book {
private Integer bookID;
private String bookName;
private String bookAuthor;
private Integer bookPrice; public Book() {
} public Integer getBookID() {
return this.bookID;
} public void setBookID(Integer bookID) {
this.bookID = bookID;
} public String getBookName() {
return this.bookName;
} public void setBookName(String bookName) {
this.bookName = bookName;
} public String getBookAuthor() {
return this.bookAuthor;
} public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
} public Integer getBookPrice() {
return this.bookPrice;
} public void setBookPrice(Integer bookPrice) {
this.bookPrice = bookPrice;
}
}
接口中的方法
//智能标签where if
public List<Book> findtrueBookByIf(String bookName,Integer bookPrice);
//智能标签where choose
public List<Book> findtrueBookByChoose(Integer bookPrice);
//智能标签where foreach array
public List<Book> findtrueBookByForeachArray(int [] array);
//智能标签where foreach list
public List<Book> findtrueBookByForeachList(List<Integer> list);
//智能标签where foreach list
public List<Book> findtrueBookByForeachListBook(List<Book> list);
小配置中
<!--智能标签,where if-->
<select id="findtrueBookByIf" resultType="Book">
select * from book
<where>
<if test="#{0}!=null">
AND bookName LIKE '%' #{} '%'
</if>
<if test="#{0}!=null">
AND bookPrice>#{}
</if>
</where>
</select>
<!--智能标签,where choose-->
<select id="findtrueBookByChoose" resultType="Book">
select * from book
<where>
<choose>
<when test="#{0}!=null">
AND bookPrice>#{}
</when>
<otherwise>=</otherwise>
</choose>
</where>
</select>
<!--智能标签,where foreach array-->
<select id="findtrueBookByForeachArray" resultType="Book">
select * from book
<where>
bookID IN
<foreach collection="array" open="(" close=")" separator="," item="myid">
#{myid}
</foreach>
</where>
</select>
<!--智能标签,where foreach list-->
<select id="findtrueBookByForeachList" resultType="Book">
select * from book
<where>
bookID IN
<foreach collection="list" open="(" close=")" separator="," item="myid">
#{myid}
</foreach>
</where>
</select>
<!--智能标签,where foreach list book-->
<select id="findtrueBookByForeachListBook" resultType="Book">
select * from book
<where>
bookID IN
<foreach collection="list" open="(" close=")" separator="," item="book">
#{book.bookID}
</foreach>
</where>
</select>
测试类中
///智能标签where + foreach list Book自定义list 进行多条件查询
@Test
public void t9selectZhiNengByForeachListBook(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> list=new ArrayList<Book>();
Book b1=new Book();
b1.setBookID();
Book b2=new Book();
b2.setBookID();
list.add(b1);
list.add(b2);
List<Book> books = mapper.findtrueBookByForeachListBook(list);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + foreach list 进行多条件查询
@Test
public void t8selectZhiNengByForeachList(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Integer> list=new ArrayList<Integer>();
list.add();
list.add();
List<Book> books = mapper.findtrueBookByForeachList(list);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + foreach array 进行多条件查询
@Test
public void t7selectZhiNengByForeachArray(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
int[] array={,};
List<Book> books = mapper.findtrueBookByForeachArray(array);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + choose进行多条件查询
@Test
public void t6selectZhiNengByChoose(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> books = mapper.findtrueBookByChoose();
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); } ///智能标签where + if 进行多条件查询
@Test
public void t5selectZhiNengByIf(){
SqlSession session= MyBatisUtils.getSession(); IBookDAO mapper = session.getMapper(IBookDAO.class);
List<Book> books = mapper.findtrueBookByIf("心",);
for (Book items:books) {
System.out.println(items.getBookName());
} session.close(); }
打完收工
SSM-MyBatis-14:Mybatis中智能标签的更多相关文章
- 后端分页神器,mybatis pagehelper 在SSM与springboot项目中的使用
mybatis pagehelper想必大家都耳熟能详了,是java后端用于做分页查询时一款非常好用的分页插件,同时也被人们称为mybatis三剑客之一,下面 就给大家讲讲如何在SSM项目和sprin ...
- java web,从零开始,一步一步配置ssm(Spring+SpringMVC+MyBatis)框架
1.安装JDK: 安装之后要配置环境变量,在系统变量里: 新建变量名JAVA_HOME,变量值C:\Program Files\Java\jdk1.8.0_77: 新建变量名CLASSPATH,变量值 ...
- SSH(Struts,Spring,Hibernate)和SSM(SpringMVC,Spring,MyBatis)的区别
SSH 通常指的是 Struts2 做前端控制器,Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做前端控制器,Spring 管理各层的组件,M ...
- SSM Spring+SpringMVC+mybatis+maven+mysql环境搭建
SSM Spring+SpringMVC+mybatis+maven环境搭建 1.首先右键点击项目区空白处,选择new->other..在弹出框中输入maven,选择Maven Project. ...
- SSM(Spring + Springmvc + Mybatis)框架面试题
JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...
- SSM(Spring +SpringMVC + Mybatis)框架搭建
SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...
- SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)
1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...
- 使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架
基本概念 使用SSM(Spring,SpringMVC和Mybatis) 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod ...
- MyBatis系列四 之 智能标签进行查询语句的拼接
MyBatis系列四 之 智能标签进行查询语句的拼接 使用Foreach进行多条件查询 1.1 foreach使用数组进行多条件查询 在MyBatis的映射文件中进行如下配置 <!--根据数组进 ...
随机推荐
- Make3D Convert your image into 3d model
Compiling and Running Make3D on your own computer source: http://make3d.cs.cornell.edu/code_linux.ht ...
- TCP的核心系列 — 重传队列的更新和时延的采样(二)
在tcp_clean_rtx_queue()中,并非对每个ACK都进行时延采样.是否进行时延采样,跟这个ACK是否为 重复的ACK.这个ACK是否确认了重传包,以及是否使用时间戳选项都有关系. 本文主 ...
- 【LaTeX排版】LaTeX论文排版<三>
A picture is worth a thousand words(一图胜千言).图在论文中的重要性不言而喻,本文主要讲解图的制作与插入. 1.图像的插入 图像可以分为两大类:位图和向量图 ...
- 苹果新的编程语言 Swift 语言进阶(八)--属性
属性是特定类.结构或枚举的相关值,属性根据作用域不同分为实例属性与类型属性,还可以根据是否存储分为存储属性和计算属性. 1.1 实例属性 为一个类.结构或枚举定义的属性默认属于实例属性,即该属性属于为 ...
- SQL 是一门美丽的语言 她来自艺术
有一种语言可以从诞生一直活跃到现在,有一个梦想从南四楼蔓延到北五楼再走向世界,有一种坚持可以从懵懂年少成长为干练成熟,有一本书可以温暖心灵彼岸,与数据库抨击撞出火花,有一个系统足以让你忘 ...
- 如何使用firefox适用于javascript的debugger命令
首先安装firebug,在firefox的扩展里搜索安装即可. 然后在页面中启用firebug中的脚本: 然后在网页某些位置加入debugger命令,比如如下页面代码: <!DOCTYPE ht ...
- 恶补web之二:css知识(2)
css字体属性定义文本的字体系列,大小,加粗,风格和变形等. css中包含两种字体系列:通用字体系列和特定字体系列. font-family属性定义文本的字体系列: body {font-family ...
- miniUI input设置默认值,js获取年月注意事项,数据库nvl函数使用
2017-6-5周一,今天碰到的一个需求是:两税附征模块进入页面筛选时间默认值为当前月的上一个月,并根据筛选结果显示数据,我们用的框架为miniUI. 坑1: 默认值设置,刚刚接触miniUI,对里面 ...
- postgresql to_char 问题
select create_time from xxx; select to_char(create_time,'yyyy-MM-dd HH24:mm:ss') as create_time fr ...
- Pycharm安装教程
1.下载PyQt 官方网站:http://www.riverbankcomputing.com/software/pyqt/download5 我的操作系统是64位的,安装的是Python3.4.3, ...