【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,或者注解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?