Boot-crm管理系统开发教程(三)

    (ps:前两章我们已经把管理员登录和查看用户的功能实现了,那么今天我们将要实现:添加用户,删除用户,和修改用户功能)

    由于Cusomer的POJO类型已经写好了,所以这次我们之前从CustomerController下手!!!

        添加用户功能

    ①在CutsomerController类中编写customerCreate方法,并在方法名上头写上请求映射路径(@RequestMapping("/customer/create.action")) ,和@ResponseBody。

     ②在customerCreate方法中先获取用户session,然后将当前用户id存储在客户对象中,然后调用customer.setCust_create_id()方法将当前用户id存储在客户对象中,接着为了得到mysql里面的时间戳,我们用了Timestamp对象获取一个yyyy/MM/dd HH:mm:ss 的时间格式,然后将这个Timestamp对象装载到customer.setCust_createtime()方法中,最后再判断Service层中受影响的行数来判断是否创建用户成功,这样我们CustomerController中的创建客户就写完了,还记得第二章教程中的流程图吗?现在我们就得去Service层中编写接口,并实现接口类。

     ③在CustomerService接口中创建createCustomer方法(注意此处的返回值类型是int类型),然后去CustomerServiceImpl实现类中实现该接口方法。

        ④在CustomerDao接口中也同样编写createCustomer方法,然后在CustomerDao.xml中编写添加客户的sql语句,代码如下:

<!-- 添加客户 -->
	<insert id="createCustomer" parameterType="customer">
	    insert into customer(
	                     cust_name,
	                     cust_user_id,
	                     cust_create_id,
	                     cust_source,
	                     cust_industry,
	                     cust_level,
	                     cust_linkman,
	                     cust_phone,
	                     cust_mobile,
	                     cust_zipcode,
	                     cust_address,
	                     cust_createtime
	             )
	             values(#{cust_name},
	                    #{cust_user_id},
	                    #{cust_create_id},
	                    #{cust_source},
	                    #{cust_industry},
	                    #{cust_level},
	                    #{cust_linkman},
	                    #{cust_phone},
	                    #{cust_mobile},
	                    #{cust_zipcode},
	                    #{cust_address},
	                    #{cust_createtime}
	            )
	</insert>

      ⑤写到这里,我们的添加用户的功能就写完了。回顾一下我们是怎么写的:"首先我们是从CustomerController下手的!在该方法中我们创建了createCustomer方法,然后再到Service层中编写createCustomer该接口方法,然后让CustomerServiceImpl实现类去实现它,最后在回到CustomerDao中创建同样的方法,然后重点是在CustomerDao.xml中的sql语句"。       

    更新用户功能,删除用户功能

           经过之前查看用户和创建用户功能,相信大家对SSM框架的运用已经了如指掌了,那么省下的两个功能,我将Controller和sql的代码粘贴到这,其他的大家自己写写试试,写不出来也没关系,源码已经分享到CSDN上了,待会会把网址贴在文章底部。
            Controller:
/*
	 * 更新客户
	 */
	@RequestMapping("/customer/update.action")
	@ResponseBody
	public String customerUpdate(Customer customer)
	{
		int rows=customerService.updateCustomer(customer);
		if(rows>0)
		{
			return "OK";
		}else {
			return "FAIL";
		}
	}
	/*
	 * 删除客户
	 */
	@RequestMapping("/customer/delete.action")
	@ResponseBody
	public String customerDelete(Integer id)
	{
		int rows=customerService.deleteCustomer(id);
		if(rows>0)
		{
			return "OK";
		}else {
			return "FAIL";
		}
	}

                XML:

<!-- 更新客户 -->
	<update id="updateCustomer" parameterType="customer">
	    update customer
	    <set>
	        <if test="cust_name!=null">
	            cust_name=#{cust_name},
	        </if>
	        <if test="cust_user_id!=null">
	            cust_user_id=#{cust_user_id},
	        </if>
	        <if test="cust_create_id!=null">
	            cust_create_id=#{cust_create_id},
	        </if>
	        <if test="cust_source!=null">
	            cust_source=#{cust_source},
	        </if>
	        <if test="cust_industry!=null">
	            cust_industry=#{cust_industry},
	        </if>
	        <if test="cust_level!=null">
	            cust_level=#{cust_level},
	        </if>
	        <if test="cust_linkman!=null">
	            cust_linkman=#{cust_linkman},
	        </if>
	        <if test="cust_phone!=null">
	            cust_phone=#{cust_phone},
	        </if>
	        <if test="cust_mobile!=null">
	            cust_mobile=#{cust_mobile},
	        </if>
	        <if test="cust_zipcode!=null">
	            cust_zipcode=#{cust_zipcode},
	        </if>
	        <if test="cust_address!=null">
	            cust_address=#{cust_address},
	        </if>
	        <if test="cust_createtime!=null">
	            cust_createtime=#{cust_createtime},
	        </if>
	    </set>
	    where cust_id=#{cust_id}
	</update>
	<!-- 删除客户 -->
	<delete id="deleteCustomer" parameterType="Integer">
	    delete from customer where cust_id=#{id}
	</delete>

           源码下载地址: Boot-crm管理系统源码下载

原文地址:https://www.cnblogs.com/Black-YeJing/p/9223630.html