<select id="xcxGetAnnounce" resultMap="policyVo"> SELECT zhengce.id,zhengce.title, zhengce.dispatch_date,zhengce.declare_date, zhengce.fw_department <if test="sysType != null and sysType == 3 and keyWord!=null and keyWord!=''"> ,MATCH(zhengce.title) AGAINST(REPLACE(#{keyWord},' ','')) AS score </if> FROM oa_zhengce zhengce WHERE type=2 AND zhijian_status=5 AND status=1 AND ( zhengce.zc_jibie=1 OR <choose> <when test="sysType != null and sysType == 3"> zhengce.area='440604' OR ( zhengce.zc_jibie=2 and zhengce.prov='440000') OR (zhengce.zc_jibie=3 and zhengce.city='440600') </when> <when test="sysType != null and sysType == 2"> zhengce.street='440604010' OR ( zhengce.zc_jibie=2 and zhengce.prov='440000') OR (zhengce.zc_jibie=3 and zhengce.city='440600') OR (zhengce.zc_jibie=4 and zhengce.city='440604') </when> <otherwise> zhengce.city=#{city} OR ( zhengce.zc_jibie=2 and zhengce.prov=#{prov}) </otherwise> </choose> ) <choose> <when test="sysType != null and sysType == 2 and keyWord!=null and keyWord!=''"> AND REPLACE(zhengce.title,' ','') like CONCAT('%',REPLACE(#{keyWord},' ',''),'%') </when> <when test="sysType != null and sysType == 3 and keyWord!=null and keyWord!=''"> AND MATCH(zhengce.title) AGAINST(REPLACE(#{keyWord},' ','')) </when> </choose> <choose> <when test="keyWord==null or keyWord==''"> ORDER BY zhengce.dispatch_date desc,zhengce.id </when> <otherwise> ORDER BY score desc ,zhengce.dispatch_date desc, zhengce.id desc </otherwise> </choose> </select>
时间: 2024-02-15 14:28:54 浏览: 23
这是一个 MyBatis 的 XML 映射文件,用于查询 oa_zhengce 表中的政策数据。它包含一个 SELECT 语句,其中包含多个 SQL 语句片段,用于根据不同的输入参数动态构建查询条件。
具体来说,该 SELECT 语句将返回 oa_zhengce 表中类型为 2、审核状态为 5、发布状态为 1,且符合特定级别和地区要求的政策数据。其中,如果输入参数 sysType 为 3,且输入参数 keyWord 不为 null 或空字符串,则会使用 MATCH AGAINST 函数对政策标题进行全文检索,并按照检索得分从高到低排序;否则,将按照发布日期和政策 id 从新到旧排序。
该 SELECT 语句还包含多个 SQL 片段,可根据不同的输入参数和条件动态构建查询条件。例如,如果输入参数 sysType 为 2,则政策所在的地区必须为指定的街道或城市,否则政策所在的地区必须为指定的城市或省份。如果输入参数 keyWord 不为 null 或空字符串,则还需要对政策标题进行模糊匹配或全文检索。
相关问题
<select id="getProductByStatusWhenCalculatePage" resultMap="productResultMap"> SELECT p.id, p.user_id, p.goods_name, GROUP_CONCAT(i.url SEPARATOR ',') AS images FROM pms_product AS p LEFT JOIN pms_product_images AS i ON p.id = i.product_id WHERE p.status = #{status} GROUP BY p.id </select> <resultMap id="productResultMap" type="Product"> <id property="id" column="id"/> <result property="userId" column="user_id"/> <result property="goodsName" column="goods_name"/> <collection property="images" ofType="java.lang.String"> <result column="images" /> </collection> </resultMap> resulttype用Map实现,不要使用vo类或实体类
如果要用 Map 类型来表示查询结果,可以将 resultMap 中的 type 属性设置为 java.util.Map,然后在 resultMap 中使用 map 标签来映射字段。
改写后的代码如下:
```
<select id="getProductByStatusWhenCalculatePage" resultMap="productResultMap">
SELECT p.id, p.user_id, p.goods_name, GROUP_CONCAT(i.url SEPARATOR ',') AS images
FROM pms_product AS p
LEFT JOIN pms_product_images AS i ON p.id = i.product_id
WHERE p.status = #{status}
GROUP BY p.id
</select>
<resultMap id="productResultMap" type="java.util.Map">
<result property="id" column="id" />
<result property="userId" column="user_id" />
<result property="goodsName" column="goods_name" />
<result property="images" column="images" javaType="java.lang.String" />
</resultMap>
```
在 resultMap 中,使用 result 标签将每个字段映射到 Map 中的相应 key 上。注意,images 字段需要显式地指定 javaType 为 java.lang.String,否则可能会出现类型转换错误。
<mapper namespace="com.kangxg.mapper.UserMapper"> <!-- 根据id 查询订单信息 --> <select id="findUserWithOrders" parameterType = "Integer" resultMap="UserWithOrdersResult"> select u.*,o.id as orders_id,o.number from tb_user u,tb_orders o where u.id = o.user_id and u.id = #{id} </select> <resultMap type="com.kangxg.po.User" id="UserWithOrdersResult"> <id property = "id" column = "id"/> <result property = "username" column = "username"/> <result property = "address" column = "address"/> <collection property = "com.kangxg.po.ordersList" ofType = "Orders"> <id property ="id" column ="orders_id"/> <result property ="number" column = "number"/> </collection> </resultMap> </mapper> 对以上代码进行优化改错
根据你提供的代码,我看到了一些问题。下面是对代码进行优化和修正的建议:
1. 首先,你需要将 `resultMap` 的定义放在 `select` 语句之前。
```xml
<mapper namespace="com.kangxg.mapper.UserMapper">
<resultMap type="com.kangxg.po.User" id="UserWithOrdersResult">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<collection property="ordersList" ofType="com.kangxg.po.Orders">
<id property="id" column="orders_id"/>
<result property="number" column="number"/>
</collection>
</resultMap>
<!-- 根据id查询订单信息 -->
<select id="findUserWithOrders" parameterType="Integer" resultMap="UserWithOrdersResult">
SELECT u.*, o.id AS orders_id, o.number
FROM tb_user u, tb_orders o
WHERE u.id = o.user_id
AND u.id = #{id}
</select>
</mapper>
```
2. 注意在 `collection` 元素中,属性名应该是 `ordersList`,而不是 `com.kangxg.po.ordersList`。此外,`ofType` 属性应该指定为 `com.kangxg.po.Orders`,而不是 `Orders`。
3. 确保 `parameterType` 的值与查询语句中的参数类型匹配。
请根据这些建议对你的代码进行修改,并确保其他相关的配置也正确无误。如果还有其他问题,请提供更多的信息。