• mybatis 动态sql



    public interface StudentMapper { // if 条件 public List<Student> searchStudents(Map<String,Object> map); // choose,when 和 otherwise 条件 public List<Student> searchStudents2(Map<String,Object> map); //where 条件 public List<Student> searchStudents3(Map<String,Object> map); //trim 条件 public List<Student> searchStudents4(Map<String,Object> map); //foreach 循环 public List<Student> searchStudents5(Map<String,Object> map); //set 条件 public int updateStudent(Student student); }

      

    <?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.java1234.mappers.StudentMapper">
    
    	<resultMap type="Student" id="StudentResult">
    		<id property="id" column="id"/>
    		<result property="name" column="name"/>
    		<result property="age" column="age"/>
    	</resultMap>
    	
    	<select id="searchStudents" parameterType="Map" resultMap="StudentResult">
    		select * from t_student 
    		 where gradeId=#{gradeId}
    		 <if test="name!=null">
    		 	and name like #{name}
    		 </if>
    		 <if test="age!=nulll">
    		 	and age=#{age}
    		 </if>
    	</select>
    	
    	<select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
    		select * from t_student 
    		 <choose>
    		 	<when test="searchBy=='gradeId'">
    		 		where gradeId=#{gradeId}
    		 	</when>
    		 	<when test="searchBy=='name'">
    		 		where name like #{name}
    		 	</when>
    		 	<otherwise>
    		 		where age=#{age}
    		 	</otherwise>
    		 </choose>
    		 
    	</select>
    	
    	<select id="searchStudents3" parameterType="Map" resultMap="StudentResult">
    		select * from t_student 
    		 <where>
    			 <if test="gradeId!=null">
    			 	gradeId=#{gradeId}
    			 </if>
    			 <if test="name!=null">
    			 	and name like #{name}
    			 </if>
    			 <if test="age!=nulll">
    			 	and age=#{age}
    			 </if>
    		 </where>
    	</select>
    	
    	<select id="searchStudents4" parameterType="Map" resultMap="StudentResult">
    		select * from t_student 
    		 <trim prefix="where" prefixOverrides="and|or">
    			 <if test="gradeId!=null">
    			 	gradeId=#{gradeId}
    			 </if>
    			 <if test="name!=null">
    			 	and name like #{name}
    			 </if>
    			 <if test="age!=nulll">
    			 	and age=#{age}
    			 </if>
    		 </trim>
    	</select>
    	
    	<select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
    		select * from t_student 
    		 <if test="gradeIds!=null">
    		 	<where>
    		 		gradeId in 
    		 		<foreach item="gradeId" collection="gradeIds" open="(" separator="," close=")">
    		 		 #{gradeId}
    		 		</foreach>
    		 	</where>
    		 </if>
    	</select>
    	
    	<update id="updateStudent" parameterType="Student">
    		update t_student
    		<set>
    		 <if test="name!=null">
    		 	name=#{name},
    		 </if>
    		 <if test="age!=null">
    		 	age=#{age},
    		 </if>
    		</set>
    		where id=#{id}
    	</update>
    </mapper> 
    

      

      

     

  • 相关阅读:
    剑指 Offer 25. 合并两个排序的链表
    53. 最大子序和 动态规划
    121. 买卖股票的最佳时机
    20. 有效的括号
    centos7 常用操作
    树莓派
    golang 学习笔记
    并发 线程 进程
    连接内网问题
    Lamp 高并发优化
  • 原文地址:https://www.cnblogs.com/nidegui/p/11084022.html
Copyright © 2020-2023  润新知