基于mybatis 动态sql collection使用
一.背景:今天隔壁菜鸡接受前台传过来的集合的时候,是在service层中把他拆开成单个对象,然后一个个传进mapper层执行,看的我真的头痛。问他为什么不在mapper层实现,他说不会,教了他之后记下来这样下次他再在service层实现这个功能就要干他了!二.工具准备,我这里没有使用springboot集成,而是直接用的mybatis,这样的话底层实现会比较清楚,如果是springboot的话可以
一.背景:今天隔壁菜鸡接受前台传过来的集合的时候,是在service层中把他拆开成单个对象,然后一个个传进mapper层执行,看的我真的头痛。问他为什么不在mapper层实现,他说不会,教了他之后记下来这样下次他再在service层实现这个功能就要干他了!
二.工具准备,我这里没有使用springboot集成,而是直接用的mybatis,这样的话底层实现会比较清楚,如果是springboot的话可以取其精华来看
首先是mybatis的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"></properties>//引入数据库配置文件
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/itsource/mapper/productMapper.xml"/>
</mappers>
</configuration>
数据库配置文件jdbc.properties
username=你的用户名
password=你的密码
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///数据库名字?useUnicode=true&characterEncoding=utf8
domain和mapper
@Data
public class Product {//product类
private Long id;
private String name;
private BigDecimal salePrice;
public Product(){};
public Product(String name,BigDecimal salePrice){
this.name=name;
this.salePrice=salePrice;
};
public Product(Long id,String name,BigDecimal salePrice){
this.id=id;
this.name=name;
this.salePrice=salePrice;
};
}
public interface ProductMapper {
/**
* 批量增加与删除
*/
void batchSave(List<Product> products);
void batchRemove(List<Long> ids);
}
创建session的工具类,用来通过反射生成mapper对象
public class MybatisUtils {
private static SqlSessionFactory sessionFactory ;
static{
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//根据io流创建SqlSessionFactory对象
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession openSession(){
if (sessionFactory != null) {
return sessionFactory.openSession();
}
return null;
}
}
三,mapper.xml具体实现//关键
在mybatis中,咱们可以传集合与数组过来,mybatis会自动把它封装进一个Map对象
如果传的是集合,Map对应的key就是list
如果传的是数组,Map对应的key就是array
需注意的是此Map的结构,比如如果传进来的是List集合 那么结构为Map<list,你传进来的List集合>
这里的list只是为了后面取出,和传进来的集合元素类型,集合名之类的没有关系
常用的关键词
1.parameterType中接收的是集合类型 一般不写
2.collection="list" 接收集合的话,默认就是使用collection来接收
这里取出默认封装的Map集合,通过key的值来取,一般只有list和arrays两种
3.separator="," 每遍历一次后的分隔符
4.item="p" 每次遍历的这个对象别名,可以修改
5.open:拼接sql以什么开始
6.close:拼接sql以什么结束
<?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="你的mapper地址">
<insert id="batchSave">
INSERT INTO t_product (name,salePrice) VALUES
<foreach collection="list" separator="," item="p">
(#{p.name},#{p.salePrice})
</foreach>
</insert>
<delete id="batchRemove">
DELETE FROM t_product WHERE id IN
<foreach collection="list" separator="," open="(" close=")" item="id">
#{id}
</foreach>
</delete>
</mapper>
最后进行测试
public class MybatisTest {
@Test
public void removeTest(){//批量删除的测试
//获取到会话对象
SqlSession session = MybatisUtils.openSession();
//拿到mapper对象,可以做相应的操作
ProductMapper mapper = session.getMapper(ProductMapper.class);
//准备数值
List<Long> ids = Arrays.asList(167L, 166L);
//执行mapper
mapper.batchRemove(ids);
//提交事务
session.commit();
}
@Test
public void saveTest(){//批量增加的测试
//获取到会话对象
SqlSession session = MybatisUtils.openSession();
//拿到mapper对象,可以做相应的操作
ProductMapper mapper = session.getMapper(ProductMapper.class);
//准备数据
List<Product> products = Arrays.asList(
new Product("洗澡贵宾1",new BigDecimal(3)),
new Product("洗澡贵宾2",new BigDecimal(11))
);
//执行mapper
mapper.batchSave(products);
//提交事务
session.commit();
}
}
一个小tips,以上仅适用于单独传一个list或者array的情况,如果是一个map封装了多个参数,其中有一个是list或array,那么就用对应的key来取值。

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)