SSM项目思路整合NEW2

上接于 https://www.cnblogs.com/shijinglu2018/p/10374541.html

 ......

 

三)客户管理模块开发

说明:其实大致思路差不太多,都是首先根据前端页面分析,并用mvc思想完成相应的功能。

mvc : 前端页面<--->控制器<--->业务层<--->数据访问对象层。

         前端页面用于显示,并且每一个小模块都是死的,这是就需要站在使用者的角度去分

         析和判断并写相应的控制器,如页面的跳转和页面信息的会显等(这里主要用是

         SpringMVC和Servlet还有前端等技术),为了降低项目各个模块的耦合性,方便后期

         修改,分离出一个业务层,这个业务层的主要接收控制层参数,并返回给控制层,另

         外,相应的异常也是在此层中进行处理的(这里主要用到的是Spring模块),最后要

         连接数据库,到DB中取出相应的数据,即JDBC,而这里主要是用MyBatis来实现。

         另外各个层之间可以用@AutoWire或者是@Resource来进行进行类的自动装配,也可

         看成是一种多态的使用。

具体实现思路参看:https://blog.csdn.net/weixin_41871468/article/details/81329712

    1. 先写实体类entity,定义对象的属性,(可以参照数据库中表的字段来设置,数据库的设计应该在所有编码开始之前)。

    2. 写Mapper.xml(Mybatis),其中定义你的功能,对应要对数据库进行的那些操作,比如 insert、selectAll、selectByKey、delete、update等。

    3. 写Mapper.java/Dao.java,将Mapper.xml中的操作按照id映射成Java函数。实际上就是Dao接口,二者选一即可。

    4. 写Service.java,为控制层提供服务,接受控制层的参数,完成相应的功能,并返回给控制层。

    5. 写Controller.java,连接页面请求和服务层,获取页面请求的参数,通过自动装配,映射不同的URL到相应的处理函数,并获取参数,对参数进行处理,之后传给服务层。

    6. 写JSP页面调用,请求哪些参数,需要获取什么数据。

 
                  其实,在实际开发时,可能是先有的前端页面,再有的服务器端和数据库等,所以也可将以上顺序整个倒过来。
 

1.查询客户

  ......

 

2.添加客户

1)先测试一下前端页面,看看并打开源代码。

<a href="#" class="btn btn-primary" data-toggle="modal" 
           data-target="#newCustomerDialog" onclick="clearCustomer()">新建</a>

 

//清空新建客户窗口中的数据
    function clearCustomer() {
        $("#new_customerName").val("");
        $("#new_customerFrom").val("")
        $("#new_custIndustry").val("")
        $("#new_custLevel").val("")
        $("#new_linkMan").val("");
        $("#new_phone").val("");
        $("#new_mobile").val("");
        $("#new_zipcode").val("");
        $("#new_address").val("");
    }
    // 创建客户
    function createCustomer() {
    $.post("<%=basePath%>customer/create.action",
    $("#new_customer_form").serialize(),function(data){
            if(data =="OK"){
                alert("客户创建成功!");
                window.location.reload();
            }else{
                alert("客户创建失败!");
                window.location.reload();
            }
        });
    }

2)Controller层

//@Controller
/**
* 控制器:添加客户
* 添加一个session,判断是否该用户已经登录了。但其实也可以不用加,因为登录有一个拦截器,
* 如果用户可以做新增用户操作,说明该用户已经登录。
* */
@RequestMapping("customer/create.action")
@ReponseBody
public String customerCreate(Customer customer,HttpSession session){
//获取Session中的当前用户信息
User user=(User)session.getAttribute("USER_SESSION");
//将当前用户id存储在客户对象中
Integer id=customer.setCust_create_id(user.getUser_id());
//如果改为custmoerServiceImpl应该也行吧,这里是向上造型,加强了其使用的可扩展性。
int rows=customerService.customerCreate(customer);
if(rows>0){
return "OK";
}else{
return "FAIL";
}
}

3)Service层

//@Service
/**
* 接口:添加用户
* */
public String customerCreate(Customer customer);

/**
* 接口的实现类中的方法:添加用户
*/
public String customerCreate(Customer customer){
return customDao.customerCreate(customer);
}

4)Dao层

/**
* DAO层:添加用户
*/
public String customerCreate(Customer customer);

 

/**
* 对应的映射文件中的方法
* 说明:paramterType后面参数名称可以小写,因为前面哪个位置设置了的。
* 另外,前端页面(弹出来的对话框和对应的数据库中的表,实体类)
* */
<insert id="customerCreate" paramterType="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_source},
#{cust_industry},
#{cust_level},
#{cust_linkman},
#{cust_phone},
#{cust_mobile},
#{cust_zipcode},
#{cust_address},
#{cust_createtime}
}
</insert>

 

3.修改客户

...

1)Controller层

/**
* 更新客户
*/
@RequestMapping("/customer/update.action")
@ResponseBody
public String customerUpdate(Customer customer) {
int rows = customerService.updateCustomer(customer);
if(rows > 0){
return "OK";
}else{
return "FAIL";
}
}

 

2)业务层

// 更新客户
public int updateCustomer(Customer customer);

3)Dao层

// 更新客户信息
    public int updateCustomer(Customer customer);
    <!-- 更新客户 -->
    <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>

 

 

 

4.删除客户功能

1)分析前端页面:

点击删除用户:

    <a href="#" class="btn btn-danger btn-xs" onclick="deleteCustomer(${row.cust_id})">删除</a>

执行并弹出以下内容:

    // 删除客户
    function deleteCustomer(id) {
        if(confirm('确实要删除该客户吗?')) {
    $.post("<%=basePath%>customer/delete.action",{"id":id},
    function(data){
                if(data =="OK"){
                    alert("客户删除成功!");
                    window.location.reload();
                }else{
                    alert("删除客户失败!");
                    window.location.reload();
                }
            });
        }
    }

2)依据customer/delete.action写控制器,一个删除用户的方法。

      /**
     * 删除客户
     */
    @RequestMapping("/customer/delete.action")
    @ResponseBody
    public String customerDelete(Integer id){
        int rows=customerService.deleteCustomer(id);
        if (rows>0){
            return "OK";
        }else{
            return "FAIL";
        }
    }

       说明: 这个删除功能没有太多操作,调用业务层接口中的方法来获取数据库中受影响的行数,如果大于0则删除成功,否则删除失败。

3)实现业务层方法

接口:

// 删除客户
public int deleteCustomer(Integer id);

实现类:

      /**
     * 删除客户
     */
    @Override
    public int deleteCustomer(Integer id){
        return customerDao.deleteCustomer(id);
    }

4)实现DAO层方法

接口:

// 删除客户
public int deleteCustomer(Integer id);

实现类(这个封装了JDBC,MyBatis底层封装了实现类):

<!-- 删除客户 -->
    <delete id="deleteCustomer" parameterType="Integer">
        delete from customer where cust_id=#{id}
    </delete>

 

复杂的事情简单做,简单的事情重复做。

学会重复,学会复盘,学会输入和输出,输入输出竟可能对等。

posted @ 2019-03-31 15:35  贰零一八  阅读(198)  评论(0编辑  收藏  举报