首页 文章 精选 留言 我的

精选列表

搜索[后台管理系统],共10000篇文章
优秀的个人博客,低调大师

后台(41)——MyBatis的动态SQL

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl MyBatis可利用动态SQL极大的简化SQL语句的拼接。 在此介绍几种MyBatis中常用的动态SQL where和if 还记得我们之前有一个查询学生的例子么? <select id="findStudentList" parameterType="cn.com.StudentQueryVO" resultType="cn.com.StudentCustom"> SELECT * FROM student WHERE gender=#{studentCustom.gender} and name like '%${studentCustom.name}%' </select> 猛地一看这个SQL没啥问题,但是仔细一想又不对,studentCustom有没有可能为null呢?studentCustom.gender和studentCustom.name有没有可能为null呢?当然是有的,那么我们可以怎么处理一下呢?此时就可以用<where>和<if>,请看: <select id="findStudentList" parameterType="cn.com.StudentQueryVO" resultType="cn.com.StudentCustom"> SELECT * FROM student <where> <if test="studentCustom!=null"> <if test="studentCustom.gender!=null"> and gender=#{studentCustom.gender} </if> <if test="studentCustom.name!=null"> and name like '%${studentCustom.name}%' </if> </if> </where> </select> 在此利用标签<where>和<if>对应了某些查询条件为null的情况。有人或许有疑问,如果两个查询条件都满足的话在这个sql中岂不是有两个and了?甭担心,这种情形下MyBatis会自动删除掉掉第一个and sql 其实,我们还可以把刚才这个例子继续优化一下利用<sql>提取出<if>作为sql片段从而提高代码的复用性 <sql id="findStudentList_if"> <if test="studentCustom!=null"> <if test="studentCustom.gender!=null"> and gender=#{studentCustom.gender} </if> <if test="studentCustom.name!=null"> and name like '%${studentCustom.name}%' </if> </if> </sql> 然后在查询语句中引用该sql片段即可 <select id="findStudentList" parameterType="cn.com.StudentQueryVO" resultType="cn.com.StudentCustom"> SELECT * FROM student <where> <include refid="findStudentList_if"></include> </where> </select> foreach 来看一种需求:查询出id为2、4、7的学生信息,我们可以这么写sql SELECT * FROM student WHERE (id=2 OR id=4 OR id=7) 那么这个sql在MyBatis里又该怎么写呢? 在此可采用<foreach>标签来拼装这个条件(id=2 OR id=4 OR id=7) 首先,我们在StudentQueryVO中定义一个字段idsList用于传递这几个id值 /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.util.List; //自定义的包装类型的pojo public class StudentQueryVO { private List<Integer> idsList; //用户查询条件 private StudentCustom studentCustom; public StudentCustom getStudentCustom() { return studentCustom; } public void setStudentCustom(StudentCustom studentCustom) { this.studentCustom = studentCustom; } public List<Integer> getIdsList() { return idsList; } public void setIdsList(List<Integer> idsList) { this.idsList = idsList; } //其他查询条件,比如教师,课程,学校等等 } 再来看mapper.xml <sql id="findStudentList_ids_if"> <if test="idsList!=null"> <foreach collection="idsList" item="student_id" open="(" close=")" separator="OR"> id=#{student_id} </foreach> </if> </sql> <select id="findStudentList" parameterType="cn.com.StudentQueryVO" resultType="cn.com.StudentCustom"> SELECT * FROM student <where> <include refid="findStudentList_ids_if"></include> </where> </select> 重点来看这个<foreach>标签 collection表示pojo中的集合 item表示遍历时集合中每个元素 open表示组拼后的语句的开头 close表示组拼后的语句的结尾 separator表示每个部门的连接符 最后来看测试代码 @Test public void findStudentList() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); StudentQueryVO studentQueryVO=new StudentQueryVO(); List<Integer> idsList=new ArrayList<Integer>(); idsList.add(2); idsList.add(4); idsList.add(7); studentQueryVO.setIdsList(idsList); List<StudentCustom> studentList = studentMapper.findStudentList(studentQueryVO); for (int i = 0; i <studentList.size(); i++) { StudentCustom sc = studentList.get(i); System.out.println(sc); } sqlSession.commit(); sqlSession.close(); } 其实,这些动态SQL并不难,也不需要死记硬背。但是要把握住总的原则和目标:利用动态SQL拼装成原始的sql语句。在实际开发时也可以先写出原始sql语句再写到mapper.xml中

