MyBatis中动态SQL语句完成多条件查询
来源:互联网 发布:百乐淘宝 编辑:程序博客网 时间:2024/05/17 05:09
MyBatis中动态SQL语句完成多条件查询,条件可以为字符串,数值,时间,以及自定义的类.
写的不好之处请指教~
MyBatis的XML:
<mapper namespace="com.dao.NotifyManagementInfoDao">
<resultMap type="com.model.NotifyManagementInfo" id="notifyManagementInfoResultMap">
<result column="streaming_no" property="streamingNo" />
<result column="id" property="id" />
<result column="id_type" property="idType" />
<result column="status" property="status" />
<result column="time" property="time" />
<result column="result_code" property="resultCode" />
</resultMap>
<select id="find" parameterType="NotifyManagementInfo" resultMap="notifyManagementInfoResultMap">
select * from notifymanagementinfo
where 1 = 1
<if test="notifyManagementInfo.streamingNo != null">
and streaming_no like concat('%',concat(#{notifyManagementInfo.streamingNo},'%'))
</if>
<if test="notifyManagementInfo.idType != -1">
and id_type = #{notifyManagementInfo.idType}
</if>
<if test="notifyManagementInfo.status != -1">
and status = #{notifyManagementInfo.status}
</if>
<if test="start != null">
and time > #{start,jdbcType=TIMESTAMP}
</if>
<if test="end != null">
and time < #{end,jdbcType=TIMESTAMP}
</if>
</select>
</mapper>
DAO层:
public List<NotifyManagementInfo> find(@Param("notifyManagementInfo")NotifyManagementInfo notifyManagementInfo,
@Param("start") Timestamp start,@Param("end") Timestamp end);
Service:
public List<NotifyManagementInfo> find(NotifyManagementInfo notifyManagementInfo,
String start,String end);
Impl:
public List<NotifyManagementInfo> find(
NotifyManagementInfo notifyManagementInfo, String start, String end) {
Timestamp startTime = new Timestamp(System.currentTimeMillis());
Timestamp endTime = new Timestamp(System.currentTimeMillis());
try {
if (start.length() != 0) {
//startTime = Timestamp.valueOf(start + " 00:00:00");
startTime = Timestamp.valueOf(start);
}else{
startTime = null;
}
if (end.length() != 0) {
//endTime = Timestamp.valueOf(end + " 00:00:00");
endTime = Timestamp.valueOf(end);
}else{
endTime = null;
}
} catch (Exception e) {
e.printStackTrace();
}
return notifyManagementInfoDao.find(notifyManagementInfo, startTime, endTime);
}
如果参数为String,dao中带参为@Param("test") String test,并且在MyBatis的XML中的<if test="test != null">即可;
如果参数为int,dao中带参为@Param("test") int test,并且在MyBatis的XML中的<if test="test != -1">即可;(如果在数据库中该参数有0,1,2,3...就是没有-1的值,取-1即可;如有-1取不可能数值就行)
如果参数为自定义类,dao中带参为:
public void alterResultCode(NotifyManagementInfo notifyManagementInfo);
这样在xml中可直接取自定义类的属性.如:
<insert id="add" parameterType="NotifyManagementInfo" >
insert into notifymanagementinfo(streaming_no,id,id_type,status,time,result_code)
values(#{streamingNo},#{id},#{idType},#{status},#{time},#{resultCode})
</insert>
但是如果参数有时间,要传入开始时间,结束时间,即:
public List<NotifyManagementInfo> find(@Param("notifyManagementInfo")NotifyManagementInfo notifyManagementInfo,
@Param("start") Timestamp start,@Param("end") Timestamp end);
XML就必须和上面的XML的一样.通过类点属性把它点出来,否则找不到.
- MyBatis中动态SQL语句完成多条件查询
- MyBatis中动态SQL语句完成多条件查询
- Mybatis中动态sql(多条件查询)
- mybatis中SQL块使用/动态条件查询
- Mybatis动态Sql语句查询
- mybatis多条件语句查询
- Mybatis 多条件动态查询
- mybatis的多条件查询案例(动态sql)
- Mybatis动态SQL 条件查询,批量删除
- mybatis分页条件查询动态sql
- MySQL中根据if标签实现多条件模糊查询(动态SQL语句)
- 根据条件查询动态拼接sql语句
- 动态sql语句拼接查询条件
- Mybatis动态sql条件查询中if判断Integer的条件写法
- 多条件查询SQL语句
- SQL多条件查询语句
- 多条件查询SQL语句
- sql多条件查询语句
- Docker4Dev#7 使用 Windows Container运行ASP.NET MVC 2 + SQLExpress 应用
- 理解并从头搭建redis集群
- JQuery中如何重置初始化表单(reset)
- .NET 十五岁,谈谈我眼中的.NET
- 软件定义数据中心—Windows Server SDDC技术与实践
- MyBatis中动态SQL语句完成多条件查询
- Windows 容器
- Docker4Dev #6 使用 Windows Container 运行.net应用
- .Net大户的选择:Windows Container在携程的应用
- .Net Core 之 MSBuild 介绍
- 使用VS Code从零开始开发调试.NET Core 1.1
- 用户注册界面
- 程序员的情人节礼物:当天微软开始Build 2017登记
- OSS.Common获取枚举字典列表标准库支持