在编程中,有可能遇到我们的实体类或者数据库中表的字段或参数过多的情况,那这时候用Map传参是比较理想的选择。
Map的特性是键值对应的,只要确定了一个键key,那么值value可以是任何的数据,这样就可以在map内存中存入任何数据。
下面例子演示在mybatis中结合Map实现基本的增删改查
数据库表(fruits)结构:
实体类:
package com.pojo.pp1; import java.math.BigDecimal; /** * 简述: *创建实体 * @author:LiYansheng * @date:2021/07/20 22:29 * @version: */ public class fruits { private int id; private String name; private BigDecimal price; public fruits(int id, String name, BigDecimal price) { this.id = id; this.name = name; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } @Override public String toString() { return "fruits{" + "id=" + id + ", name="" + name + """ + ", price=" + price + "}"; } }
Dao接口:
package com.pojo.Dao; import com.pojo.pp1.fruits; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; /** * 简述: * * @author:LiYansheng * @date:2021/07/20 22:31 * @version: */ public interface fruitsDao { /*查询全部信息*/ List<fruits> getfruitsList(); /*通过ID查询信息*/ fruits getfruitsById(@Param("map") Map<String, Object> map); /*添加信息*/ int addfruits(@Param("map") Map<String, Object> map); /*删除信息*/ int deletefruits(@Param("map") Map<String, Object> map); /*修改信息*/ int updatefruits(@Param("map") Map<String, Object> map); }
Dao接口.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.pojo.Dao.fruitsDao"> <select id="getfruitsList" resultType="fruits"> select id,name,price from fruits </select> <select id="getfruitsById" parameterType="Map" resultType="fruits"> select id,name,price from fruits where id=#{map.id}; </select> <insert id="addfruits" parameterType="Map" > insert into fruits(id,name,price) values (#{map.id},#{map.name},#{map.price}); </insert> <delete id="deletefruits" parameterType="Map" > delete from fruits where id=#{map.id}; </delete> <update id="updatefruits" parameterType="Map" > update fruits set name=#{map.name},price=#{map.price} where id=#{map.id} </update> </mapper>
在Test类中测试运行:
package com.pojo.Dao; import com.pojo.pp1.fruits; import com.utils.mybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.math.BigDecimal; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 简述: * * @author:LiYansheng * @date:2021/07/20 22:39 * @version: */ public class fruitsDaoTest { @Test public void getfruitsListTest() { SqlSession sqlSession = mybatisUtils.getSqlSession(); fruitsDao fruitsDao = sqlSession.getMapper(com.pojo.Dao.fruitsDao.class); List<fruits> fruitsList = fruitsDao.getfruitsList(); for (fruits f : fruitsList) { System.out.println(f); } sqlSession.close(); } @Test public void getfruitsByIdTest() { SqlSession sqlSession = mybatisUtils.getSqlSession(); fruitsDao fruitsDao = sqlSession.getMapper(com.pojo.Dao.fruitsDao.class); Map<String, Object> map = new HashMap<>(); map.put("id", 3); fruits f = fruitsDao.getfruitsById(map); System.out.println(f); sqlSession.close(); } @Test public void addfruits() { SqlSession sqlSession = mybatisUtils.getSqlSession(); fruitsDao fruitsDao = sqlSession.getMapper(com.pojo.Dao.fruitsDao.class); Map<String, Object> map = new HashMap<>(); map.put("id", 0); map.put("name", "watermelon"); BigDecimal bigDecimal= BigDecimal.valueOf(2); map.put("price", bigDecimal); int a = fruitsDao.addfruits(map); if (a > 0) { System.out.println("添加信息ok"); } sqlSession.commit(); sqlSession.close(); } @Test public void deletefruits() { SqlSession sqlSession = mybatisUtils.getSqlSession(); fruitsDao fruitsDao = sqlSession.getMapper(com.pojo.Dao.fruitsDao.class); Map<String, Object> map = new HashMap<>(); map.put("id", 3); int a = fruitsDao.deletefruits(map); if (a > 0) { System.out.println("删除信息ok"); } sqlSession.commit(); sqlSession.close(); } @Test public void updatefruits() { SqlSession sqlSession = mybatisUtils.getSqlSession(); fruitsDao fruitsDao = sqlSession.getMapper(com.pojo.Dao.fruitsDao.class); Map<String, Object> map = new HashMap<>(); map.put("id", 1); BigDecimal bigDecimal = BigDecimal.valueOf(5); map.put("name", "Big_apple"); map.put("price", bigDecimal); int a = fruitsDao.updatefruits(map); if (a > 0) { System.out.println("修改信息ok"); } sqlSession.commit(); sqlSession.close(); } }
测试结果,全部都可以通过
需要注意的地方:
标记序号的地方是两种写类型的方式
第二种方式需要在项目配置中resources下mybatis-config.xml的文件中加入以下的类型别名:
<typeAliases> <typeAlias type="java.util.Map" alias="Map"/> </typeAliases>
推荐用第二种方式哦
到此这篇关于Mybatis中使用万能的Map传参实现的文章就介绍到这了,更多相关Mybatis Map传参内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_44107140/article/details/118947185