如何申请一个网站 做视频直播,app运营策划,网站备案资料修改,网站生成手机端文章目录 1. 什么是动态sql2. 动态sql之if3. 动态sql之where4. 动态sql之foreach5. sql片段抽取 此篇的代码基于
【Spring集成MyBatis】MyBatis的Dao层实现#xff08;基于配置#xff0c;非注解开发#xff09;续写 1. 什么是动态sql
MyBatis映射… 文章目录 1. 什么是动态sql2. 动态sql之if3. 动态sql之where4. 动态sql之foreach5. sql片段抽取 此篇的代码基于
【Spring集成MyBatis】MyBatis的Dao层实现基于配置非注解开发续写 1. 什么是动态sql
MyBatis映射文件中的SQL都比较简单有时当业务逻辑复杂时SQL是动态变化的此时在前面学的简单SQL就无法满足需求了。 具体地说假如我的数据库中有id,username,password三个字段假如我想查询usernamexxx的所有用户信息时候查询语句是 select * from user where usernamexxx 当我想查询usernamexxx且passwordxxx的所有用户信息时查询语句是 select * from user where usernamexxx and passwordxxx 这两者的共通之处就是username和password其实都是User里面的的一个属性假如我的输入以User为条件而不是以string为条件让SQL语句自动去判断我要根据什么查询有username根据username查询有password根据password查询有username和password时根据二者共同查询 如果我写的是select * from user where id#{id} and username#{username} and password#{password}此时sql语句等效为图中红字部分
但是这不是我们理想的效果我们想的是当password时语句自动变为 select * from user where id#{id} and username#{username} 这时候就无法查询出结果如果我们想要查询出结果就需要使用动态sql去实现
2. 动态sql之if
如果有if语法我们就可以很好地拼接SQL语句假如id ! 0就写 id#{id}假如username就写username#{username}依此类推
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.demo.dao.UserMapperselect idfindByCondition parameterTypeuser resultTypeuserselect * from user where 11if testid!0and id#{id}/ifif testusername!nulland username#{username}/ifif testpassword!passwordand password#{password}/if/select/mapperif标签里的test写的就是判断再次运行测试代码仍能够查询出结果
package com.example.demo.service;import com.example.demo.dao.UserMapper;
import com.example.demo.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class ServiceDemo {public static void main(String[] args) throws IOException {InputStream inputStream Resources.getResourceAsStream(sqlMapConfig.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream);SqlSession sqlSession sqlSessionFactory.openSession();UserMapper userMapper sqlSession.getMapper(UserMapper.class);// 模拟条件userUser condition new User();condition.setId(0);condition.setUsername(aaa);ListUser userList userMapper.findByCondition(condition);System.out.println(userList);}
}结果
3. 动态sql之where
上面为了保证在无条件的时候不报错、也能够查询我们还多写了一个where 11 为了使得代码更优雅即有需要写的where时写上where无需where时不写还可以修改如下
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.demo.dao.UserMapperselect idfindByCondition parameterTypeuser resultTypeuserselect * from user where 11whereif testid!0and id#{id}/ifif testusername!nulland username#{username}/ifif testpassword!passwordand password#{password}/if/where/select/mapper4. 动态sql之foreach
假如我们想查询id1或者id2或者id3…的 select * from user where id1 or id2 or id3 select * from user where id in(1, 2, 3) 这时候我们可能就需要用到循环 在XML文件中
select idfindByIds parameterTypelist resultTypeuserselect * from userwhereforeach collectionlist openid in( close) itemid separator,#{id}/foreach/where
/select其中foreach中的 collection指定遍历的类型如果是列表就写list如果是数组就写array open写的是开始的内容 close写的是结束的内容 item里边是循环中当前的元素 seperator写的是分隔符 在接口里需要添加上该方法
public ListUser findByIds(ListInteger ids);在测试类中
ListInteger ids new ArrayListInteger();
ids.add(0);
ids.add(1);
ListUser userList userMapper.findByIds(ids);这样结果为id0和id1的数据
5. sql片段抽取
Sql中可以将重复的sql提取出来使用时通过include引用即可最终达到Sql重用的目的 例如我们原本有以下文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.demo.dao.UserMapperselect idfindByCondition parameterTypeuser resultTypeuserselect * from userwhereif testid!0and id#{id}/ifif testusername!nulland username#{username}/ifif testpassword!passwordand password#{password}/if/where/selectselect idfindByIds parameterTypelist resultTypeuserselect * from userwhereforeach collectionlist openid in( close) itemid separator,#{id}/foreach/where/select/mapper其中两个语句都包含select * from user 那么我们就使用以下语法抽取sql片段简化编写sql标签
sql idselectUserselect * from user/sql然后使用inclue标签进行引用refid中写对应sql片段的id
include refidselectUser/include所以以上代码可改写为
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.example.demo.dao.UserMappersql idselectUserselect * from user/sqlselect idfindByCondition parameterTypeuser resultTypeuserinclude refidselectUser/includewhereif testid!0and id#{id}/ifif testusername!nulland username#{username}/ifif testpassword!passwordand password#{password}/if/where/selectselect idfindByIds parameterTypelist resultTypeuserinclude refidselectUser/includewhereforeach collectionlist openid in( close) itemid separator,#{id}/foreach/where/select/mapper