ssm实现web项目

本文详细展示了在SpringBoot项目中,如何使用MyBatis的XML映射文件(CustomerMapper.xml)定义CRUD操作,以及在CustomerServlet控制器中调用这些操作的方法实现.
摘要由CSDN通过智能技术生成

sql语句

CustomerMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.fs.dao.CustomerDao">

    <insert id="customerInsert" parameterType="Customer">
        insert into customer values
                        (null,#{cardNum},#{name},#{idCrad},#{money},#{password},#{typeId})
    </insert>

    <select id="queryById" parameterType="int" resultType="Customer">
        SELECT * FROM customer where id = #{id}
    </select>

    <select id="queryAll" resultMap="CustomerMap">
        SELECT * FROM customer c JOIN type t ON c.typeId=t.typeId
    </select>

    <select id="customerDelete" parameterType="int">
        delete from customer where id = #{id}
    </select>
    <select id="queryByName" resultType="Customer">
        SELECT * FROM customer where name = #{name}
    </select>
    <select id="queryNum" resultType="int">
        SELECT count(1) FROM customer
    </select>
    <select id="queryByCustomerVO" parameterType="com.fs.vo.CustomerVO" resultMap="CustomerMap">
        SELECT * FROM customer c JOIN type t ON c.typeId=t.typeId where 1=1
        <if test="cardNum!=null">
            and c.cardNum = #{cardNum}
        </if>
        <if test="cName!=null and cName != ''">
            and c.cName = #{name}
        </if>
        <if test="idCrad!=null and idCrad != ''">
            and c.idCrad = #{idCrad}
        </if>
        <if test="typeId!=null">
            and c.typeId = #{typeId}
        </if>
    </select>

    <update id="customerUpdate" parameterType="Customer">
        update customer set cardNum=#{cardNum},name=#{name},idCrad=#{idCrad},money=#{money},password=#{password},typeId=#{typeId} where id=#{id}
    </update>

    <resultMap id="CustomerMap" type="Customer">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="cardNum" property="cardNum"/>
        <result column="idCrad" property="idCrad"/>
        <result column="money" property="money"/>
        <result column="cPassword" property="password"/>
        <result column="typeId" property="typeId"/>
        <association property="type" resultMap="TypeMap"/>
    </resultMap>

    <resultMap id="TypeMap" type="Type">
        <id column="typeId" property="typeId"/>
        <id column="typeName" property="typeName"/>
    </resultMap>
</mapper>

控制器 

CustomerServlet

package com.fs.controller;

import com.alibaba.fastjson.JSON;
import com.fs.pojo.Customer;
import com.fs.pojo.Type;
import com.fs.service.CustomerService;
import com.fs.service.TypeService;
import com.fs.vo.CustomerVO;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author hua
 * @version 1.0
 * @title insertCustomer
 * @description
 * @create 2024/3/4 16:17
 */
@Controller
public class CustomerServlet{
    @Resource
    private CustomerService customerService;
    @Resource
    private TypeService typeService;


    @RequestMapping("/add.action")
    @ResponseBody
    public String add(@RequestBody Customer customer) throws Exception {
        String json="";
        int i = customerService.customerInsert(customer);
        if(i>0) {
//            modelAndView.addObject("msg", "添加成功");
            json = "{\"message\":\"添加成功\",\"code\":100}";
        }else {
//            modelAndView.addObject("msg", "添加失败");
            json = "{\"message\":\"添加失败\",\"code\":400}";
        }
        return json;
    }

    @RequestMapping("/getAll.action")
    @ResponseBody
    public List<Customer> getAll() throws Exception {
//        String json="";
        List<Customer> customers = customerService.queryAll();
//        json= JSON.toJSONString(customers);
        return customers;
    }

    @RequestMapping("/getPage.action")
    @ResponseBody
    public List<Customer> getPage(int pageNum) throws Exception {
//        String json="";
        PageHelper.startPage(pageNum, 5);
        List<Customer> customers = customerService.queryAll();
//        json= JSON.toJSONString(customers);
        return customers;
    }

    @RequestMapping("/getPageByVO.action")
    @ResponseBody
    public List<Customer> getPageByVO(int pageNum,@RequestBody CustomerVO customerVO) throws Exception {
//        String json="";
        PageHelper.startPage(pageNum, 5);
        List<Customer> customers = customerService.queryByCustomerVO(customerVO);
//        json= JSON.toJSONString(customers);
        return customers;
    }

    @RequestMapping("/getNum.action")
    @ResponseBody
    public String getNum() throws Exception {
        String json="";
        int pageSum = customerService.queryNum();
        json = "{\"message\":"+pageSum+",\"code\":100}";
        return json;
    }

    @RequestMapping("/getType.action")
    @ResponseBody
    public List<Type> getType() throws Exception {
//        String json="";
        List<Type> types = typeService.queryAll();
//        json= JSON.toJSONString(types);
        return types;
    }

    @RequestMapping("/getOne.action")
    @ResponseBody
    public Customer getOne(int cId) throws Exception {
//        String json="";
        Customer customer = customerService.queryById(cId);
//        json= JSON.toJSONString(customer);
        return customer;
    }

    @RequestMapping("/editOne.action")
    @ResponseBody
    public String editOne(Customer customer) throws Exception {
        String json="";
        int i = customerService.customerUpdate(customer);
        if(i>0) {
            json = "{\"message\":\"修改成功\",\"code\":100}";
        }else {
            json = "{\"message\":\"修改失败\",\"code\":400}";
        }
        return json;
    }

    @RequestMapping("/removeOne.action")
    @ResponseBody
    public String removeOne(int cId) throws Exception {
        String json="";
        customerService.customerDelete(cId);
        json = "{\"message\":\"删除成功\",\"code\":100}";
        return json;
    }
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值