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的映射文件中进行如下配置 <!--根据数组进 ...
随机推荐
- OpenCV——PS 滤镜, 浮雕效果
具体的算法原理可以参考: PS 滤镜, 浮雕效果 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITH ...
- mybatis源码之BaseStatementHandler
/** * @author Clinton Begin */ public abstract class BaseStatementHandler implements StatementHandle ...
- leetCode之旅(14)-Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- IOS中的数据存储方式,特点,使用情况
数据存储的核心都是写文件,主要有四种持久化方式:属性列表(Plist),对象序列化,SQLite数据库,CoreData. 存储Plist: 键值进行存储,不能存储对象.对象需要序列化编码才能写入文件 ...
- Oracle创建视图view权限不足问题剖析
问题: 使用USER1等其他用户登录Oracle以后,创建视图,提示"权限不够",怎么解决? 这是因为USER1这个帐户目前没有创建视图的权限. 解决方法为: 首先使用system ...
- rails中migration数据库后测试不通过的问题
rails项目中由于后期需求变化,需要在products数据库中增加一个字段来满足多国家商品的分类: rails g migration add_locale_to_products locale:s ...
- 详解基于vue,vue-router, vuex以及addRoutes进行权限控制
基于vuex, vue-router,vuex的权限控制教程,完整代码地址见https://github.com/linrunzheng/vue-permission-control 接下来让我们模拟 ...
- 深入源码解析类Route
微软官网对这个类的说明是:提供用于定义路由及获取路由相关信息的属性和方法.这个说明已经很简要的说明了这个类的作用,下面我们就从源码的角度来看看这个类的内部是如何工作的. public class Ro ...
- CSS 文章链接
文本溢出显示为省略号 Ellipsis for text overflow in table cell?
- N-Queens(N皇后问题)
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...