优秀的个人博客,低调大师

后台(39)——MyBatis输入映射parameterType

探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 我们知道:MyBatis通过parameterType对sql的输入参数进行定义,参数的类型可以是:基本类型、HashMap、pojo。在此分别介绍为parameterType传入三种类型的不同处理方式。 基本类型 其实,从这个MyBatis学习系列开始,我们已经多次为parameterType传入基本类型的参数,比如int 、long等。故,在此不再赘述,请参见前几篇博客的示例。 HashMap 首先来看mapper.mxl中的sql语句 <select id="findStudentByHashMap" parameterType="hashmap" resultType="cn.com.Student"> SELECT * FROM student WHERE id=#{id} and name like '%${name}%' </select> 嗯哼,看到没有:我们为parameterType指定的输入类型是hashmap。在sql语句中从hashmap中取出id和name作为查询条件 接下来瞅瞅mapper.java中的定义 public List<Student> findStudentByHashMap(HashMap<String, Object> hashMap); 最后,再来看看测试代码: @Test public void findStudentByHashMap() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); HashMap<String, Object> hashMap=new HashMap<String, Object>(); hashMap.put("id", 7); hashMap.put("name", "木"); List<Student> studentList = studentMapper.findStudentByHashMap(hashMap); for (int i = 0; i <studentList.size(); i++) { Student student = studentList.get(i); System.out.println(student); } sqlSession.commit(); sqlSession.close(); } 在此,创建一个HashMap且指定两个key:id和name并为它们赋值;然后执行查询即可。 pojo 有时候,我们需要执行一些复杂的查询,比如:查询的条件不仅包括学生查询条件还包括其它的查询条件(比如:课程,教师,学校等)。此时,可以使用自定义pojo传递输入参数。 首先,定义一个Student的扩展类 /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; //Student的扩展类 public class StudentCustom extends Student{ } 再自定义包装类型的pojo /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; //自定义的包装类型的pojo public class StudentQueryVO { //用户查询条件 private StudentCustom studentCustom; public StudentCustom getStudentCustom() { return studentCustom; } public void setStudentCustom(StudentCustom studentCustom) { this.studentCustom = studentCustom; } //其他查询条件,比如教师,课程,学校等等 } 在该pojo中不仅包括与学生相关的查询条件,还有与教师,课程,学校有关的查询条件。 接下来请看mapper.xml <select id="findStudentList" parameterType="cn.com.StudentQueryVO" resultType="cn.com.StudentCustom"> SELECT * FROM student WHERE gender=#{studentCustom.gender} and name like '%${studentCustom.name}%' </select> 嗯哼,看到了吧:我们将自定义的包装类型的pojo作为输入参数设置给parameterType;然后取出输入参数StudentQueryVO中的studentCustom的gender和name作为条件查询。 再来瞅瞅mapper.java中的定义 public List<StudentCustom> findStudentList(StudentQueryVO studentQueryVO); 最后,请看测试代码: @Test public void findStudentList() throws IOException { SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); StudentQueryVO studentQueryVO=new StudentQueryVO(); StudentCustom studentCustom=new StudentCustom(); studentCustom.setGender("female"); studentCustom.setName("木"); studentQueryVO.setStudentCustom(studentCustom); List<StudentCustom> studentList = studentMapper.findStudentList(studentQueryVO); for (int i = 0; i <studentList.size(); i++) { StudentCustom sc = studentList.get(i); System.out.println(sc); } sqlSession.commit(); sqlSession.close(); }

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册