循环测试配【mapper.xml】置文件编写
我这里便利的是【uuid()】的id,故而是字符串。
<?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.item.mapper.ProductMapper"> <select id="GetInfo" resultType="Product"> select * from product where id in <foreach collection="array" item="id" index="i" open="(" close=")" separator=","> "${id}" </foreach> </select> </mapper> DAO层: package com.item.dao; import com.item.common.JDBC; import com.item.mapper.ProductMapper; import com.item.model.Product; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; public class ProduceDAO { /** * 多个查询 * @param array (字符串id数组) * @return */ public static List<Product> GetInfo(String []array) { SqlSessionFactory factory = JDBC.GetConn(); SqlSession session = factory.openSession(); ProductMapper db = session.getMapper(ProductMapper.class); List<Product> list = db.GetInfo(array); session.close(); return list; } }
参数传递:
package com.item.mapper; import com.item.model.Product; import org.apache.ibatis.annotations.Param; import java.util.List; public interface ProductMapper { List<Product> GetInfo(@Param("array") String[] array); }
测试类:
package com.item.action; import com.item.dao.ProduceDAO; import com.item.model.Product; import java.util.List; public class Action { public static void main(String[] args){ String [] array={"b383581fd20211ec84b500e070bfdb54", "b3839547d20211ec84b500e070bfdb54", "b383d49dd20211ec84b500e070bfdb54", "b384180cd20211ec84b500e070bfdb54", "f6715eb2d20111ec84b500e070bfdb54"}; List<Product> list = ProduceDAO.GetInfo(array); for (Product p : list) { System.out.println("编号"+p.getId()); System.out.println("创建时间"+p.getCreateDate()); System.out.println("修改时间"+p.getModifyDate()); System.out.println("产品名称"+p.getProductName()); System.out.println("产品标题"+p.getProductTitle()); System.out.println("产品价格"+p.getProductPrice()); System.out.println("产品数量"+p.getProductCount()); System.out.println("品牌类型"+p.getProductType()); System.out.println("重量"+p.getProductWeight()); System.out.println("状态"+(p.getProductStatus()==1?"上架":"下架")); } } }
查询结果:
循环的用处可以很多可以进行批量添加、修改、删除等处。