2021-06-10-JavaEE-SSM整合

请求状态提示

根据 状态码 判断
“StatusCode”: 100,
表示数据不为空
“StatusDescription”: “请求成功。”,
提示“请求成功。”

	"DataStatus": {
		"RequestParameter": "keywords=烽火&pagesize=10&pagenumber=1&type=&searchtype=all",
		"StatusCode": 100,
		"StatusDescription": "请求成功。",
		"ResponseDateTime": "2021-06-10 09:25:05",
		"DataTotalCount": 244
	},

Update

根据id更改
在这里插入图片描述

Add

id自增,不用写
返回的“1”为改变的行数,新增了一行即改变了一行。
在这里插入图片描述

Delete

根据id删除
在这里插入图片描述

FindAll

查询全部
在这里插入图片描述

FindById

根据id查询
在这里插入图片描述

CustomerDao.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.dao.CustomerDao">
    <!--根据id查询-->
    <select id="findCustomerById" parameterType="Integer" resultType="Customer">
        select * from t_customer where id =#{id}
    </select>
<!--    查询所有用户-->
    <select id="findAllCustomer" resultType="Customer">
        select * from t_customer
    </select>
<!---->
    <delete id="deleteCustomer" parameterType="Integer">
        delete from t_customer where id=#{id}
    </delete>
<!---->
    <insert id="addCustomer" parameterType="Customer">
        insert into t_customer(username,jobs,phone) values (#{username},#{jobs},#{phone})
    </insert>
<!---->
    <update id="updateCustomer" parameterType="Customer">
        update t_customer
        <set>
            <if test="username !=null and username !=''">
                username =#{username},
            </if>
            <if test="jobs !=null and jobs !=''">
                jobs =#{jobs},
            </if>
            <if test="phone !=null and phone !=''">
                phone =#{phone},
            </if>
        </set>
        where id=#{id}
    </update>
</mapper>

还有个CustomerDao.java接口与CustomerService.java差不多

CustomerService

package com.service;
import com.po.Customer;

import java.util.List;

public interface CustomerService {
    public Customer findCustomerById(Integer id);
    public List<Customer> findAllCustomer();
    public int deleteCustomer(Integer id);
    public int addCustomer(Customer customer);
    public int updateCustomer(Customer customer);
}

CustomerServiceImpl

package com.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dao.CustomerDao;
import com.po.Customer;
import com.service.CustomerService;

import java.util.List;

@Service
//注解@Transactional作用:对数据的增、修、改进行事务管理
@Transactional
public class CustomerServiceImpl implements CustomerService {
    //注解注入CustomerDao
    @Autowired
    private CustomerDao customerDao;
    //查询客户
    public Customer findCustomerById(Integer id) {
        return this.customerDao.findCustomerById(id);
    }

    @Override
    public List<Customer> findAllCustomer() {
        return this.customerDao.findAllCustomer();
    }

    @Override
    public int deleteCustomer(Integer id) {
        return this.customerDao.deleteCustomer(id);
    }

    @Override
    public int addCustomer(Customer customer) {
       return this.customerDao.addCustomer(customer);
    }

    @Override
    public int updateCustomer(Customer customer) {
        return this.customerDao.updateCustomer(customer);
    }
}

CustomerController

package com.controller;

import com.po.Customer;
import com.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@CrossOrigin
@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @ResponseBody
    @RequestMapping("/findCustomerById")
    public Customer findCustomerById(Integer id) {
        Customer customer = customerService.findCustomerById(id);
        return customer;
    }
//    查询所有
    @ResponseBody
    @RequestMapping("/findAllCustomer")
    public List<Customer> findAllCustomer(){
        List<Customer> customers = customerService.findAllCustomer();
        return customers;
    }
    //删
    @ResponseBody
    @RequestMapping("/deleteCustomer")
    public int deleteCustomer(Integer id) {
        return customerService.deleteCustomer(id);//返回改变的(删除的)行数
    }
    //增
    @ResponseBody
    @RequestMapping("/addCustomer")
    public int addCustomer(@RequestBody Customer customer) {
        return customerService.addCustomer(customer);
    }
    //改
    @ResponseBody
    @RequestMapping("/updateCustomer")
    public int updateCustomer(@RequestBody Customer customer) {
        return customerService.updateCustomer(customer);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦中千秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值