mybatis sql in 查询 mapper与service层写法

来源:互联网 发布:linux查看网口状态命令 编辑:程序博客网 时间:2024/06/02 22:38
  1. 当查询的参数只有一个时
    findByIds(List ids)
    1.1 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list(要是Repository 层指定参数必须与参数一致)
    Xml代码 收藏代码
<select id="findByIdsMap" resultMap="BaseResultMap">   Select   <include refid="Base_Column_List" />   from jria where ID in   <foreach item="item" index="index" collection="list" open="(" separator="," close=")">    #{item}   </foreach>  </select>  

findByIds(Long[] ids)
1.2 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array(要是Repository 层指定参数必须与参数一致)
Xml代码 收藏代码

<select id="findByIdsMap" resultMap="BaseResultMap">  select  <include refid="Base_Column_List" />  from tabs where ID in  <foreach item="item" index="index" collection="array" open="(" separator="," close=")">   #{item}  </foreach>     </select>  
  1. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)
    这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
    下面是一个示例
    Map
<select id="findByIdsMap" resultMap="BaseResultMap">   select   <include refid="Base_Column_List" />   from tabs where ID in   <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">    #{item}   </foreach>  </select>  

下面看一下部分代码
讲的是当查询参数只有一个而且是List的情况我,我们用的最多

@Repositorypublic interface OrgnizationRepository {    1. List<Orgnization> selectListIds(@Param("lists") List<Long> ids);    2. List<Orgnization> selectListIds( List<Long> ids);}
public interface OrgnizationService {    public List<OrgnizationModel> selectListIds(List<Long> listLong);}
@Servicepublic class OrgnizationServiceImpl implements OrgnizationService {    @Autowired    private BeanMapper beanMapper;    @Autowired    private OrgnizationRepository orgnizationRepo;    @Override    public List<OrgnizationModel> selectListIds(List<Long> ids) {        List<Orgnization> list = orgnizationRepo.selectListIds(ids);        return beanMapper.mapAsList(list, OrgnizationModel.class);    }}
public class OrgnizationServiceImplTest extends BaseTest {    @Autowired    OrgnizationService orgnizationService;    @Test    public void testSelectListIds() {        List<Long> list = new ArrayList<>();        list.add(Long.valueOf("633465951"));        list.add(Long.valueOf("633465952"));        List<OrgnizationModel> selectListIds =         orgnizationService.selectListIds(list);    }}
 <select id="selectListIds" resultMap="BaseResultMap">   select     1. <include refid="Base_Column_List"/>       from orgnization where is_deleted = 0 and orgn_id in       <foreach item="item" index="index" collection="lists" open="(" separator="," close=")">            #{item}        </foreach>    </select>   <select id="selectListIds" resultMap="BaseResultMap">2.必须list   select    2.  <include refid="Base_Column_List"/>       from orgnization where is_deleted = 0 and orgn_id in       <foreach item="item" index="index" collection="list" open="(" separator="," close=")">            #{item}        </foreach>    </select>
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 汕头交警 违章扣分怎么办办理 幼儿园家长不保险应该怎么办 csgo掉白银坑了怎么办 错过教资认定现场确认怎么办 乡村建设导致民房开裂怎么办 项目部公章丢了怎么办 手机掉了没有卡怎么办 苹果系统软件删了还是出现怎么办 钉钉检测到作弊怎么办 电脑麦说话声音小怎么办 穿越火线麦克风有杂音怎么办 手闲不住就抠东西怎么办 大便堵在肛门口怎么办 在外地流量不够用怎么办 电脑键盘数字键没反应怎么办 音响音量键坏了怎么办 摩托罗拉移动电话通话没声音怎么办 摩托罗拉对讲机充电座坏了怎么办 手机导航键太灵敏怎么办 经侦大队不立案怎么办 槐茂酱菜太咸怎么办 法院执法局执法不公怎么办 搞养殖卖不出去怎么办 孔雀吃了蚯蚓该怎么办 多肉幼苗长徒了怎么办 小鸵鸟嘴断了怎么办 武汉早谢怎么办博大直上 威猛先生伤手了怎么办 嗓子眼边条线下边长块肉怎么办 线长在肉里怎么办 北京买车没有号怎么办 租的车处理违章怎么办 以租代购车不要怎么办 订车的合同掉了怎么办 买车合同丢了怎么办 车的证件都丢了怎么办 桃树直立的徒长枝怎么办 月季花长了独枝怎么办? 6岁半B超没子宫怎么办 学生学籍号和身份证号不一致怎么办 学生学籍号和身份证号不一样怎么办