请求状态提示
根据 状态码 判断
“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); } }