(ps:前兩章咱們已經把管理員登陸和查看用戶的功能實現了,那麼今天咱們將要實現:添加用戶,刪除用戶,和修改用戶功能)html
因爲Cusomer的POJO類型已經寫好了,因此此次咱們以前從CustomerController下手!!!mysql
①在CutsomerController類中編寫customerCreate方法,並在方法名上頭寫上請求映射路徑(@RequestMapping("/customer/create.action")) ,和@ResponseBody。sql
②在customerCreate方法中先獲取用戶session,而後將當前用戶id存儲在客戶對象中,而後調用customer.setCust_create_id()方法將當前用戶id存儲在客戶對象中,接着爲了獲得mysql裏面的時間戳,咱們用了Timestamp對象獲取一個yyyy/MM/dd HH:mm:ss 的時間格式,而後將這個Timestamp對象裝載到customer.setCust_createtime()方法中,最後再判斷Service層中受影響的行數來判斷是否建立用戶成功,這樣咱們CustomerController中的建立客戶就寫完了,還記得第二章教程中的流程圖嗎?如今咱們就得去Service層中編寫接口,並實現接口類。session
③在CustomerService接口中建立createCustomer方法(注意此處的返回值類型是int類型),而後去CustomerServiceImpl實現類中實現該接口方法。app
④在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語句"。 spa
/*
* 更新客戶
*/
@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:.net
<!-- 更新客戶 -->
<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管理系統源碼下載code