【Mybatis 框架(自学)】Day02--2022/3/11

Map和模糊查询拓展

万能Map

​ 假设,我们实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!

//(Dao/Mapper)接口实现类

public interface StudentMapper(){
    
    int addStudent2(Map<String,Object> map);
    
}

<!--Mapper映射-->
<insert id="addStudent2" parametereType="map" useGeneratedKeys="true" KeyProperty="id">
	insert into 表明 (name,gender,phone) Values (#{自定义},#{自定义},#{自定义})
</insert>
//Junit单元测试
public class StudentDaoTest() extends MybatisUtils{
    //导入mybatis工具类
 	MybatisUtils mybatisUtils = new MybatisUtils();
    //将工厂对象传入给sqlSesion
    Sqlsession sqlSession = mybatisUtils.getSqlSession();
    //获取接口实现类
    StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class)
        
    public void addStudent2() throws Exception{
        try{
            Map<String,Object> map = new HashMap<>();
            map.put("自定义","内容");
            map.put("自定义","内容");
            map.put("自定义","内容");
            studentMapper.addStudent2(map);
            commitSqlSession(sqlSession)
       }catch (Exception e){
           e.printStackTrace();
       }finally{
           closeSqlSession(sqlSession)
        }
    }
    
}

参数传递:

Map传递参数,直接在sql中取出key即可 【parameterType="map"】

对象传递参数,直接在sql中取出对象属性即可 【parameterType="Student"】

只有一个基本类型的情况下,可以直接在sql中去到

多个参数时用Map,或者注解

posted @   VVS,冷漠的心  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

囚鸟该如何超越今生?

点击右上角即可分享
微信分享提示