【若依(ruoyi)】向DAO中传递动态参数

向DAO中传递动态参数

比如,mapper中这样定义selectUserList:

<select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
	...
select>

上面的SysUser为orm类,SysUser的属性是sys_user表的字段。基于若依(ruoyi)自动生成的代码如何进行时间段检索呢?此时需要向DAO中传递动态参数。

假如按照用户的创建时间做时间段检索,可以这样做:

<select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
	...
	where u.del_flag = '0'
	<if test="params.beginTime != null and params.beginTime != ''">
		AND date_format(u.create_time,'%Y%m%d') >= date_format(#{params.beginTime},'%Y%m%d')
	if>
	<if test="params.endTime != null and params.endTime != ''">
		AND date_format(u.create_time,'%Y%m%d') <= date_format(#{params.endTime},'%Y%m%d')
	if>
select>

上面的params.beginTimeparams.endTime就是动态参数(beginTimeendTimeSysUser类中不存在的属性,但,通过SysUser父类中params属性添加到了SysUser类中)。

调用DAO时,传递动态参数

SysUser user = new SysUser();
Map<String, Object> params = user.getParams();
params.put("beginTime", '2020-01-01');
params.put("endTime", '2020-01-31');
sysUserService.selectUserList(user);

页面上提交表单时,传递动态参数

<form id="user-form">
	<div class="select-list">
		<ul>
			<li class="select-time">
				<label>创建时间: label>
				<input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params['beginTime']"/>
				<span>-span>
				<input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params['endTime']"/>
			li>
			<li>
				<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search">i> 搜索a>
			    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh">i> 重置a>
			li>
		ul>
	div>
form>
@RequiresPermissions("system:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

DATE_FORMAT

SELECT DATE_FORMAT('2020-10-09 08:30:00',   '%Y-%m-%d %H:%i:%S');
SELECT DATE_FORMAT('2020-10-09',   '%Y-%m-%d');

你可能感兴趣的:(ruoyi,java,java,mybatis,mysql)