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;
}
}