【MyBatis】SQL动态操作
MybatisUtils.java工具类可以访问获取:【MyBatis】测试链接数据库增删改查 - 小鼻涕孩 - 博客园 (cnblogs.com)
CustomerMapper.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.xiaobiti.pojo.Customer"> 6 <!-- 查询 --> 7 <select id="findCustomerByNameAndJobs" parameterType="com.xiaobiti.pojo.Customer" resultType="com.xiaobiti.pojo.Customer"> 8 <!-- 使用<trim>元素避免语句冲突 prefixOverrides语句前缀,以下的语句前缀是"and" --> 9 SELECT * from customer 10 <trim prefix="where" prefixOverrides="and"> 11 <choose> 12 <when test="name != null and name != ''"> 13 and name like concat('%',#{name},'%') 14 </when> 15 <when test="jobs != null and jobs != ''"> 16 and jobs = #{jobs} 17 </when> 18 <otherwise> 19 and phone != '' 20 </otherwise> 21 </choose> 22 </trim> 23 24 <!-- 使用<where>元素避免查询语句冲突 --> 25 <!-- SELECT * from customer--> 26 <!-- <where>--> 27 <!-- <choose>--> 28 <!-- <when test="name != null and name != ''">--> 29 <!-- and name like concat('%',#{name},'%')--> 30 <!-- </when>--> 31 <!-- <when test="jobs != null and jobs != ''">--> 32 <!-- and jobs = #{jobs}--> 33 <!-- </when>--> 34 <!-- <otherwise>--> 35 <!-- and phone != ''--> 36 <!-- </otherwise>--> 37 <!-- </choose>--> 38 <!-- </where>--> 39 40 <!-- SELECT * from customer where 1=1 --> 41 <!-- <if test="name != null and name != ''">--> 42 <!-- and name like concat('%',#{name},'%')--> 43 <!-- </if>--> 44 <!-- <if test="jobs != null and jobs != ''">--> 45 <!-- and jobs = #{jobs}--> 46 <!-- </if>--> 47 </select> 48 49 <!-- 更新 --> 50 <update id="edit" parameterType="com.xiaobiti.pojo.Customer"> 51 <!-- 使用<trim>元素避免语句冲突 suffixOverrides语句后缀,以下的语句后缀是"," --> 52 update customer 53 <trim prefix="set" suffixOverrides=","> 54 <if test="name != null and name != ''"> 55 name = #{name}, 56 </if> 57 <if test="jobs != null and jobs != ''"> 58 jobs = #{jobs}, 59 </if> 60 <if test="phone != null and phone != ''"> 61 phone = #{phone}, 62 </if> 63 </trim> 64 where id = #{id} 65 </update> 66 67 <!-- 多条ID查询 --> 68 <select id="findByArray" parameterType="java.util.ArrayList" resultType="com.xiaobiti.pojo.Customer"> 69 select * from customer where id in 70 <!-- 使用List对象 --> 71 <!-- <foreach collection="list" item="id" index="index" open="(" separator="," close=")" >--> 72 <!-- #{id}--> 73 <!-- </foreach>--> 74 75 <!-- 使用Map对象 --> 76 <foreach collection="id" item="roleMap" index="index" open="(" separator="," close=")"> 77 #{roleMap} 78 </foreach> 79 </select> 80 </mapper>
Customer.java
1 package com.xiaobiti.test; 2 3 import com.xiaobiti.pojo.Customer; 4 import com.xiaobiti.utils.MybatisUtils; 5 import org.apache.ibatis.session.SqlSession; 6 import org.junit.jupiter.api.Test; 7 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 public class MybatisTest { 14 @Test 15 //查询 16 public void findCustomerByNameAndJobs(){ 17 SqlSession session = MybatisUtils.getSession(); 18 Customer customer = new Customer(); 19 customer.setName("jo"); 20 customer.setJobs("海洋学家"); 21 List<Customer> list = session.selectList("findCustomerByNameAndJobs",customer); 22 for(Customer c:list){ 23 System.out.println(c); 24 } 25 } 26 27 @Test 28 //更新 29 public void edit(){ 30 SqlSession session = MybatisUtils.getSession(); 31 Customer customer = new Customer(); 32 customer.setId(4); 33 customer.setName("jojo"); 34 customer.setJobs("海洋学家"); 35 customer.setPhone("123"); 36 session.update("edit",customer); 37 session.commit(); 38 } 39 40 @Test 41 //多条ID查询 42 public void findByArray(){ 43 SqlSession session = MybatisUtils.getSession(); 44 //Integer[] arr = {1,2,3}; 45 List<Integer> ids = new ArrayList<Integer>(); 46 ids.add(1); 47 ids.add(2); 48 ids.add(3); 49 Map<String,Object> map = new HashMap<String, Object>(); 50 map.put("id",ids); 51 map.put("jobs","nurse"); 52 List<Customer> list = session.selectList("findByArray",map); 53 for(Customer c:list){ 54 System.out.println(c); 55 } 56 } 57 }

浙公网安备 33010602011771号