MSG生成mapper后之有基本增删改查操作
若添加动态条件查询,还需要另外编写动态查询语句
如下代码为例
1. 注释方式
CrawlerWebsiteSqlProvider.java
public String selectFilterSelective(String websiteName, String websiteStatus, String websiteType, String createTimeStart ,String createTimeEnd) {
SQL sql = new SQL();
sql.SELECT("*");
sql.FROM("crawler_website");
if (websiteName != null&&websiteName != "") {
sql.WHERE("WEBSITE_NAME = '"+websiteName+"'");
}
if (websiteType != null&&websiteType != "") {
sql.WHERE("WEBSITE_TYPE = '"+websiteType+"'");
}
if (websiteStatus != null&&websiteStatus != "") {
sql.WHERE("WEBSITE_STATUS = "+websiteStatus);
}
if (createTimeStart != null&&createTimeEnd!=null) {
sql.WHERE("CREATE_TIME >= '"+createTimeStart+"'");
sql.AND();
sql.WHERE("CREATE_TIME <= '"+createTimeEnd+"'");
}
System.out.println(sql.toString());
return sql.toString();
}
CrawlerWebsiteMapper.java
@SelectProvider(method="selectFilterSelective",type=CrawlerWebsiteSqlProvider.class)
@Results({
@Result(column = "WEBSITE_ID", property = "websiteId",javaType=String.class, jdbcType=JdbcType.VARCHAR, id=true),
@Result(column = "WEBSITE_NAME", property = "websiteName"),
@Result(column = "WEBSITE_TYPE", property = "websiteType"),
...
})
List<CrawlerWebsite> getFilterAll(String websiteName, String websiteStatus, String websiteType, String createTimeStart ,String createTimeEnd );
2. XML方式
<select id="selectSelective" parameterType="Map" resultMap="BaseResultMap">
select * from crawler_item_info
<where>
<if test="itemId != null">
AND ITEM_ID = #{itemId,jdbcType=VARCHAR}
</if>
<if test="itemName != null">
AND ITEM_NAME LIKE CONCAT(CONCAT('%', #{itemName,jdbcType=VARCHAR}), '%')
</if>
<if test="exampleData != null">
AND EXAMPLE_DATA = #{exampleData,jdbcType=VARCHAR}
</if>
<if test="exampleUrl != null">
AND EXAMPLE_URL = #{exampleUrl,jdbcType=VARCHAR}
</if>
<if test="itemStatus != null">
AND ITEM_STATUS = #{itemStatus,jdbcType=DECIMAL}
</if>
<if test="createTime != null">
AND CREATE_TIME <![CDATA[>=]]> #{createTime,jdbcType=DATE}
</if>
<if test="modifyTime != null">
AND MODIFY_TIME <![CDATA[>=]]> #{modifyTime,jdbcType=DATE}
</if>
<if test="founderId != null">
AND FOUNDER_ID = #{founderId,jdbcType=VARCHAR}
</if>
<if test="modifierId != null">
AND MODIFIER_ID = #{modifierId,jdbcType=VARCHAR}
</if>
</where>
</select>
网友评论