MyBatis的动态sql语句
1.if 条件
2.choose , when 和 otherwise条件
3.where 条件
where条件:1.自动加上where。2.如果where子句以and或者or开头,则自动删除第一个and或者or。。所以我们不需要自己加where
4.trim 条件
trim条件和where条件类似但是功能更强大:不仅可以替换掉子句开头的and或者or,还提供了加前缀和后缀的功能。
5.forEach循环
6.set 条件
set条件:自动加上set,自动去除最后一个逗号
下面示例代码:
1.mapper接口
package com.maya.mappers; import java.util.List; import java.util.Map; import com.maya.model.Student; public interface StudentMapper { public List<Student> saerchStudent(Map<String, Object> param);//if查询 public List<Student> saerchStudent2(Map<String, Object> param);//choose查询 public List<Student> searchStudent3(Map<String, Object> param);//where查询 public List<Student> searchStudent4(Map<String, Object> param);//trim查询 public List<Student> searchStudent5(Map<String, Object> param);//forEach查询,一般用在 in() public int updateStudent(Student student);//set动态修改 }
2.mapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.maya.mappers.StudentMapper"> <resultMap type="Student" id="StudentResult"> <id property="snumber" column="son" /> <result property="name" column="sname" /> <result property="sex" column="sex" /> </resultMap> <!-- if查询 --> <select id="saerchStudent" parameterType="Map" resultMap="StudentResult"> select * from student where 1=1 <if test="sno!=null"> and son=#{sno} </if> <if test="name!=null"> and sname like #{name} </if> <if test="sex!=null"> and sex=#{sex} </if> </select> <!-- choose查询 --> <select id="saerchStudent2" parameterType="Map" resultMap="StudentResult"> select * from student <choose> <when test="searchBy==‘son‘"> where son=#{sno} </when> <when test="searchBy==‘name‘"> where sname like #{name} </when> <otherwise> where 1=1 </otherwise> </choose> </select> <!-- where查询 --> <!-- where条件:1.自动加上where。2.如果where子句以and或者or开头,则自动删除第一个and或者or。。所以我们不需要自己加where --> <select id="searchStudent3" parameterType="Map" resultMap="StudentResult"> select * from student <where> <if test="sno!=null"><!-- 第一个也可以加上and,不过纯属多此一举!! --> son=#{sno} </if> <if test="name!=null"> and sname like #{name} </if> <if test="sex!=null"> and sex=#{sex} </if> </where> </select> <!-- trim查询 --> <!-- trim条件和where条件类似但是功能更强大:不仅可以替换掉子句开头的and或者or,还提供了加前缀和后缀的功能。 1.前缀:prefix="where",是一定要加上的 2.prefixOverrides="and/or",替换掉第一个and或者or 3.后缀:suffix="",一般sql语句根本用不到,或许很复杂的sql才会用到吧 --> <select id="searchStudent4" parameterType="Map" resultMap="StudentResult"> select * from student <trim prefix="where" prefixOverrides="and|or" ><!-- 这样trim的就完成了where的功能,where开头,替换子句的第一个and或者where --> <if test="sno!=null"> son=#{sno} </if> <if test="name!=null"> and sname like #{name} </if> <if test="sex!=null"> and sex=#{sex} </if> </trim> </select> <!-- forEach查询 --> <!-- 例如in查询时 1.collection:接收的参数 2.item:自己定义的参数变量 3.open="(" separator="," close=")" : 这个一看就明白了吧。 4.mybatis可以接受一个集合,也可以接收一个数组,具体详情查帮助文档 --> <select id="searchStudent5" parameterType="Map" resultMap="StudentResult"> select * from student <where> <if test="snos!=null"> son in <foreach item="ids" collection="snos" open="(" separator="," close=")"> #{ids} </foreach> </if> <if test="name!=null"> and sname like #{name} </if> <if test="sex!=null"> and sex=#{sex} </if> </where> </select> <!-- set动态修改 --> <!-- 众所周知,修改的sql语句是 update 表名 set 字段名=值,字段名=值,字段名=值 where id=1; MyBatis的set动态sql, 1.自动帮助你加上set; 2.并且自动去除掉最后一个,号 注意:别忘记加逗号!!! --> <update id="updateStudent" parameterType="Student"> update student <set> <if test="name!=null"> sname=#{name},<!-- 千万不要忘记加逗号 --> </if> <if test="sex!=null"> sex=#{sex}, </if> </set> where son=#{snumber} </update> </mapper>
3.junit测试类
package com.maya.service; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.maya.mappers.StudentMapper; import com.maya.model.Student; import com.maya.util.MyBatisUtil; public class JunitTest { private static Logger logger=Logger.getLogger(JunitTest.class); private SqlSession sqlSession=null; private StudentMapper studentMapper=null; @Before public void setUp() throws Exception { sqlSession=MyBatisUtil.openSession(); studentMapper=sqlSession.getMapper(StudentMapper.class); } @After public void tearDown() throws Exception { sqlSession.close(); } @Test //if查询 public void testSerach() { logger.info("动态sql---if查询"); Map<String, Object> param=new HashMap<String, Object>(); //param.put("sno", "107"); param.put("name", "%军%"); param.put("sex", "男"); List<Student> list=studentMapper.saerchStudent(param); System.out.println(list); } @Test //choose查询 public void testSerach2() { logger.info("动态sql---choose查询"); Map<String, Object> param=new HashMap<String, Object>(); param.put("searchBy", "son"); param.put("sno", "107"); param.put("name", "%军%"); param.put("sex", "女"); List<Student> list=studentMapper.saerchStudent2(param); System.out.println(list); } @Test //where查询 public void testSerach3() { logger.info("动态sql---where查询"); Map<String, Object> param=new HashMap<String, Object>(); //param.put("sno", "107"); param.put("name", "%军%"); //param.put("sex", "男"); List<Student> list=studentMapper.searchStudent3(param); System.out.println(list); } @Test //trim查询 public void testSerach4(){ logger.info("动态sql---trim查询"); Map<String, Object> param=new HashMap<String, Object>(); //param.put("sno", "107"); //param.put("name", "%军%"); param.put("sex", "男"); List<Student> list=studentMapper.searchStudent4(param); System.out.println(list); } @Test //forEach查询 public void testSerach5(){ logger.info("动态sql---forEach查询"); List<String> snos=new ArrayList<String>(); snos.add("101"); snos.add("107"); snos.add("105"); snos.add("103"); Map<String, Object> param=new HashMap<String, Object>(); param.put("snos", snos); //param.put("sno", "107"); param.put("name", "%军%"); param.put("sex", "男"); List<Student> list=studentMapper.searchStudent5(param); System.out.println(list); } @Test //set动态修改 public void testUpdateStudent(){ logger.info("动态sql---set动态修改"); Student s=new Student(); s.setSnumber("108"); s.setName("增华"); s.setSex("男"); int i=studentMapper.updateStudent(s); System.out.println(i); sqlSession.commit(); } }
时间: 2024-09-28 20:14:07