MyBatis select标签
来源:互联网 发布:js new date 减一天 编辑:程序博客网 时间:2024/06/15 20:53
常用元素有:id、parameterType、resultType、resultMap,设置缓存用到flushCache、useCache
- id:配合Mapper的全限定名,联合成为一个唯一的标识,用户标识这条SQL。 parameterType:表示这条SQL接受的参数类型,可以是MyBatis系统定义或者自定义的别名,比如int、string、float等,也可以是全限定名,比如com.xx.xx.xx.pojo.User。
- resultType:表示这条SQL返回的结果类型,与parameterType一样,可以是系统定义的别名,也可以是类的全限定名。
- resultMap:它是映射器的引用,将执行强大的映射功能。我们可以使用resultType和resultMap中的一个,resultMap能提供自定义映射的机会
- flushCache:它的作用是在调用SQL后,是否要求MyBatis清空之前的查询本地缓存和二级缓存,默认 false。
- useCache:启动二级缓存的开关,是否要求MyBatis将此结果缓存 默认true。
传递多个参数
(1) 使用map接口传递参数–不推荐接口:
List<User> queryUserByMap(Map<String,Object> paramentMap);
service:
Map<String,Object> map=new HashMap<String,Object>(); map.put("userName", "张三"); map.put("mobile", "13800000000");
XML:
<select id="queryUserByMap" parameterType="map" resultType="com.bob.analyst.model.User" > select <include refid="Base_Column_List" /> from tbl_user where user_name like concat('%',#{userName},'%') and mobile like concat('%',#{mobile},'%') </select>
(2) 使用注解传递多个参数 —推荐
使用@Param(org.apache.ibatis.annotations.Param)
接口:
List<User> queryUserByAnnotation(@Param("userName") String username,@Param("mobile") String mobile);
XML:
<select id="queryUserByAnnotation"resultType="com.bob.analyst.model.User" > select <include refid="Base_Column_List" /> from tbl_user where user_name like concat('%',#{userName},'%') and mobile like concat('%',#{mobile},'%') </select>
备注:这里不需要使用parameterType来表明参数类型,让MyBatis自动检索。
(3) 通过Java Bean传递多个参数
接口:
List<User> queryUserByBean(User user);
XML:
<select id="queryUserByBean" parameterType="com.bob.analyst.model.User" resultType="com.bob.analyst.model.User" > select <include refid="Base_Column_List" /> from tbl_user where user_name like concat('%',#{userName},'%') and mobile like concat('%',#{mobile},'%') </select>
(4) 混合使用
接口:
List<User> queryUserByPage(@Param("user") User user,@Param("page") PageParam pageParam);
XML:
<select id="queryUserByPage" resultType="com.bob.analyst.model.User" > select <include refid="Base_Column_List" /> from tbl_user where user_name like concat('%',#{user.userName},'%') and mobile like concat('%',#{user.mobile},'%') limit #{page.start}, #{page.limit} </select>
(5) 使用resultMap作为映射结果集
<resultMap id="BaseResultMap" type="com.bob.analyst.model.User"> <id column="id" property="id" /> <result column="user_name" property="userName" /> <result column="mobile" property="mobile" /> <result column="password" property="password" /></resultMap>
resultMap 的 id 代表它的标识,type代表使用哪个类作为其映射类,可以使用别名或者全限定名.
id:代表resultMap的主键,而result代表其属性
property:代表POJO的属性名称,而column代表SQL的列名。
<resultMap id="BaseResultMap" type="com.bob.analyst.model.User"> <constructor><!-- 构造方法 --> <idArg/> <arg/> </constructor> <id/><!-- 主键 --> <result/><!-- 配置POJO到SQL列名的映射关系 --> <association property=""></association><!-- 一对一级联 --> <collection property=""></collection><!-- 一对多级联 --> <discriminator javaType=""><!-- 鉴别器 --> <case value=""></case><!-- 用于区分 --> </discriminator> </resultMap>
(6) 分页参数 RowBounds
接口:
List<User> queryUserByRowBounds(@Param("userName") String username,@Param("mobile") String mobile,RowBounds rowBounds);
service:
RowBounds rowBounds=new RowBounds(0,20);
XML:
<select id="queryUserByRowBounds" resultType="com.bob.analyst.model.User" > select <include refid="Base_Column_List" /> from tbl_user where user_name like concat('%',#{user.userName},'%') and mobile like concat('%',#{user.mobile},'%') </select>
备注:它是MyBatis的一个附加参数,MyBatis会自动识别它。
阅读全文
0 0
- MyBatis select标签
- 【MyBatis】MyBatis的学习笔记之<select>标签
- mybatis select标签中resultMap和resultType作用区别
- MyBatis的映射文件mapper的select标签
- Mybatis-select
- Mybatis-Select
- select标签
- select标签
- select标签
- select标签
- select标签
- MyBatis 中mapper.xml文件中<sql>标签,<select>标签是使用
- MyBatis 中mapper.xml文件中<sql>标签,<select>标签是使用
- 【select】选择标签select
- MyBatis标签
- MyBatis标签
- MyBatis标签
- mybatis标签
- RedisTemplate访问Redis数据结构(一)——String
- mysql 查看连接数 查看被锁的表
- 用ViewPage实现不同的滑动效果
- 让控件融入系统状态栏(沉浸式)
- JAVA中对list map根据map某个key值进行排序
- MyBatis select标签
- 基于嵌入式linux iic 总线读写mpu6050
- MySQL mysql-5.7.17 的配置与搭建
- Codeforces Round #433 (Div. 2)A&B
- Learning Path : Your mentor to become a machine learning expert
- 三极管工作状态
- jquery ui table
- dll导出接口
- 向量化与for循环耗时对比——python实现