Mybatis如何获取参数值和查询功能 | 您所在的位置:网站首页 › mybatis查询字段 › Mybatis如何获取参数值和查询功能 |
Mybatis如何获取参数值和查询功能
发布时间:2023-03-21 10:18:48
来源:亿速云
阅读:69
作者:iii
栏目:开发技术
这篇文章主要介绍了Mybatis如何获取参数值和查询功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatis如何获取参数值和查询功能文章都会有所收获,下面我们一起来看看吧。 一、MyBatis的增删改查1.1、新增 insert into t_user values(null,'admin','123456',23,'男') 1.2、删除 delete from t_user where id = 1 1.3、修改 update t_user set username='ybc',password='123' where id = 6 1.4、查询一个实体类对象 select * from t_user where id = 2 1.5、查询list集合 select * from t_user注意: 查询的标签select必须设置属性resultType或resultMap,用于设置实体类和数据库表的映射关系 resultType:自动映射,用于属性名和表中字段名一致的情况 resultMap:自定义映射,用于一对多或多对一或字段名和属性名不一致的情况 二、MyBatis获取参数值的两种方式MyBatis获取参数值的两种方式:${} 和 #{} ${} 的本质就是字符串拼接,#{} 的本质就是占位符赋值 ${} 使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单 引号; #{} 使用占位符赋值的方式拼接sql,此时为字符串类型或日期类型的字段进行赋值时, 可以自动添加单引号 2.1、单个字面量类型的参数若 mapper 接口中的方法参数为单个的字面量类型 此时可以使用 ${} 和 #{} 以任意的名称获取参数的值,注意 ${} 需要手动加单引号 select * from t_user where username = #{username} 2.2、多个字面量类型的参数若mapper接口中的方法参数为多个时 此时MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1...为键,以参数为值、以 param1,param2...为键,以参数为值; 因此只需要通过 ${} 和 #{} 访问map集合的键就可以获取相 对应的值,注意 ${} 需要手动加单引号 select * from t_user where username = #{arg0} and password = #{arg1} 2.3、map集合类型的参数若mapper接口中的方法需要的参数为多个时,此时可以手动创建map集合,将这些数据放在 map中 只需要通过 ${} 和 #{} 访问map集合的键就可以获取相对应的值,注意 ${} 需要手动加单引号 select * from t_user where username = #{username} and password = #{password} 2.4、实体类类型的参数若mapper接口中的方法参数为实体类对象时 此时可以使用 ${} 和 #{} ,通过访问实体类对象中的属性名获取属性值,注意 ${} 需要手动加单引号 insert into t_user values(null,#{username},#{password},#{age},#{gender},#{email}) 2.5、使用@Param标识参数可以通过 @Param 注解标识mapper接口中的方法参数 此时,会将这些参数放在map集合中,以@Param注解的value属性值为键,以参数为值;以 param1,param2...为键,以参数为值; 只需要通过 ${} 和 #{} 访问map集合的键就可以获取相对应 的值, 注意 ${} 需要手动加单引号 select * from t_user where username = #{username} and password = #{password} 三、MyBatis的各种查询功能3.1、查询一个实体类对象package com.ssm.mybatis.mapper; import com.ssm.mybatis.pojo.User; import org.apache.ibatis.annotations.Param; import java.util.List; /** * Author:wy * Date:2023/3/18 */ public interface SelectMapper { /** * 根据用户id查询用户信息 * @param id * @return */ User getUserById(@Param("id") int id); } select * from t_user where id = #{id} @Test public void testGetUserById() { SqlSession sqlSession = SqlSessionUtil.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); User user = mapper.getUserById(1); System.out.println(user); }3.2、查询一个list集合 /** * 查询所有用户信息 * @return */ List getUserList(); select * from t_user @Test public void testGetUserList() { SqlSession sqlSession = SqlSessionUtil.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); List userList = mapper.getUserList(); for (User user : userList) { System.out.println(user); } }当查询的数据为多条时,不能使用实体类作为返回值,否则会抛出异常TooManyResultsException; 但是若查询的数据只有一条,可以使用实体类或集合作为返回值 3.3、查询单个数据 /** * 查询用户的总记录数 * @return */ Integer getCount(); select count(*) from t_user @Test public void testGetCount() { SqlSession sqlSession = SqlSessionUtil.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); Integer count = mapper.getCount(); System.out.println("用户总数=" + count); }3.4、查询一条数据为map集合 /** * 根据用户id查询用户信息为map集合 * @param id * @return */ Map getUserByIdToMap(@Param("id") Integer id); select * from t_user where id = #{id} @Test public void testGetUserByIdToMap() { SqlSession sqlSession = SqlSessionUtil.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); Map map = mapper.getUserByIdToMap(1); System.out.println(map); //结果:{password=123456, gender=男, id=1, age=21, [email protected], username=张三} }3.5、查询多条数据为map集合方式一 /** * 查询所有用户信息为map集合 * @return * 将表中的数据以map集合的方式查询,一条数据对应一个map;若有多条数据,就会产生多个map集合 此时可以将这些map放在一个list集合中获取 */ @MapKey("id") List getAllUserToMap(); select * from t_user @Test public void testGetAllUserToMap() { SqlSession sqlSession = SqlSessionUtil.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); List allUserToMap = mapper.getAllUserToMap(); for (Map map : allUserToMap) { System.out.println(map); } }方式二 /** * 查询所有用户信息为map集合 * @return * 可以将每条数据转换的map集合放在一个大的map中, * 但是必须要通过@Mapkey注解将查询的某个字段的值作为大的map的键 */ @MapKey("id") Map getAllUserToMap(); select * from t_user @Test public void testGetAllUserToMap() { SqlSession sqlSession = SqlSessionUtil.getSqlSession(); SelectMapper mapper = sqlSession.getMapper(SelectMapper.class); Map map = mapper.getAllUserToMap(); System.out.println(map); //{ // 1={password=123456, gender=男, id=1, age=21, [email protected], username=张三}, // 2={password=123456, gender=女, id=2, age=19, [email protected], username=老六} // } }关于“Mybatis如何获取参数值和查询功能”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Mybatis如何获取参数值和查询功能”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。 推荐阅读: MyBatis之动态SQL语句的示例分析 如何实现Javaweb mybatis接口开发免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:[email protected]进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。 mybatis 上一篇新闻:linux下有哪些ftp命令 猜你喜欢 PHP获取2个时间点之间的年月 一个胖子的paython之路(1) 解决Flink反压的方法有哪些? CAD显示图层(网页版) 「Protocol_Buffer」之基本使用与格式定义 Ribbon如何负载均衡地消费microservice-provider-user composer出现404错误 如何理解Angular服务 Angular 文档中链接的修改路径 如何在java中调用kotlin代码 |
CopyRight 2018-2019 实验室设备网 版权所有 |